アクセス権の問い合わせ用に記述されたメソッド。私は複数の許可を求めています。実行時アクセス許可の例外
private void askForPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int contactsPermission = checkSelfPermission(Manifest.permission.READ_CONTACTS);
int extStoragePermission = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
int cameraPermission = checkSelfPermission(Manifest.permission.CAMERA);
int recordPermission = checkSelfPermission(Manifest.permission.RECORD_AUDIO);
int internetPermission = checkSelfPermission(Manifest.permission.INTERNET);
Log.e("Start_Permission1",""+(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M));
Log.e("Start_Permission2",""+(contactsPermission != PackageManager.PERMISSION_GRANTED));
Log.e("Start_Permission3",""+(extStoragePermission != PackageManager.PERMISSION_GRANTED));
Log.e("Start_Permission4",""+(cameraPermission != PackageManager.PERMISSION_GRANTED));
Log.e("Start_Permission5",""+(recordPermission != PackageManager.PERMISSION_GRANTED));
Log.e("Start_Permission6",""+(internetPermission != PackageManager.PERMISSION_GRANTED));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && contactsPermission != PackageManager.PERMISSION_GRANTED && extStoragePermission != PackageManager.PERMISSION_GRANTED && cameraPermission != PackageManager.PERMISSION_GRANTED && recordPermission != PackageManager.PERMISSION_GRANTED) {
Log.e("Start","here i am");
requestPermissions(new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.RECORD_AUDIO, Manifest.permission.READ_CONTACTS}, REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
}
Log.e("Start_permission","Asking done..");
}
In onCreate method:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
// First take all permissions
askForPermissions();
if (!checkNetworkAvailability()) {
Toast.makeText(getApplicationContext(), "Sorry! You need Internet Connection", Toast.LENGTH_LONG).show();
spinner.setVisibility(View.INVISIBLE);
} else {
if (populateUserData()) {
spinner = (ProgressBar) findViewById(R.id.spinner);
spinner.setVisibility(ProgressBar.VISIBLE);
Log.e("Start_populateUSrData", "User data populated");
fetchContacts();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
//insertDummyContact();
} else {
// Permission Denied
Toast.makeText(Start.this, "WRITE_CONTACTS Denied", Toast.LENGTH_SHORT)
.show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
ログ:私はそれが連絡先を読み、この権限拒否エラーを投げている初めにaskForPermissionを追加したという事実の
12-24 16:53:26.251 1465-1465/com.moodoff E/Minikin: addFont failed to create font /system/fonts/NanumGothic.ttf
12-24 16:53:26.252 1465-1465/com.moodoff E/Minikin: addFont failed to create font /system/fonts/DroidSansFallback.ttf
12-24 16:53:26.252 1465-1465/com.moodoff E/Minikin: addFont failed to create font /system/fonts/MTLmr3m.ttf
12-24 16:53:26.434 1465-1465/com.moodoff E/MultiWindowProxy: getServiceInstance failed!
12-24 16:53:26.595 1465-1465/com.moodoff E/Start_Permission1: true
12-24 16:53:26.595 1465-1465/com.moodoff E/Start_Permission2: true
12-24 16:53:26.595 1465-1465/com.moodoff E/Start_Permission3: true
12-24 16:53:26.595 1465-1465/com.moodoff E/Start_Permission4: true
12-24 16:53:26.595 1465-1465/com.moodoff E/Start_Permission5: true
12-24 16:53:26.596 1465-1465/com.moodoff E/Start_Permission6: false
12-24 16:53:26.596 1465-1465/com.moodoff E/Start: here i am
12-24 16:53:26.618 1465-1465/com.moodoff E/Start_permission: Asking done..
12-24 16:53:26.876 1465-1572/com.moodoff E/AndroidRuntime: FATAL EXCEPTION: Thread-1132
Process: com.moodoff, PID: 1465
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{292d428 1465:com.moodoff/u0a207} (pid=1465, uid=10207) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
at android.os.Parcel.readException(Parcel.java:1599)
at android.os.Parcel.readException(Parcel.java:1552)
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3847)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:5062)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2026)
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1517)
at android.content.ContentResolver.query(ContentResolver.java:482)
at android.content.ContentResolver.query(ContentResolver.java:441)
at com.moodoff.ContactList.getContactNames(ContactList.java:135)
at com.moodoff.helper.DBHelper$1.run(DBHelper.java:73)
at java.lang.Thread.run(Thread.java:818)
Inspite ...連絡先は読まずにアプリが求めている行許可が適切に働き、正常に動作します。 何ができますか?
たhttp://stackoverflow.com/questions/34040355/how-to-check-the-multiple-これを参照してください。許可のシングルリクエストでのアンドロイド-m – sukumar