2016-09-03 43 views
-4

任何人都可以請我建議我在我的代碼中進行哪些更改以從Android聯繫人列表中檢索聯繫人。這是我寫的是工作完美的棒棒糖和下方,但對於上述棒棒糖它崩潰說安全例外的邏輯..獲取聯繫人時的Android棉花糖安全異常

+0

您是否獲得在運行時的權限?對於Android M及以上版本,您需要在運行時請求權限。 –

+0

我剛剛要求清單中的權限,並使用內容提供者進行檢索。 – Ankit

+1

這將幫助你https://developer.android.com/training/permissions/requesting.html –

回答

1

請參考https://developer.android.com/training/permissions/requesting.html

「,開始在在Android 6.0(API等級23),用戶授予權限到應用程序運行時應用程序,而不是安裝應用程序時。「

權限被分類爲正常且有危險。

下面的代碼檢查,如果該應用程序有權限讀取用戶的聯繫人,並在必要時請求權限:

// Here, thisActivity is the current activity 
if (ContextCompat.checkSelfPermission(thisActivity, 
      Manifest.permission.READ_CONTACTS) 
    != PackageManager.PERMISSION_GRANTED) { 

// Should we show an explanation? 
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, 
     Manifest.permission.READ_CONTACTS)) { 

    // Show an expanation 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(thisActivity, 
      new String[]{Manifest.permission.READ_CONTACTS}, 
      MY_PERMISSIONS_REQUEST_READ_CONTACTS); 

    // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an 
    // app-defined int constant. The callback method gets the 
    // result of the request. 
} 
}