• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Android can not read device_id (TelephonyManager)

J

Juhran

Guest
For some reason my java code can't be compiled. I wanted to detect my device id inside my extension:

Code:
public String imei()
{

  //Get the instance of TelephonyManager 
        TelephonyManager  tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 
        
        //Calling the methods of TelephonyManager the returns the information 
        String IMEINumber=tm.getDeviceId(); 
        
                
        
return IMEINumber;

}
Error:
J:\com.company.game\src\main\java\com\company\game\testclass.java:42: error: cannot find symbol
TelephonyManager tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
^
symbol: method getSystemService(String)
location: class testclass
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: J:\com.company.game\src\main\java\com\company\game\Gamepad\Gamepad.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
:com.company.game:compileDebugJavaWithJavac FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':com.company.game:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
My Java file:
Code:
package ${YYAndroidPackageName};

import ${YYAndroidPackageName}.R;
import com.yoyogames.runner.RunnerJNILib;

import android.app.Activity;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.content.Context;
import android.util.Log;




import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;



public class testclass {
    

public String imei()
{

  //Get the instance of TelephonyManager 
        TelephonyManager  tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 
        
        //Calling the methods of TelephonyManager the returns the information 
        String IMEINumber=tm.getDeviceId(); 
        
                
        
return IMEINumber;

}

}
Not sure why is'nt working :-/
 
W

Wraithious

Guest
error: cannot find symbol
TelephonyManager tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
^ symbol: method getSystemService(String)
Hi, That error is telling you that to code thinks you're trying to call a method that doesn't exist. The problem is you may have missed an import library before the class code for that method call to work, and/or you may have to inject 1 or more permissions in the manifest (double click your extension in the file tree and select android tab)
also it looks like you imported
import android.content.Context;
twice which can cause errors. Also you might have to extend your class's activity import at the start of your class, like this:
Code:
public class testclass extends Activity {
 
Last edited by a moderator:
J

Juhran

Guest
Thank you! I did check all these and found that Lint is causing this not to compile.
Seems as if I'll have to check if the permission is set?

MissingPermission: Missing Permissions
../../src/main/java/com/company/game/testclass.java:51: Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with checkPermission) or explicitly handle a potential SecurityException
48 TelephonyManager tm=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
49
50 //Calling the methods of TelephonyManager the returns the information
51 String IMEINumber=tm.getDeviceId();
52
53

Note: This issue has an associated quickfix operation in Android Studio/IntelliJ
Priority: 9 / 10
Category: Correctness
Severity: Error
Explanation: Missing Permissions.
This check scans through your code and libraries and looks at the APIs being used, and checks this against the set of permissions required to access those APIs. If the code using those APIs is called at runtime, then the program will crash.

Furthermore, for permissions that are revocable (with targetSdkVersion 23), client code must also be prepared to handle the calls throwing an exception if the user rejects the request for permission at runtime.​
 
W

Wraithious

Guest
Did you inject the permission in the manifest?
The other part for asking user for permission can be handled by adding the check/get permissions code into your extension, or you can just download my android permissions extension on the marketplace (it's free) and call it at game start (note that you still must inject the permission(s) needed in the manifest)
 
J

Juhran

Guest
I did add
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
inside the window for "Inject to AndroidManifest.xml Manifest" for "Extension Android Properties".

Which extension is yours in the Marketplace?

I fear a big problem here is I am used to write Code inside Android Studio inside the Activity.
Did declare "public class testclass extends Activity {..." as you mentioned earlier.

Had to handle the Context which caused trouble and placed it derictly at first code
inside the class "private Context context;"

My method for the detecting the imei looks currently like this:



Code:
public String xximei()
{
   String imeinum="huhu";



       if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
                != PackageManager.PERMISSION_GRANTED) {
                         // We do not have this permission. Let's ask the user
 

//    Activity.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, 0);

                         return "imei error";

        }else {
     
   
            TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
            imeinum = tm.getDeviceId();

           return "reached this line;
 
 
       }

}

For some reason I receive "undefined" again and not " reached this line" for the call to xximei.
 
W

Wraithious

Guest
it looks like you need to attatch the RunnerActivity, and you have to use ActivityCompat when requesting it, and first you have to check what os is being used (older os's that don't require permissions checks will crash if you try to check them)

Code:
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        {
            if(ContextCompat.checkSelfPermission(RunnerActivity.CurrentActivity,Manifest.permission.READ_PHONE_STATE)==0)
            ActivityCompat.requestPermissions(RunnerActivity.CurrentActivity, new String[]{Manifest.permission.READ_PHONE_STATE}, Get_Permission);
        }
most likely you need these imports also
Code:
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.content.pm.PackageManager;
import android.Manifest;
import android.os.Build;
import java.util.ArrayList;
import java.util.List;
Not sure if you need the last 2 but definatly the first 5.

But be aware the new google play rules state you must give reason for the permission you're asking for if it's listed as a 'dangerous permission', which adds a lot more code to make it work, that's why I made that extension for permissions. You can find it on the marketplace here.
 
J

Juhran

Guest
I did try out your extension from the marketplace. Awesome & works with Game Make Studio 2 as well.
Now I am able to call the permissions needed by my Project.

I am only stuck with the initial Java code which is working fine in Android Studio, to print the imei:

Code:
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
     
            imeinum = tm.getDeviceId();
         
          return imeinum;
Even with set permission this call fails to return anything beside undefined. I did not believe it would be that hard to get the imei via GMS2 :-/
 
W

Wraithious

Guest
I did try out your extension from the marketplace. Awesome & works with Game Make Studio 2 as well.
Now I am able to call the permissions needed by my Project.

I am only stuck with the initial Java code which is working fine in Android Studio, to print the imei:

Code:
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    
            imeinum = tm.getDeviceId();
        
          return imeinum;
Even with set permission this call fails to return anything beside undefined. I did not believe it would be that hard to get the imei via GMS2 :-/
I figured it out, you need to attach the runner to that object, that line of code should be:
Code:
TelephonyManager tm = (TelephonyManager) RunnerActivity.CurrentActivity.getSystemService(Context.TELEPHONY_SERVICE);
 
Top