使用Contact数据模型来批量插入联系人(上)

使用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">&lt;?xml version="1.0" encoding="utf-8"?&gt;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">&lt;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"> &gt;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">&lt;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"> /&gt;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> &lt;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"> /&gt;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">&lt;Button android:text="@string/add" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt;&lt;/Button&gt;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">&lt;/LinearLayout&gt;</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">&lt;?xml version="1.0" encoding="utf-8"?&gt;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">&lt;resources&gt;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> &lt;string name="hello"&gt;please input the number to add for contact!&lt;/string&gt;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> &lt;string name="app_name"&gt;ContactGenerator&lt;/string&gt;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> &lt;string name="add"&gt;add&lt;/string&gt;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> &lt;string name="insert_title"&gt;insert contact&lt;/string&gt;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px"> &lt;string name="insert_tip"&gt;waiting&lt;/string&gt;</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">&lt;/resources&gt;</span></div> </div> <br style="line-height:25px"></wbr>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值