我尝试使用以下代码在Android电话簿中存储记录,但失败了。 我发布了我试过的代码,
@Override
public void onClick(View v) {
String nm, num;
nm = t1.getText().toString();
num = t2.getText().toString();
ContentResolver c = getContentResolver();
ContentValues record = new ContentValues();
int iddd = TotalCount + 1;
record.put("Contacts._ID", iddd);
record.put("Contacts.DISPLAY_NAME", nm);
record.put("Contacts.HAS_PHONE_NUMBER", 1);
try {
c.insert(ContactsContract.Contacts.CONTENT_URI, record);
ContentValues record1 = new ContentValues();
record1.put("CommonDataKinds.Phone.NUMBER", num);
c.insert(CommonDataKinds.Phone.CONTENT_URI, record1);
Toast.makeText(this, "Contact saved2", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this, "Failed to save contact", Toast.LENGTH_LONG)
.show();
}
}
This code when executed, gives an error. Failed to insert contact.
Minimum sdk version is 8.
Target sdk is 18.
But don't know why it's causing this error.
答案 0 :(得分:0)
您可以尝试使用此代码存储手机中的联系人...将联系人电话存储在arrayList中,将联系人姓名存储在另一个arrayList中:
=== Contacts.java ===
package com.showcontacts;
import java.util.ArrayList;
import android.app.Activity;
导入android.content.ContentResolver;
import android.database.Cursor;
导入android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
导入android.view.Menu;
导入android.view.View;
public class Contacts extends Activity {
String phoneNumber;
ArrayList<String> phones = new ArrayList<String>();
ArrayList<String> contact_name = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.contacts, menu);
return true;
}
/**** get all contacts ******/
public void getNumber(View v) {
ContentResolver cr = getContentResolver();
String name;
Cursor cursor = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
while (cursor.moveToNext()) {
name = cursor
.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNumber = cursor
.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phones.add(phoneNumber);
contact_name.add(name);// fill the Arraylist
}
cursor.close();// close cursor
for (int i = 0; i < phones.size(); i++) {
Log.d(contact_name.get(i) + i, (phones.get(i)));
}
}
}
** activity_contacts.xml **
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Contacts" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:text="Button"
android:onClick="getNumber"
/>
**别忘了在你的清单中添加权限**
<uses-permission android:name="android.permission.READ_CONTACTS"/>
祝你好运