Berikut adalah contoh terperinci dengan beberapa permintaan izin: -
Aplikasi ini membutuhkan 2 izin saat startup. SEND_SMS dan ACCESS_FINE_LOCATION (keduanya disebutkan dalam manifes.xml).
Saya menggunakan Support Library v4 yang disiapkan untuk menangani Android pra-Marshmallow sehingga tidak perlu memeriksa versi versi.
Segera setelah aplikasi dimulai, ia meminta beberapa izin bersama. Jika kedua izin diberikan, aliran normal berjalan.
public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(checkAndRequestPermissions()) {
// carry on the normal flow, as the case of permissions granted.
}
}
private boolean checkAndRequestPermissions() {
int permissionSendMessage = ContextCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS);
int locationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
List<String> listPermissionsNeeded = new ArrayList<>();
if (locationPermission != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (permissionSendMessage != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.SEND_SMS);
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}
ContextCompat.checkSelfPermission (), ActivityCompat.requestPermissions (), ActivityCompat.shouldShowRequestPermissionRationale () adalah bagian dari perpustakaan dukungan.
Jika satu atau lebih izin tidak diberikan, ActivityCompat.requestPermissions () akan meminta izin dan kontrol menuju ke metode callback onRequestPermissionsResult ().
Anda harus memeriksa nilai flag shouldShowRequestPermissionRationale () di metode callback onRequestPermissionsResult ().
Hanya ada dua kasus: -
Kasus 1: -Setiap kali pengguna mengklik Tolak izin (termasuk yang pertama kali), itu akan mengembalikan true. Jadi ketika pengguna menolak, kami dapat menunjukkan lebih banyak penjelasan dan terus bertanya lagi
Kasus 2: -Hanya jika pengguna memilih "tidak pernah bertanya lagi" itu akan kembali palsu. Dalam hal ini, kami dapat melanjutkan fungsionalitas terbatas dan memandu pengguna untuk mengaktifkan izin dari pengaturan untuk lebih banyak fungsi, atau kami dapat menyelesaikan pengaturan, jika izinnya sepele untuk aplikasi.
KASUS 1
KASUS-2
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
Log.d(TAG, "Permission callback called-------");
switch (requestCode) {
case REQUEST_ID_MULTIPLE_PERMISSIONS: {
Map<String, Integer> perms = new HashMap<>();
// Initialize the map with both permissions
perms.put(Manifest.permission.SEND_SMS, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_FINE_LOCATION, 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.SEND_SMS) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "sms & location services permission granted");
// process the normal flow
//else any one or both the permissions are not granted
} else {
Log.d(TAG, "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.SEND_SMS) || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
showDialogOK("SMS and Location Services Permission required for this app",
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.
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();
}