gpt4 book ai didi

java - 为什么我可以在没有 android.permission.READ_CONTACTS 的情况下检索我的联系人?

转载 作者:太空狗 更新时间:2023-10-29 15:26:49 25 4
gpt4 key购买 nike

我写了一些代码来获取所有联系人,但忘记在 AndroidManifest.xml 中声明权限。

AndroidManifest.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ninja.mobilehelper" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.ninja.mobilehelper.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Java代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Uri contactsUri= ContactsContract.Contacts.CONTENT_URI;
String[] proj1=new String[]{ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER,
ContactsContract.Contacts.LOOKUP_KEY};
Cursor curContacts=getContentResolver().query(contactsUri,proj1, null, null, null);

//declare a ArrayList object to store the data that will present to the user
ArrayList<String> contactsList=new ArrayList<String>();
String allPhoneNo="";
if(curContacts.getCount()>0){
while(curContacts.moveToNext()){
// get all the phone numbers if exist
if(curContacts.getInt(1)>0){
allPhoneNo=getAllPhoneNumbers(curContacts.getString(2));
}
contactsList.add(curContacts.getString(0)+" , "+allPhoneNo);
allPhoneNo="";
}
}

// binding the data to ListView
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, contactsList));
ListView lv=getListView();
lv.setTextFilterEnabled(true);

}

我的手机是 Moto X,Android 4.4.2,使用 IntelliJIdea。

Gradle 构建。Android 构建工具版本为“19.0.3”。

似乎需要 uses-permission android:name="android.permission.READ_CONTACTS",但未经许可就可以工作!为什么?!

有什么建议吗?

补充:是我的错,我打开了之前的应用程序,声明了权限。很伤心,我好像在Android中发现了一个错误- -!!

最佳答案

是的,如果您在代码中授予 uri 权限,它将起作用。

 grantUriPermission(getPackageName(),ContactsContract.Contacts.CONTENT_URI, Intent.FLAG_GRANT_READ_URI_PERMISSION);

关于java - 为什么我可以在没有 android.permission.READ_CONTACTS 的情况下检索我的联系人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22616079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com