1

I am trying support my app android M?I have some permission need to ask explicitly at run time.I am was able access other permissions at run time but "android.permission.READ_PHONE_STATE" causing SecurityException.

Here is my manifest file:

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="23" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- Map related permissions -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!--  -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
Joe
  • 550
  • 2
  • 11
  • 21

4 Answers4

1

In your app's build.gradle

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="23" />

change to

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="22" />

And remove these line from manifest.xml. Then it will work in android M also.

Refer this

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "your.package.name"
        minSdkVersion 14
        targetSdkVersion 22
        // other lines defaultConfig
    }
 // other lines in android
}
Ankit Kumar
  • 3,663
  • 2
  • 26
  • 38
  • ,Thanks, Ya I know It will work in target = 22, But I need target version to be 23. – Joe Mar 04 '16 at 07:32
  • Yes your right..But I want to achieve the same in target 23...Its not possible in 23? – Joe Mar 04 '16 at 07:40
  • this is possible with 23 too. But u need to check permission on each event which required permission. if permission is not granted u need to ask user to give permission.. please refer this http://developer.android.com/training/permissions/requesting.html – Ankit Kumar Mar 04 '16 at 07:41
  • @AnkitKumar will this work even if we are not setting maxsdkversion in manifest file? and only minSdkVersion and targetSdkVersion ? Have you tested? – Sreehari Mar 04 '16 at 07:44
  • @Stallion : Yeah, it will definitely work. I have implemented it in one of my apps.. – Ankit Kumar Mar 04 '16 at 07:46
1

For Android 23 or higher, you need to ask explicitly to your users if they want to give you a permission. Two ways to handle this: Set your Max SDK version to be 22 (or lower), or ask the user in the run time in addition to specifying in the manifest file.

More detailed information on how to ask permissions at run time can be found at Working with System Permissions. "Requesting Permissions at Run Time" session from the first the link explains how to do this in your codes in great details.

That being said, try to do some searches before you post a question. Duplicate questions are not encouraged.

Other links that may help you: similar question on StackOverFlow

Community
  • 1
  • 1
Matt
  • 1,424
  • 1
  • 13
  • 17
0

You need to request for the permission when needed, on M devices and handle if the permission is not granted.You can refer to android documentation here

Ravi Theja
  • 3,371
  • 1
  • 22
  • 34
0

You need requestPermissions, here's some example

private final static int REQUEST_CODE_ASK_PERMISSIONS = 1002;

@Override
protected void onStart() {
    super.onStart();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

        int res = checkSelfPermission(Manifest.permission.READ_PHONE_STATE);
        if (res != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE},
                    REQUEST_CODE_OVERLAY_PERMISSION);
        }

    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
        @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case REQUEST_CODE_ASK_PERMISSIONS:
            if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(MainActivity.this, "READ_PHONE_STATE Denied", Toast.LENGTH_SHORT)
                        .show();
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

For more details, please see my code on GitHub

tianyu
  • 179
  • 2
  • 6