使用Contact数据模型来批量插入联系人主要需要contact数据模型的以下两文件:
com.android.contacts.model下的:
EntitySet.java
EntityDelta.java
以上文件的源码地址可以在http://hi-android.info/src/找到。
EntitySet在Android 4.0中已经被改名为EntityDeltaList.
我自己的代码在MainActivity.java,DialogResolver.java和ContactPersistTask.java。
注意:需要在AndroidManifest.xml加入写contacts的权限。
写contacts的权限为:
<uses-permission android:name="android.permission.READ_CONTACTS" />
读contacts的权限为:
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
文件1
MainActivity.java文件<wbr style="line-height:25px"><div style="line-height:25px"> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">package com.teleca.robin.Contact;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">import android.app.Activity;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">import android.app.Dialog;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">import android.content.Context;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">import android.os.Bundle;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">import android.view.View;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">import android.view.View.OnClickListener;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">import android.widget.Button;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">import android.widget.EditText;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">import android.widget.Toast;</span></div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">public class</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff00ff; line-height:25px">MainActivity</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">extends</span><span style="color:#3366ff; line-height:25px">Activity</span><span style="color:#993300; line-height:25px">implements</span><span style="color:#3366ff; line-height:25px">DialogResolver{</span> </div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"><span style="line-height:25px; white-space:pre"></span>final static String TAG="robin";</span></div> <div style="line-height:25px"><span style="color:#808080; line-height:25px"> /** Called when the activity is first created. */</span></div> <div style="line-height:25px"><span style="color:#808080; line-height:25px"> @Override</span></div> <div style="line-height:25px"> <span style="color:#3366ff; line-height:25px"> </span><span style="color:#993300; line-height:25px">public void onCreate</span><span style="color:#3366ff; line-height:25px">(Bundle savedInstanceState) {</span> </div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> super.onCreate(savedInstanceState);</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> setContentView(R.layout.main);</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> Button button = (Button) findViewById(R.id.Button01);</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> OnClickListener listener = new OnClickListener() {</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> public void onClick(View v) {</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> <span style="line-height:25px; white-space:pre"> </span>doAddAction();</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> }</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> };</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> button.setOnClickListener(listener);</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> <span style="line-height:25px; white-space:pre"> </span>editText=(EditText)findViewById(R.id.editText);</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> <span style="line-height:25px; white-space:pre"> </span>editText.setText("100");</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> }</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> EditText editText;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> void doAddAction() {</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"><br style="line-height:25px"></span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> <span style="line-height:25px; white-space:pre"> </span>String content=editText.getText().toString();</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> <span style="line-height:25px; white-space:pre"> </span>int count=0;</span></div> <div style="line-height:25px"> <span style="color:#3366ff; line-height:25px"> </span><span style="color:#993300; line-height:25px"><span style="line-height:25px; white-space:pre"> </span>try</span><span style="color:#3366ff; line-height:25px">{</span> </div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> <span style="line-height:25px; white-space:pre"> </span>count=Integer.parseInt(content);</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> <span style="line-height:25px; white-space:pre"> </span>}catch(NumberFormatException e)</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> <span style="line-height:25px; white-space:pre"> </span>{</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> <span style="line-height:25px; white-space:pre"> </span>Toast.makeText(this, "please input a Number for Contacts count!",</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> <span style="line-height:25px; white-space:pre"> </span>Toast.LENGTH_SHORT).show();</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> <span style="line-height:25px; white-space:pre"> </span>}</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> final ContactPersistTask task = new ContactPersistTask(this);</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> task.execute(count);</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"><br style="line-height:25px"></span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> }</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> public void showDialog(Dialog dialog)</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> {</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> <span style="line-height:25px; white-space:pre"> </span>dialog.show();</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> }</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> public void dismissDialog(Dialog dialog)</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> {</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> <span style="line-height:25px; white-space:pre"> </span>dialog.dismiss();</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> }</span></div> <div style="line-height:25px"> <span style="color:#3366ff; line-height:25px"> </span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">Activity getActivity(){</span> </div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> <span style="line-height:25px; white-space:pre"> </span>return this;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> }</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span></div> </div> <div style="line-height:25px"><span style="line-height:25px">文件2</span></div> <div style="line-height:25px">DialogResolver.java文件</div> <div style="line-height:25px"> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">package com.teleca.robin.Contact;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"><br style="line-height:25px"></span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">import android.app.Dialog;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"><br style="line-height:25px"></span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">public interface DialogResolver{</span></div> <div style="line-height:25px"><span style="color:#0000ff; line-height:25px">public void showDialog(Dialog dialog);</span></div> <div style="line-height:25px"><span style="color:#0000ff; line-height:25px">public void dismissDialog(Dialog dialog);</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span></div> </div> <div style="line-height:25px">注意:对于任何实现了DialogResolver的类,要求该类必须是Activity。</div> <div style="line-height:25px"><span style="line-height:25px">文件3</span></div> <div style="line-height:25px">布局文件main.xml文件</div> <div style="line-height:25px"> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"><?xml version="1.0" encoding="utf-8"?></span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> android:orientation="vertical"</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> android:layout_width="fill_parent"</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> android:layout_height="fill_parent"</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> ></span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"><TextView </span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> android:layout_width="fill_parent"</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> android:layout_height="wrap_content"</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> android:text="@string/hello"</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> /></span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> <EditText </span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> android:id="@+id/editText"</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> android:layout_width="100dp"</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> android:layout_height="wrap_content"</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> /></span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"><Button android:text="@string/add" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button></span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"></LinearLayout></span></div> </div> <div style="line-height:25px"><span style="line-height:25px">文件4</span></div> <div style="line-height:25px">strings.xml文件</div> <div style="line-height:25px"> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"><?xml version="1.0" encoding="utf-8"?></span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"><resources></span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> <string name="hello">please input the number to add for contact!</string></span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> <string name="app_name">ContactGenerator</string></span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> <string name="add">add</string></span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> <string name="insert_title">insert contact</string></span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> <string name="insert_tip">waiting</string></span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"></resources></span></div> </div> <br style="line-height:25px"></wbr>