android app crashes after permission disabling on

2019-08-26 07:40发布

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

5条回答
我想做一个坏孩纸
2楼-- · 2019-08-26 07:49

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"
      />
查看更多
3楼-- · 2019-08-26 07:54

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);
查看更多
姐就是有狂的资本
4楼-- · 2019-08-26 08:09

u need to check for permission every time in code you are going to use the camera of memory functions https://developer.android.com/training/permissions/requesting.html

查看更多
地球回转人心会变
5楼-- · 2019-08-26 08:13

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.

查看更多
小情绪 Triste *
6楼-- · 2019-08-26 08:14

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

查看更多
登录 后发表回答