android2。3.3中导入联系人如何去掉重复

a397927829 2012-10-15 02:57:17
package com.lzp.test1;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.heavenlake.ecommerce.Contact;
import com.heavenlake.rbac.EUser;

import android.R.string;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Nickname;
import android.provider.ContactsContract.CommonDataKinds.Note;
import android.provider.ContactsContract.CommonDataKinds.Organization;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.Photo;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.PhoneLookup;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Entity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.SimpleAdapter;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;

public class lixi<myAdapters> extends Activity {
List li = new ArrayList();
String mimetype = "";
private ArrayList<Map<String, Object>> list;
private String mString = "";
private ArrayList<Map<String, Object>> mContactsName = new ArrayList<Map<String, Object>>();
/** 联系人头像 **/
private ArrayList<String> mContactsNumber = new ArrayList<String>();
ListView mListView = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button but = (Button) findViewById(R.id.lixi);

// 这句话放到软件启动的位置,是用来初始化数据库的
try {
EUser.initLocalConn(this);

} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}

public void test(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.lixi:

dialog2();

break;

}
}

private void getContact() throws SQLException, Exception {
String string = "";
String strName = null;
String strPhoneNumber = null;
ContentResolver cr = getContentResolver();

Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);

while (cursor.moveToNext()) {

// 设置相关属性.
Contact contact = new Contact();
int nameFieldColumnIndex = cursor
.getColumnIndex(PhoneLookup.DISPLAY_NAME);
strName = cursor.getString(nameFieldColumnIndex);

// 保存到数据库
contact.setName(strName);

String contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID)); // 获取联系人的ID号,在SQLite中的数据库ID
Cursor phone = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "
+ contactId, null, null);
int ks = 0;
while (phone.moveToNext()) {
String phoneNumber = phone
.getString(phone
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int phoneType = phone
.getInt(phone
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) {

string = phoneNumber;
contact.setMobile(string);


contact.store(EUser.getCurrentUser());


}
//家庭电话
else if (phoneType == Phone.TYPE_HOME) {
string = phoneNumber;
contact.setTelHome(string);
contact.store(EUser.getCurrentUser());

}

// 单位电话
else if (phoneType == Phone.TYPE_WORK) {
string = phoneNumber;
System.out.println(string + "8");
contact.setTel(string);

contact.store(EUser.getCurrentUser());

}


}
// 获取该联系人邮箱
Cursor emails = getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null);
if (emails.moveToFirst()) {
do {
// 遍历所有的Email
String emailValue = emails.getString(emails
.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
contact.setEmail(emailValue);
contact.store(EUser.getCurrentUser());
} while (emails.moveToNext());
}

// 获取该联系人地址
Cursor address = getContentResolver().query(
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null);
if (address.moveToFirst()) {
do {
// 遍历所有的地址 street接到 city城市 region地区 postCode邮政
String street = address.getString(address
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
String city = address.getString(address
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
String region = address.getString(address
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
String postCode = address.getString(address
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
String formatAddress = address.getString(address
.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
contact.setAddress(city+region+street+postCode+formatAddress);
contact.store(EUser.getCurrentUser());
} while (address.moveToNext());
}

// 获取备注信息
Cursor notes = getContentResolver().query(
Data.CONTENT_URI,
new String[] { Data._ID, Note.NOTE },
Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"
+ Note.CONTENT_ITEM_TYPE + "'",
new String[] { contactId }, null);
if (notes.moveToFirst()) {
do {
String noteinfo = notes.getString(notes
.getColumnIndex(Note.NOTE));
contact.setComment(noteinfo);
contact.store(EUser.getCurrentUser());

} while (notes.moveToNext());
}


phone.close();
}

cursor.close();

}
public void dialog2() {

AlertDialog dialog = new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher).setTitle("导入联系人")
.setMessage("是否导入联系人?")

.setPositiveButton("导入手机中联系人", new OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

try {
getContact();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}).setNegativeButton("导入Sim卡中联系人", new OnClickListener() {

public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
try {
getContactSimka();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}).create();
dialog.show();

}
}
<LinearLayout 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:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="联系人:" />

<Button
android:id="@+id/lixi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="导入"
android:layout_marginLeft="227dp"
android:onClick="test" />

</LinearLayout>

<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>
...全文
196 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

80,371

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧