gpt4 book ai didi

android - 运行时权限异常

转载 作者:行者123 更新时间:2023-11-29 15:40:29 25 4
gpt4 key购买 nike

为询问权限而编写的方法。我正在请求多个权限。

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,但它正在读取联系人并抛出此权限拒绝错误...没有联系人读取行,应用程序正在正确请求权限并且工作正常。可以做什么?

最佳答案

这里没有最小的、完整的和可验证的示例。但是,在 onCreate() 中,如果 checkNetworkAvailability() 返回 true 并且 populateUserData() 返回 true ,您调用了 fetchContacts(),尽管您可能还没有权限。

与 Android 中的许多事情一样,requestPermissions() 是异步的。当您的 requestPermissions() 调用返回时,尚未向用户询问这些权限,而您还没有这些权限。如果您正在请求权限,则在 onRequestPermissionsResult() 之前,您不能根据该权限执行初始化,然后仅当用户授予权限时。

关于android - 运行时权限异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41312865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com