2017-10-18 95 views
0

我在谷歌播放的崩潰報告中出現錯誤,我正在尋找聯繫人列表中的聯繫人,以便知道是否必須創建它或者它是否已存在,但它給我java.lang.SecurityException異常在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"/> 

我正在尋找,但我無法理解這個錯誤,我希望有人能幫助我。

編輯: 這是我在谷歌跟蹤播放

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) 
+1

請編輯問題併發布整個Java堆棧跟蹤。另外,請確保您已設置[運行時權限](https://developer.android.com/training/permissions/requesting.html),因爲「READ_CONTACTS」和「WRITE_CONTACTS」都是「危險」權限。 – CommonsWare

+0

只需要清楚......第一行('java.lang.SecurityException:')中的':'後面沒有任何內容,接下來是'at android.os.Parcel.readException'處?通常,一個'SecurityException'提供了關於到底出錯的更多細節。 – CommonsWare

+0

是的,我只是複製和粘貼就像它 –

回答

0

requestPermissions()是異步的。當它返回時,用戶甚至不會問關於權限,更不用說將任何權限授予您的應用。

你需要安排在兩點做你的聯繫人查找:

  • 如果你已經擁有你打電話contactExists()checkSelfPermission()回報PERMISSION_GRANTED)時間允許,你可以繼續做你的查找

  • 如果您沒有權限,你需要等到onRequestPermissionsResult()被調用後,你確認,你現在有權限