本文介绍了尝试编写和更新联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试编写并使用URI content://com.android.contacts/contacts更新联系人,但出现运行时错误:Uid 10101无权访问uri content://com.android.contacts /联系人。我在清单文件中声明了权限。
I am trying to write and then to update contacts using URI content://com.android.contacts/contacts and I get a RunTime Error: Uid 10101 does not have permission to uri content://com.android.contacts/contacts. I declared permission in the Manifest file.
这里是代码
public class MainActivity extends Activity {
protected String[] names = {"Anda", "Candy", "Dandy"};
protected String[] numbers = {"12345678900","14035567890","12392344556"};
protected int[] ids = {1,2,3};
TextView txt;
ListView list;
ContentValues cv;
Uri u;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView)findViewById(R.id.txt);
list = (ListView) findViewById(R.id.list);
Uri uri = getTheUri();
grantUriPermission("ContactsContract.CommonDataKinds.Phone.CONTENT_URI", uri, 2);
insertContacts(names, numbers, ids);
showContacts();
updateContact();
showContacts();
}
public static Uri getTheUri() {
//return Uri.parse("content://com.android.contacts/data/phones");
return Uri.parse("content://com.android.contacts/contacts");
//return Uri.parse("content://contacts/people");
}
protected void insertContacts(String[] names, String[] numbers, int[] ids) {
ContentValues cv=new ContentValues();
this.grantUriPermission("com.example.contactsdemo",Uri.parse("content://com.android.contacts/contacts"), MODE_WORLD_WRITEABLE);
for (int i = 0; i < names.length; i++) {
cv.put(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,names[i]);
cv.put(ContactsContract.CommonDataKinds.Phone.NUMBER,numbers[i]);
cv.put(ContactsContract.CommonDataKinds.Phone._ID,ids[i]);
u = getContentResolver().insert(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, cv);
}
}
protected void updateContact() {
//update phone number
getContentResolver().update(u, cv, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + "=Anda", new String[]{"18585541315"});
}
protected void showContacts() {
String [] projection=new String[] {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone._ID};
int[] to = new int[] { R.id.txtName, R.id.txtNumber };
txt.setText(ContactsContract.PhoneLookup.CONTENT_FILTER_URI.toString());
Uri contacts = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
Cursor managedCursor = managedQuery(contacts,projection,null,null,null);
//Cursor managedCursor =cr.query(contacts, projection, null, null, null);
ListAdapter sca=new SimpleCursorAdapter(this,R.layout.list_entry,managedCursor,projection,to);
list.setAdapter(sca);
}
}
并显示清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.contactsdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-spermission
android:name="android.permission.WRITE_CONTACTS" />
<uses-permission
android:name="android.permission.READ_CONTACTS" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:debuggable="true" >
<activity
android:name="com.example.contactsdemo.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>
如何摆脱运行时错误?
How do I get rid of the RunTime Error? Any help would be greatly appreciated.
推荐答案
您正在使用spermission,这并不意味着尝试更改清单中的权限
you are using spermission that is not meaning try to change the permission in your menifest
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>
可以在
这篇关于尝试编写和更新联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!