Reputation: 11
I'm getting below Error on code
The 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 less... (Ctrl+F1)
Inspection info: 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. Issue id: MissingPermission
Code:
@SuppressWarnings( "deprecation" )
void setDeviceImei() {
TelephonyManager telephonyManager = ( TelephonyManager ) getSystemService( Context.TELEPHONY_SERVICE );
Utils.setIMEI( context, String.valueOf( telephonyManager.getDeviceId() ) );
WifiManager wm = ( WifiManager ) getApplicationContext().getSystemService( WIFI_SERVICE );
String ip = Formatter.formatIpAddress( wm.getConnectionInfo().getIpAddress() );
Utils.setIpAddress( context, ip );
int v = 0;
try {
v = context.getPackageManager().getPackageInfo( context.getPackageName(), 0 ).versionCode;
Utils.setVersionCode( context, v );
}
catch( NameNotFoundException e ) {
e.printStackTrace();
}
if( !Utils.isConnectingToInternet( context ) ) {
alertpopup( context, context.getResources().getString( R.string.no_internet ) );
return;
} else {
timer = new Timer();
timer.schedule( new TimerTask() {
public void run() {
callValidateImeiApi();
timer.cancel();
timer = null;
}
}, 500 );
}
}
Error on below line:
Utils.setIMEI( context, String.valueOf( telephonyManager.getDeviceId() ) );
Please assist to clear it!
Upvotes: 0
Views: 11523
Reputation: 1205
The error is generated by
telephonyManager.getDeviceId()
First you need to add in AdroidManifest.xml:
<uses-permission android:name="Manifest.permission.READ_PHONE_STATE" />
Starting with SDK 24, the permission have to be asked at runtime (when application starts), and application will rise a Dialog asking for permissions.
Details are explaind here. https://developer.android.com/training/permissions/requesting
What you should do in your app:
// couple of members to be added in the Activity
//identify for what permission we ask
private static final int WANT_TO_READ_PHONE_STATE =1;
//flag to rememebr if permission granted or not, default is false
private boolean READ_PHONE_STATE_granted = false;
/* ask for permissions */
private void askForPermission(String permission, Integer requestCode) {
if (Build.VERSION.SDK_INT >= 23 &&
ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
//This is called if user has denied the permission before
//In this case I am just asking the permission again
ActivityCompat.requestPermissions(this, new String[]{permission}, requestCode);
} else {
/* if permission was requested in AndroidManifest.xml then it is automatically granted for SDK <=23 */
ActivityCompat.requestPermissions(this, new String[]{permission}, requestCode);
}
} else {
// on
READ_PHONE_STATE_granted = true;
}
}
/* Callback when user agreed/disagreed */
@Override
public void onRequestPermissionsResult (int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == WANT_TO_READ_PHONE_STATE) {
// check only the results
for( int i=0; i< grantResults.length; i++) {
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
READ_PHONE_STATE_granted = true;
}
}
}
}
// ask for permissions somwhere in onCreate
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
askForPermission(Manifest.permission.READ_PHONE_STATE, WANT_TO_READ_PHONE_STATE);
/*... other code */
}
Now when you call getDeviceId, you should check first if the READ_PHONE_STATE_granted == true , otherway you will get a crash of your app.
Upvotes: 1
Reputation: 370
To get device Id you'll have to add below line in manifest
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
For Android 6.0 and above you'll have to explicitly ask for permission before you can use TelephonyManager. Also getDeviceId is deprecated for Android O so use as follows :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//getDeviceId() is Deprecated so for android O we can use getImei() method
return telephonyManager.getImei();
}
else {
return telephonyManager.getDeviceId();
}
Upvotes: 1
Reputation: 321
telephonyManager.getDeviceId() This method was deprecated in API level 26 https://developer.android.com/reference/android/telephony/TelephonyManager#getDeviceId()
Try this instead
@RequiresApi(api = Build.VERSION_CODES.O)
TelephonyManager tm = (TelephonyManager)
getSystemService(this.TELEPHONY_SERVICE);
String imei = tm.getImei();
Also see How to get the device's IMEI/ESN programmatically in android?
Upvotes: 0