我有一個關於android 6權限的問題。如果用戶拒絕授予權限,則用戶無法在我的應用中使用某些功能。我如何使用代碼來指導用戶在系統設置應用程序中的應用程序權限設置,以便用戶可以打開此權限。我沒有找到相關的方法。你能幫我解答嗎? 謝謝
-1
A
回答
0
在棉花糖你應該請求權限。 此鏈接可以幫助你。
https://stackoverflow.com/a/33666161/5577853
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
Log.d("", "Permission callback called-------");
switch (requestCode) {
case REQUEST_ID_MULTIPLE_PERMISSIONS: {
Map<String, Integer> perms = new HashMap<String, Integer>();
// Initialize the map with both permissions
perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.CALL_PHONE, PackageManager.PERMISSION_GRANTED);
// Fill with actual results from user
if (grantResults.length > 0) {
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for both permissions
if (perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
Log.d("", " permission granted");
// process the normal flow
//else any one or both the permissions are not granted
} else {
Log.d("", "Some permissions are not granted ask again ");
//permission is denied (this is the first time, when "never ask again" is not checked) so ask again explaining the usage of permission
// shouldShowRequestPermissionRationale will return true
//show the dialog or snackbar saying its necessary and try again otherwise proceed with setup.
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)
|| ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) {
showDialogOK("This app needs something something, please grant permissions",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
checkAndRequestPermissions();
break;
case DialogInterface.BUTTON_NEGATIVE:
// proceed with logic by disabling the related features or quit the app.
Toast.makeText(getApplicationContext(), "Please go to settings and enable permissions.", 2000).show();
break;
}
}
});
}
//permission is denied (and never ask again is checked)
//shouldShowRequestPermissionRationale will return false
else {
Toast.makeText(this, "Go to settings and enable permissions", Toast.LENGTH_LONG)
.show();
// //proceed with logic by disabling the related features or quit the app.
}
}
}
}
}
}
private void showDialogOK(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", okListener)
.create()
.show();
}
private boolean checkAndRequestPermissions() {
int permissionWriteExternal = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
int permissionCallPhone = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);
List<String> listPermissionsNeeded = new ArrayList<String>();
if (locationPermission != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
}
if (permissionCallPhone != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.CALL_PHONE);
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}
0
試試下面的代碼。
public static void mOpenActivity(Context context) {
if (context == null) {
return;
}
final Intent i = new Intent();
i.setAction(Settings.ACTION_APPLICATION_DETAILS_ SETTINGS);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setData(Uri.parse("package:" + context.getPackageName()));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(i);
}
+0
@ mmm2006檢查這個是否工作 –
+0
謝謝。我會嘗試。 – mmm2006
相關問題
- 1. 如何將系統權限授予我的Android應用程序?
- 2. Android系統應用程序權限
- 3. 如何寫權限,系統/應用程序在Android中
- 4. 在Android中如何使用intent打開系統內置的gallery應用程序?
- 5. 如何在Swift中先請求權限打開應用程序?
- 6. 如何從我的應用程序中打開設置應用程序?
- 7. 格蘭特\系統\等寫權限的應用程序[打開失敗:EROFS]
- 8. 如何使用系統權限編譯Android應用程序
- 9. 如何在其他應用程序中使用Liferay的權限系統?
- 10. 在我的應用程序中設置分類系統
- 11. 爲Android Studio應用程序設置不同的用戶權限
- 12. 如何限制快速應用程序中的用戶權限?
- 13. 如何在應用程序打開時更改應用程序設置(設置)?
- 14. 在設置應用程序中打開應用程序的通知設置
- 15. 如何在android中獲取應用程序權限設置?
- 16. Rails應用程序中的動態角色和權限系統
- 17. 權限僅授予系統的應用程序,在清單
- 18. 從應用程序中打開系統設置的具體部分
- 19. 如何從我自己的應用程序打開iPhone設置?
- 20. 如何在設置中打開您的應用程序iOS 11
- 21. 創建一個新的多租戶應用程序授權和權限系統
- 22. 如何在測驗應用程序中設置評分系統?
- 23. 如何在android應用程序中設置DEFAULT系統字體?
- 24. 更改應用程序中設置更改的系統設置
- 25. 如何更新我的應用程序/系統/應用程序
- 26. /系統文件的用戶權限
- 27. 在OnCreate之前設置系統權限
- 28. 如何重置我的應用程序的權限?
- 29. 如何設置Office 365 Unified API的應用程序權限?
- 30. 在Firefox中開發特權應用程序操作系統
您可以再次請求獲取所需權限。 –
如果用戶拒絕並選擇永不問,我根本無法獲得權限對話框。所以我想引導用戶自己設置並打開它。我認爲最好只通知用戶他沒有權限。 – mmm2006
@ mmm2006嘗試以上代碼 –