我在Google Play中的崩溃报告中有一个错误,我正在联系人列表中搜索联系人,以便知道我是否必须创建它,或者它是否已经存在,但它给我提供了Android 7.0设备上的java.lang.SecurityException。
这是我的代码:
public boolean contactExists() {
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(RegisterActivity.this,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(RegisterActivity.this,
Manifest.permission.READ_CONTACTS)) {
showMessageOKCancel("Necesitas permisos para guardar nuestro contácto en tu teléfono y poder enviarnos mensajes");
return false;
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
if (Bellisima_number != null) {
Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(Bellisima_number));
String[] mPhoneNumberProjection = { ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.NUMBER, ContactsContract.PhoneLookup.DISPLAY_NAME };
Cursor cur = this.getContentResolver().query(lookupUri, mPhoneNumberProjection, null, null, null);
try {
if (cur.moveToFirst()) {
return true;
}
} finally {
if (cur != null)
cur.close();
}
return false;
} else {
return false;
}
}
}
return false;
}// contactExists
我的清单中有联系权限
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
我正在搜索,但是我不能理解这个错误。
这是我在Google Play中的轨迹
java.lang.SecurityException:
at android.os.Parcel.readException (Parcel.java:1683)
at android.os.Parcel.readException (Parcel.java:1636)
at android.app.ActivityManagerProxy.getContentProvider (ActivityManagerNative.java:4263)
at android.app.ActivityThread.acquireProvider (ActivityThread.java:5475)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider (ContextImpl.java:2239)
at android.content.ContentResolver.acquireUnstableProvider (ContentResolver.java:1515)
at android.content.ContentResolver.query (ContentResolver.java:514)
at android.content.ContentResolver.query (ContentResolver.java:472)
at co.bellisima.bellisimaco.RegisterActivity.contactExists (RegisterActivity.java:318)
at co.bellisima.bellisimaco.RegisterActivity.createUser (RegisterActivity.java:209)
at co.bellisima.bellisimaco.RegisterActivity$1.onComplete (RegisterActivity.java:149)
at com.google.android.gms.tasks.zzc$1.run (Unknown Source)
at android.os.Handler.handleCallback (Handler.java:751)
at android.os.Handler.dispatchMessage (Handler.java:95)
at android.os.Looper.loop (Looper.java:154)
at android.app.ActivityThread.main (ActivityThread.java:6123)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:757)
发布于 2017-10-19 08:09:27
requestPermissions()
是异步的。当它返回时,用户甚至没有被询问权限,更不用说授予你的应用程序任何权限了。
您需要安排在两个点执行联系人查找:
contactExists()
(checkSelfPermission()
返回PERMISSION_GRANTED
)时已经拥有权限,则可以继续执行查找onRequestPermissionsResult()
并确认您现在拥有权限https://stackoverflow.com/questions/46820883
复制相似问题