2017-06-16 71 views
-1

這是我的代碼獲取權限...但仍然停止當用戶執行權限操作(目標API是25)。任何其他方式?應用程序不工作在Android nougat

boolean isMdevice; 
boolean pstatus; 
int code=1; 
String[] perms = { android.Manifest.permission.RECEIVE_SMS, Manifest.permission.CALL_PHONE, Manifest.permission.SEND_SMS}; 


/////then in oncreate 

isMdevice=isMarshmallowPlusDevice(); 
pstatus= isPermissionRequestRequired(MainActivity.this, perms, code); 

////then methods 

public static boolean isMarshmallowPlusDevice() { 

    return Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1; 
} 

@TargetApi(Build.VERSION_CODES.M) 
public static boolean isPermissionRequestRequired(Activity activity, @NonNull String[] permissions, int requestCode) { 
    if (isMarshmallowPlusDevice() && permissions.length > 0) { 
     List<String> newPermissionList = new ArrayList<>(); 
     for (String permission : permissions) { 
      if (PERMISSION_GRANTED != activity.checkSelfPermission(permission)) { 
       newPermissionList.add(permission); 

      } 
     } 
     if (newPermissionList.size() > 0) { 
      activity.requestPermissions(newPermissionList.toArray(new String[newPermissionList.size()]), requestCode); 
      return true; 
     } 

    } 

    return false; 
} 

回答

0

嘗試檢查許可的代碼,並要求運行權限: 這個類添加到您的項目

public class PermissionUtils { 

public static boolean checkPermission(Context context, String permission) { 
    return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED; 
} 

public static void requestPermissions(Activity activity, String messageInCaseOfCustomDialog, int permissionId, String... permissions) { 
    /* if (o instanceof Fragment) { 
     FragmentCompat.requestPermissions((Fragment) o, permissions, permissionId); 
    } else */ 
    if (ActivityCompat.shouldShowRequestPermissionRationale(activity, 
      permissions[0])) { 
     showCustomDialogForAskPermission(activity, messageInCaseOfCustomDialog, permissionId, permissions); 
     // Show an explanation to the user *asynchronously* -- don't block 
     // this thread waiting for the user's response! After the user 
     // sees the explanation, try again to request the permission. 

    } else if (/*MySharedPreference.getSharedPrefrence(activity).getBoolean(permissions[0], false)*/) { //Use your Shared Preference 
     // No explanation needed, we can request the permission. 
     showCustomDialogForGoToTheSettings(activity, messageInCaseOfCustomDialog); 
     // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an 
     // app-defined int constant. The callback method gets the 
     // result of the request. 
    } else { 
     ActivityCompat.requestPermissions(activity, permissions, permissionId); 
    } 
    /*MySharedPreference.getSharedPrefrence(activity).edit().putBoolean(permissions[0], true).commit();*/ //Use your Shared Preference 
} 

private static void showCustomDialogForGoToTheSettings(final Activity activity, String message) { 
    AlertDialog.Builder dialog = new AlertDialog.Builder(activity); 
    dialog.setTitle("Permission Required"); 
    dialog.setMessage(message); 
    dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      Intent intent = new Intent(); 
      intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); 
      Uri uri = Uri.fromParts("package", activity.getPackageName(), null); 
      intent.setData(uri); 
      activity.startActivity(intent); 
     } 
    }); 
    dialog.show(); 
} 

private static void showCustomDialogForAskPermission(final Activity activity, String message, final int permissionId, final String... permissions) { 
    AlertDialog.Builder dialog = new AlertDialog.Builder(activity); 
    dialog.setTitle("Permission Required"); 
    dialog.setMessage(message); 
    dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      ActivityCompat.requestPermissions(activity, permissions, permissionId); 

     } 
    }); 
    dialog.show(); 
} 

}

現在你可以直接檢查,如果你有這個權限或不,假設你想檢查READ_EXTERNAL_STORAGE權限

if (PermissionUtils.checkPermission(RegisterScreenActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)) { 
        //Do your work 
} else {PermissionUtils.requestPermissions(RegisterScreenActivity.this, "You need to provide storage permission for this service", 101, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE});//ask for permission 
} 

您還可以跟蹤其onRequestPermissionsResult結果()之類

@Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    switch (requestCode) { 


     case 101: 
      if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       //permission granted, can do your work 
      } else { 
       //permission not granted 
      } 
      break; 


    } 
} 
+0

感謝回覆方法....我試圖這樣做....在牛軋糖??這項工作...和U可以請告訴我我是錯的.. –

+0

是的,它肯定會工作,我也使用這個,它在所有Android版本 –

+0

罰款工作已經有一個簡單的方法給予檢查權限,即ContextCompat.checkSelfPermission(context,permission)== PackageManager.PERMISSION_GRANTED;它返回true就是app已經有權限,否則返回false。那麼你爲什麼要編寫複雜的代碼來檢查權限 –

相關問題