为询问权限而编写的方法。我要求多种权限。
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);
}
}
日志:
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)
尽管我在开始时添加了askForPermission,但它正在阅读联系人并抛出此权限拒绝错误...没有联系人读取行应用程序正在请求权限并且正常工作。 可以做些什么?
答案 0 :(得分:3)
此处没有最小,完整且可验证的示例。但是,在var rowid = $tr.attr("id");
中,如果formatter: "actions"
返回onCreate()
而checkNetworkAvailability()
返回true
,则会调用populateUserData()
,尽管您可能没有许可。
与Android中的许多内容一样,true
是异步的。当您的fetchContacts()
调用返回时,系统未向用户询问这些权限,但您还没有这些权限。如果您要请求权限,则无法在requestPermissions()
之前根据该权限执行初始化,并且只有在用户授予权限的情况下才能执行初始化。
答案 1 :(得分:1)
有同样的问题,通过在清单中添加相同的权限来解决它,即使那些是运行时权限