从后台删除应用程序时,权限已消失

时间:2016-11-10 07:54:30

标签: android android-6.0-marshmallow android-permissions

我要求在Marshmallow及以上设备上运行时对Android应用程序的权限。权限被正确授予,但是一旦我杀死了应用程序(从后台删除而不是强制停止),权限就会消失,当您再次打开应用程序时它会再次请求权限。

boolean granted=checkPermission();
    Log.d("Permission already there","---"+granted );// it always returns false

    if (Build.VERSION.SDK_INT >= 23) {
        System.out.println("Build version is"+Build.VERSION.SDK_INT);
        if(!granted){
            requestPermission();
        }
    }


  private void requestPermission() {

    ActivityCompat.requestPermissions(activity,new String[]{
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.CALL_PHONE,
            Manifest.permission.CAMERA,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_CONTACTS,
            Manifest.permission.GET_ACCOUNTS,

    },RequestPermissionCode);

}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {

    case RequestPermissionCode:

        if (grantResults.length > 0) {

            boolean ACCESS_FINE_LOCATION = grantResults[0] == PackageManager.PERMISSION_GRANTED;
            boolean RECORD_AUDIO = grantResults[1] == PackageManager.PERMISSION_GRANTED;
            boolean CALL_PHONE = grantResults[2] == PackageManager.PERMISSION_GRANTED;
            boolean CAMERA = grantResults[3] == PackageManager.PERMISSION_GRANTED;
            boolean WRITE_EXTERNAL_STORAGE = grantResults[4] == PackageManager.PERMISSION_GRANTED;
            boolean GET_ACCOUNTS = grantResults[5] == PackageManager.PERMISSION_GRANTED;
            boolean READ_CONTACTS = grantResults[6] == PackageManager.PERMISSION_GRANTED;



            if (ACCESS_FINE_LOCATION && RECORD_AUDIO && CALL_PHONE && CAMERA && WRITE_EXTERNAL_STORAGE && GET_ACCOUNTS && READ_CONTACTS) {

                Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_LONG).show();
            }
            else {
                Toast.makeText(MainActivity.this,"Permission Denied",Toast.LENGTH_LONG).show();

            }
        }

        break;
    }
}

public boolean checkPermission() {


    int FirstPermissionResult= ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION);
    int SecondPermissionResult=ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECORD_AUDIO);
    int ThirdPermissionResult=ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE);
    int FourthPermissionResult=ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA);
    int FifthPermissionResult=ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    int SixthPermissionResult=ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.GET_ACCOUNTS);
    int SeventhPermissionResult=ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_CONTACTS);

    return FirstPermissionResult == PackageManager.PERMISSION_GRANTED &&
            SecondPermissionResult == PackageManager.PERMISSION_GRANTED &&
            ThirdPermissionResult == PackageManager.PERMISSION_GRANTED && 
            FourthPermissionResult == PackageManager.PERMISSION_GRANTED &&
            FifthPermissionResult == PackageManager.PERMISSION_GRANTED &&
            SixthPermissionResult == PackageManager.PERMISSION_GRANTED &&
            SeventhPermissionResult == PackageManager.PERMISSION_GRANTED;
}

1 个答案:

答案 0 :(得分:0)

来自PackageManager

中的Android来源和文档
public static final int PERMISSION_DENIED = -1;
public static final int PERMISSION_GRANTED = 0;

并且在您的代码中,您正在添加所有权限的权限结果。因此,即使未授予一个项目权限,您的result变量也会变为-1,并且您的方法返回false;

您的代码的另一个问题是,您要求VIBRATE等内容的权限,请阅读此doc以了解正常权限和危险权限之间的区别。

最后但最重要的问题是谷歌不推荐一次性询问所有运行时权限。此外,App Store还有机会拒绝您的应用,因为我们没有遵循权限指南

编辑: 从列表中删除这些权限,然后重试

Manifest.permission.MODIFY_AUDIO_SETTINGS
Manifest.permission.CAPTURE_AUDIO_OUTPUT
Manifest.permission.CAPTURE_VIDEO_OUTPUT
Manifest.permission.CAPTURE_SECURE_VIDEO_OUTPUT
Manifest.permission.ACCESS_NETWORK_STATE
Manifest.permission.VIBRATE
  

请阅读文档,明确提到您不要run time permissions normal permissions