2016-09-03 5 views
-4

Androidの連絡先リストから連絡先を取得するために、コード内でどのような変更を行うべきかお教えください。私が書かれている論理は、ロリポップのための完璧な作業と以下が、上記のために、それはセキュリティ例外を言ってクラッシュだロリポップます。..連絡先を取得する際にAndroid Marshmallowセキュリティ例外が発生する

+0

実行時に許可を得ていますか? Android M以上の場合は、実行時に権限をリクエストする必要があります。 –

+0

私は、マニフェストの許可とコンテンツプロバイダを使用した取得を依頼しました。 – Ankit

+1

これは、https://developer.android.com/training/permissions/requesting.html –

答えて

1

のAndroid 6.0(APIレベル23)に始まり、ユーザーが権限を付与https://developer.android.com/training/permissions/requesting.html

」を参照してください。アプリの実行中ではなく、アプリをインストールするときではなく、

許可は通常のものと危険なものに分類されます。

次のコードをチェックアプリがユーザーの連絡先の読み取り権限を持っており、必要に応じて権限を要求した場合:

// 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. 
} 
} 
関連する問題