デバイス上の連絡先にアクセスして後でリストに表示しようとしていますが、カーソルで連絡先にアクセスしようとすると、READ_CONTACTSのアクセス許可がないというエラーが表示されますが、AndroidManifestで既にアクセス許可を付与しています.xml
連絡先にアクセスするコード:
public void setupCursor() {
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(this, name + ": " + number, Toast.LENGTH_SHORT).show();
}
cursor.close();
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:Android="http://schemas.Android.com/apk/res/Android"
package="io.github.brady131313.textback" >
<uses-sdk Android:minSdkVersion="15" Android:targetSdkVersion="21"/>
<uses-permission Android:name="Android.PERMISSION.READ_CONTACTS"/>
<application
Android:allowBackup="true"
Android:icon="@mipmap/ic_launcher"
Android:label="@string/app_name"
Android:theme="@style/AppTheme" >
<activity
Android:name=".ActivityMainSettings"
Android:label="@string/app_name" >
<intent-filter>
<action Android:name="Android.intent.action.MAIN" />
<category Android:name="Android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
Android:name=".ActivityContactSelect"
Android:label="Select Contacts">
</activity>
</application>
</manifest>
エラーログ
05-17 17:12:41.544 20202-20202/io.github.brady131313.textback E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: io.github.brady131313.textback, PID: 20202
Java.lang.RuntimeException: Unable to start activity ComponentInfo{io.github.brady131313.textback/io.github.brady131313.textback.ActivityContactSelect}: Java.lang.SecurityException: Permission Denial: opening provider com.Android.providers.contacts.ContactsProvider2 from ProcessRecord{14c069a9 20202:io.github.brady131313.textback/u0a90} (pid=20202, uid=10090) requires Android.permission.READ_CONTACTS or Android.permission.WRITE_CONTACTS
at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2661)
at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2726)
at Android.app.ActivityThread.access$900(ActivityThread.Java:172)
at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1421)
at Android.os.Handler.dispatchMessage(Handler.Java:102)
at Android.os.Looper.loop(Looper.Java:145)
at Android.app.ActivityThread.main(ActivityThread.Java:5835)
at Java.lang.reflect.Method.invoke(Native Method)
at Java.lang.reflect.Method.invoke(Method.Java:372)
at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:1388)
at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:1183)
Caused by: Java.lang.SecurityException: Permission Denial: opening provider com.Android.providers.contacts.ContactsProvider2 from ProcessRecord{14c069a9 20202:io.github.brady131313.textback/u0a90} (pid=20202, uid=10090) requires Android.permission.READ_CONTACTS or Android.permission.WRITE_CONTACTS
at Android.os.Parcel.readException(Parcel.Java:1540)
at Android.os.Parcel.readException(Parcel.Java:1493)
at Android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.Java:3582)
at Android.app.ActivityThread.acquireProvider(ActivityThread.Java:5081)
at Android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.Java:2926)
at Android.content.ContentResolver.acquireUnstableProvider(ContentResolver.Java:1452)
at Android.content.ContentResolver.query(ContentResolver.Java:468)
at Android.content.ContentResolver.query(ContentResolver.Java:428)
at io.github.brady131313.textback.ActivityContactSelect.setupCursor(ActivityContactSelect.Java:46)
at io.github.brady131313.textback.ActivityContactSelect.onCreate(ActivityContactSelect.Java:32)
at Android.app.Activity.performCreate(Activity.Java:6221)
at Android.app.Instrumentation.callActivityOnCreate(Instrumentation.Java:1119)
at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2614)
at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2726)
at Android.app.ActivityThread.access$900(ActivityThread.Java:172)
at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1421)
at Android.os.Handler.dispatchMessage(Handler.Java:102)
at Android.os.Looper.loop(Looper.Java:145)
at Android.app.ActivityThread.main(ActivityThread.Java:5835)
at Java.lang.reflect.Method.invoke(Native Method)
at Java.lang.reflect.Method.invoke(Method.Java:372)
at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:1388)
at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:1183)
どんな助けでも大歓迎です!
アプリケーションタグの外側にuses-permissionを追加し、Android.permissionを小文字にします。コメントで以下の合意に従って修正する必要があります
新しい ランタイム権限モデル がAndroid 6.0で導入されたため、
ランタイムパーミッションモデル を使用する場合は、このコードが役立つことを願っています。また、詳細な実装を確認し、 ここ からソースコードを取得することもできます。
import Android.Manifest;
import Android.content.ContentResolver;
import Android.content.pm.PackageManager;
import Android.database.Cursor;
import Android.os.Build;
import Android.provider.ContactsContract;
import Android.support.v7.app.AppCompatActivity;
import Android.os.Bundle;
import Android.util.Log;
import Android.widget.ArrayAdapter;
import Android.widget.ListView;
import Android.widget.Toast;
import Java.util.ArrayList;
import Java.util.List;
public class MainActivity extends AppCompatActivity {
// The ListView
private ListView lstNames;
// Request code for READ_CONTACTS. It can be any number > 0.
private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the list view
this.lstNames = (ListView) findViewById(R.id.lstNames);
// Read and show the contacts
showContacts();
}
/**
* Show the contacts in the ListView.
*/
private void showContacts() {
// Check the SDK version and whether the permission is already granted or not.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, PERMISSIONS_REQUEST_READ_CONTACTS);
//After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method
} else {
// Android version is lesser than 6.0 or the permission is already granted.
List<String> contacts = getContactNames();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, Android.R.layout.simple_list_item_1, contacts);
lstNames.setAdapter(adapter);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission is granted
showContacts();
} else {
Toast.makeText(this, "Until you grant the permission, we canot display the names", Toast.LENGTH_SHORT).show();
}
}
}
/**
* Read the name of all the contacts.
*
* @return a list of names.
*/
private List<String> getContactNames() {
List<String> contacts = new ArrayList<>();
// Get the ContentResolver
ContentResolver cr = getContentResolver();
// Get the Cursor of all the contacts
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
// Move the cursor to first. Also check whether the cursor is empty or not.
if (cursor.moveToFirst()) {
// Iterate through the cursor
do {
// Get the contacts name
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
contacts.add(name);
} while (cursor.moveToNext());
}
// Close the curosor
cursor.close();
return contacts;
}
}
私はあなたが持っているのと同じ状況を持っていました、基本的にあなたはあなたの許可の前半のために「Android.permission.READ_CONTACTS」を小文字に変更する必要があるだけです。