我有一箇舊的項目與目標sdk 21,現在我必須把運行時權限,所以任何人都可以建議我任何最好的和簡單的方法來做到這一點。最好的方式來處理運行時權限android
-1
A
回答
1
您可以在初始屏幕或第一屏幕上處理權限。
如果多個權限然後處理所有的一次。
如果用戶拒絕任何權限,則顯示消息需要一些權限。
希望它能幫助你。
以下是如何處理多個權限的示例。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
if(checkAndRequestPermissions()){
//all permission granted
}else{
//require all permission.
}
}
private boolean checkAndRequestPermissions() {
int camerapermission = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
int phonestate = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);
int location = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
int recordaudio = ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO);
int permissionLocation = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
List<String> listPermissionsNeeded = new ArrayList<>();
if (camerapermission != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.CAMERA);
}
if (phonestate != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.READ_PHONE_STATE);
}
if (location != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (recordaudio != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.RECORD_AUDIO);
}
if (permissionLocation != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_ID_MULTIPLE_PERMISSIONS: {
Map<String, Integer> perms = new HashMap<>();
perms.put(Manifest.permission.CAMERA, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_PHONE_STATE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.RECORD_AUDIO, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, 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.CAMERA) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
) {
Log.d(TAG, "camera & location services permission granted");
// here you can do your logic all Permission Success Call
moveToNxtScreen();
} else {
Log.d(TAG, "Some permissions are not granted ask again ");
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA) || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
showDialogOK("Some Permissions are required for Open Camera",
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.
dialog.dismiss();
break;
}
}
});
} else {
explain("You need to give some mandatory permissions to continue. Do you want to go to app settings?");
}
}
}
}
}
}
private void showDialogOK(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", okListener)
.create()
.show();
}
private void explain(String msg) {
final android.support.v7.app.AlertDialog.Builder dialog = new android.support.v7.app.AlertDialog.Builder(this);
dialog.setMessage(msg)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
Utils.startInstalledAppDetailsActivity(LoginActivity.this);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
dialog.create().dismiss();
finish();
}
});
dialog.show();
}
1
創建兩個變量,如
private static final int REQUEST_CODE_PERMISSION = 1;
String mPermission = Manifest.permission.ACCESS_FINE_LOCATION;
後創建方法類似這樣的
private void getPermission() {
if(Build.VERSION.SDK_INT>= 23) {
if (checkSelfPermission(mPermission) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{mPermission,
},
REQUEST_CODE_PERMISSION);
return;
}
else
{
/* your code if permission already access
}
}
}
覆蓋後onRequestPermissionsResult這樣
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Log.e("Req Code", "" + requestCode);
if (requestCode == REQUEST_CODE_PERMISSION) {
if (grantResults.length == 1 &&
grantResults[0] == MockPackageManager.PERMISSION_GRANTED) {
methodRedirect();
}
else{
this.getPermission();
}
}
}
這樣即使用戶拒絕權限的方法,它會再次顯示。
您可以像這樣管理多個權限。
相關問題
- 1. ASP/ASP.NET最好的方式來處理寫權限?
- 2. 處理Android運行時權限
- 3. 運行時權限處理
- 4. 最好的方法來處理OOME android
- 5. 最好的方式來處理櫥櫃中的列表(android庫)
- 6. 運行時權限和永不問問權限處理
- 7. 使用多模塊項目處理Android M運行時權限
- 8. JAX-WS最好的方式來處理超時
- 9. 同時下載文件 - 最好的方式來處理它
- 10. 哪個更好的方式來定義android的權限?
- 11. 最好的方法來處理NSString Memeory
- 12. 最好的方式來處理改變python類的'模式'
- 13. Magento - 最好的方式來處理塊的形式?
- 14. 最好的方式來花時間在C#中運行決策#
- 15. 春:最好的方式來選擇在運行時
- 16. 處理Windows ACL權限的最佳方式
- 17. 最好的方法來處理401與春天的錯誤android
- 18. 處理運行時方向更改android
- 19. 使用SDK的Android運行時權限
- 20. Android中的運行時權限
- 21. 最好的方式來處理指定數據類型的NAs
- 22. 什麼是最好的方式來處理DBNull的
- 23. 最好的處理方法
- 24. 最好的方式來處理後臺進程
- 25. 最好的方式來處理$ onAuth維護離子/ angularfire
- 26. erlang主管最好的方式來處理ibrowse:send_req conn_failed
- 27. 最好的方式來處理跨服務器php包括
- 28. 什麼是最好的方式來處理多個文件
- 29. 最好的OO方式來處理「取消按鈕」
- 30. 什麼是最好的方式來處理異步請求
Thanx Sachin,但如果用戶拒絕設置後啓動畫面的權限?所以我已經到處檢查。 –
然後你需要檢查每次授予權限或不授權。如果權限被拒絕,則需要簡單的顯示烤麪包權限和開放權限設定畫面。檢查如何打開權限屏幕http://stackoverflow.com/a/32983128/3416642 – Sach