简体   繁体   中英

android app crashes after permission disabling on API 23

My app uses permissions

in app i'm asking runtime permission and it works fine

BUT when doing this steps

 1) using device home button going to current app settings page

 2) switching off permission

 3)then going back to app again with home button

it crashes without any exception

for exmaple INSTAGRAM app

1) open instagram 

2) goto settings page

3) switch permission 

4)return to app

it restarts, i want to do something like that

To use CAMERA you have to check the permission at runtime.

All the info are here.

First check if the user grant the permission:

if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA)
    == PackageManager.PERMISSION_DENIED)

Then, you could use this to request to the user:

ActivityCompat.requestPermissions(activity, new String[] {Manifest.permission.CAMERA}, requestCode);

您每次需要使用存储功能摄像头的代码中都需要检查许可权https://developer.android.com/training/permissions/requesting.html

try this your need to ask runtime permission because Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.

ask runtime permission using below code

String permission = Manifest.permission.permission.CAMERA;
int grant = ContextCompat.checkSelfPermission(this, permission);
if (grant != PackageManager.PERMISSION_GRANTED) {
    String[] permission_list = new String[1];
    permission_list[0] = permission;
    ActivityCompat.requestPermissions(this, permission_list, 1);
}

and than handle result like this

 @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == 1) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(AccountClass.this,"permission granted", Toast.LENGTH_SHORT).show();  
             // perform your action here

        } else {
            Toast.makeText(AccountClass.this,"permission not granted", Toast.LENGTH_SHORT).show();
        }
    }

}

read about runtime permission

Do one thing. Check for runtime permission every time you're trying to open the camera and in onRequestPermissionsResult() handle it properly if permission is denied. Do the same for memory permission also.

you can use method

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // code here
}

in Manifest add android:configChanges

<activity
       android:name=".ui.edit.EditActivity"
       android:configChanges="orientation|screenSize|locale|layoutDirection"
      />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM