尝试编写和更新联系人

时间:2013-06-22 06:06:41

标签: android contacts

我正在尝试编写然后使用URI内容更新联系人://com.android.contacts/contacts我得到一个RunTime错误:Uid 10101没有uri内容的权限://com.android.contacts /联系人。我在Manifest文件中声明了权限。

这是代码

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>

如何摆脱RunTime错误?任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:2)

您使用的spermission并不意味着尝试更改您的清单中的权限

<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>

可以在Android reference pages.

上找到完整的权限标记集

答案 1 :(得分:0)

检查您的MANIFEST文件..

尝试像这样添加..

<uses-permission
                android:name="android.permission.WRITE_CONTACTS" />

使用权限不使用-spermission ...

希望这会有所帮助......

答案 2 :(得分:0)

看看你的许可:

<uses-spermission
                android:name="android.permission.WRITE_CONTACTS" />

其拼写错误:在s字词之前移除permission

<uses-permission
                android:name="android.permission.WRITE_CONTACTS" />