Reputation: 1641
I am trying to implement runtime permission asking from the user using my app for the first time. Here is the code:
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
if ((ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) &&
(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)) {
new masterdb(getApplicationContext());
new checkRegistration().execute();
} else {
}
} else {
//initialize database
}
But my app crashes with application not responding. The logcat shows something like this :
Could not find method android.app.Activity.checkSelfPermission, referenced from method myPackageName.Lib.hasPermission 09-16 11:46:54.969 3763-3763/myPackageName W/dalvikvm: VFY: unable to resolve virtual method 170: Landroid/app/Activity;.checkSelfPermission (Ljava/lang/String;)I
The thing here is , this code shouldn't even run (checking for permission) in android Kitkat. But still I get this log. and when I move my code out of the if block of sdk version check it runs fine.
Upvotes: 2
Views: 3901
Reputation: 116
Please try use this code
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
for (String permission : permissions) {
int phoneStatePermission = Context.checkSelfPermission(permission);
if (phoneStatePermission != PackageManager.PERMISSION_GRANTED) {
}
}
}
Upvotes: 1