之前做了一个基于android的通讯录,借鉴一些别人的开源代码加上了一些自己的理解和想法,在这里做一个总结以便记忆以及以后的使用
抱歉由于使用的是真机测试,所以涉及到一些不方便的地方,所以不得不做一些修饰,这是联系人的主界面。
点击listView,弹出窗口,进行一些操作。
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
以上是部分效果图。
=======================================
下面是代码的实现。
界面布局
1、主界面(activity_contacts_view.xml)
activity_contacts.xml
2、ContactsAcivity.java(main_activity):包含了适配器
package com.example.telephone;
import java.io.InputStream;
import java.lang.Thread.UncaughtExceptionHandler;
import java.security.Permission;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Handler;
import javax.security.auth.callback.Callback;
import org.xml.sax.helpers.DefaultHandler;
import com.example.R;
import com.example.SMS.SMSListActivity;
import com.example.bohao.CallLogBean;
import com.example.bohao.DialAdapter;
import com.example.bohao.tonghuajilu;
import com.example.service.MyService;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
//import com.example.helloworld.R;
import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.BaseBundle;
import android.os.Bundle;
import android.os.Looper;
import android.os.Message;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.Photo;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.support.v4.widget.DrawerLayout.LayoutParams;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import com.example.telephone.QuickAlphabeticBar;
import com.example.view.ui.SelectPicPopupWindow;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.PopupWindow;
import android.widget.SearchView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class ContactsActivity extends Activity {
private final String TAG="ContactsActivity";
Context mContext = null;
MyListAdapter myAdapter = null;
ListView mListView = null;
private static final String[] PHONES_PROJECTION = new String[] { /**获取库Phone表字段**/
Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID,Phone.CONTACT_ID };
private static final int PHONES_DISPLAY_NAME_INDEX = 0; /**联系人显示名称**/
private static final int PHONES_NUMBER_INDEX = 1; /**电话号码**/
private static final int PHONES_PHOTO_ID_INDEX = 2; /**头像ID**/
private static final int PHONES_CONTACT_ID_INDEX = 3; /**联系人的ID**/
private ArrayList mContactsName = new ArrayList(); /**联系人名称**/
private ArrayList mContactsNumber = new ArrayList(); /**联系人号码**/
private ArrayList mContactsPhonto = new ArrayList();/**联系人头像**/
private ArrayList mContactsId = new ArrayList();
private Context context;
private String currentSearchTip ;
private QuickAlphabeticBar alpha;
private MyReceiver receiver=null;
protected View popupWindow;
protected Object parent;
//protected Context self;
PopupWindow popup=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate Method is executed");
setContentView(R.layout.activity_contacts_view);
//requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
mListView = (ListView)this.findViewById(R.id.listView1);
TextView t = (TextView)this.findViewById(R.id.text1);
alpha = (QuickAlphabeticBar)this.mListView.findViewById(R.id.fast_scroller);
mContext = this;
ActionBar mActionbar = getActionBar();
myAdapter = new MyListAdapter(this);
mListView.setAdapter(myAdapter);
//getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
getPhoneContacts();
ImageButton b = (ImageButton) findViewById(R.id.ib_add);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Uri insertUri = android.provider.ContactsContract.Contacts.CONTENT_URI;
Intent intent = new Intent(Intent.ACTION_INSERT, insertUri);
startActivityForResult(intent, 1008);
}
});
Button b1 = (Button)this.findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(ContactsActivity.this,SelectPicPopupWindow.class));
}
});
mListView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView> parent, View view, final int position, long id) {
// TODO Auto-generated method stub
new AlertDialog.Builder(mContext)
.setTitle("请选择")
.setItems(new String[] {"拨打电话","编辑","删除","分组"},new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Uri uri = null;
switch(which){
case 0:
String toPhone = mContactsNumber.get(position);
uri = Uri.parse("tel:" + toPhone);
Intent it = new Intent(Intent.ACTION_CALL, uri);
startActivity(it);
break;
case 1:
Intent intent = new Intent(ContactsActivity.this,ContactsXXActivity.class);
startActivity(intent);
break;
case 2:
Delete(mContactsId.get(position), position);
break;
}
}
})
.show();
}
});
}
private void Delete(final Long long1, final int position){
new AlertDialog.Builder(mContext).setIcon(android.R.drawable.ic_dialog_alert).setTitle("是否删除该联系人")
.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,long1);
Uri lookupUri = ContactsContract.Contacts.getLookupUri(ContactsActivity.this.getContentResolver(), uri);
if(lookupUri != Uri.EMPTY){
ContactsActivity.this.getContentResolver().delete(uri, null, null);
}
myAdapter.remove(position);
myAdapter.notifyDataSetChanged();
Toast.makeText(ContactsActivity.this, "该联系人已经被删除.", Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
protected void onStart(){
super.onStart();
startService(new Intent(ContactsActivity.this,MyService.class));
receiver = new MyReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("com.example.service.MyService");
ContactsActivity.this.registerReceiver(receiver,filter);
Log.i(TAG, "onStart Method is executed");
}
protected void onRestart(){
super.onRestart();
Log.i(TAG, "onRestart Method is executed");
}
protected void onResume(){
super.onResume();
Log.i(TAG, "onResume Method is executed");
}
protected void onStop(){
super.onStop();
Log.i(TAG, "onStop Method is executed");
}
protected void onPause(){
super.onPause();
Log.i(TAG, " onPause Method is executed");
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(1008 == requestCode){
getPhoneContacts();
}
super.onActivityResult(requestCode, resultCode, data);
}
/**得到手机通讯录联系人信息**/
public void getPhoneContacts() {
ContentResolver resolver = mContext.getContentResolver();
Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,PHONES_PROJECTION, null, null, null); // 获取手机联系人
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX); //得到手机号码
if (TextUtils.isEmpty(phoneNumber)) //当手机号码为空的或者为空字段 跳过当前循环
continue;
String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME_INDEX); //得到联系人名称
Long contactid = phoneCursor.getLong(PHONES_CONTACT_ID_INDEX); //得到联系人ID
Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX); //得到联系人头像ID
Bitmap contactPhoto = null; //得到联系人头像Bitamp
if(photoid > 0 ) { //photoid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的
Uri uri =ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,contactid);
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri);
contactPhoto = BitmapFactory.decodeStream(input);
}else {
contactPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.ic_a6);
}
mContactsName.add(contactName);
mContactsNumber.add(phoneNumber);
mContactsPhonto.add(contactPhoto);
mContactsId.add(contactid);
}
phoneCursor.close();
}
}
class MyListAdapter extends BaseAdapter {
public MyListAdapter(Context context) {
mContext = context;
}
public void remove(int position) {
// TODO Auto-generated method stub
return;
}
public int getCount() {
//设置绘制数量
return mContactsName.size();
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView( final int position, View convertView, ViewGroup parent) {
ImageView iamge = null;
TextView title = null;
TextView text = null;
if (convertView == null || position < mContactsNumber.size()) {
convertView = LayoutInflater.from(mContext).inflate(
R.layout.activity_contacts, null);
iamge = (ImageView) convertView.findViewById(R.id.color_image);
title = (TextView) convertView.findViewById(R.id.color_title);
text = (TextView) convertView.findViewById(R.id.color_text);
}
//绘制联系人名称
title.setText(mContactsName.get(position));
title.setTextColor(Color.BLACK);
Resources resources = getBaseContext().getResources();
//绘制联系人号码
text.setText(mContactsNumber.get(position));
//绘制联系人头像
//text.setTextColor(Color.WHITE);
text.setTextColor(Color.BLACK);
Resources resources1 = getBaseContext().getResources();
iamge.setImageBitmap(mContactsPhonto.get(position));
return convertView;
}
}
protected void onDestroy(){
stopService(new Intent(ContactsActivity.this,MyService.class));
super.onDestroy();
}
public class MyReceiver extends BroadcastReceiver{
protected String CMD_STOP_SERVICE;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();
int i = bundle.getInt("i");
mListView.setFilterText(i+"");
}
}
}
3、SelectPopupWindow.java
package com.example.view.ui;
import com.example.R;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class SelectPicPopupWindow extends Activity implements OnClickListener{
private Button btn_take_photo;
private Button btn_pick_photo;
private Button btn_cancel;
private LinearLayout layout;
private Button btn_yy;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.huachu_item);
btn_take_photo = (Button) this.findViewById(R.id.tp);
btn_pick_photo = (Button) this.findViewById(R.id.pp);
btn_cancel = (Button) this.findViewById(R.id.bc);
btn_yy = (Button)this.findViewById(R.id.yy);
layout=(LinearLayout)findViewById(R.id.pop_layout);
layout.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "提示:点击窗口外部关闭窗口!",
Toast.LENGTH_SHORT).show();
}
});
//添加按钮监听
btn_cancel.setOnClickListener(this);
btn_take_photo.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(i, Activity.DEFAULT_KEYS_DIALER);
}
});
btn_pick_photo.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent picture = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(picture, 1);
}
});
btn_yy.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Intent intent = new Intent(Intent.ACTION_PICK);
// intent.setDataAndType(Uri.EMPTY,"vnd.android.cursor.dir/playlist");
// intent.putExtra("withtabs", true);
// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Intent j = Intent.createChooser(intent, "Choose an application to open with:");
// if(j == intent){
// startActivity(j);
// }else{
// Intent intent_1 = new Intent("android.intent.action.MUSIC_PLAYER");
// startActivity(intent_1);
// }
Intent intent_music = new Intent(Intent.ACTION_PICK);
intent_music.setDataAndType(Uri.EMPTY,"vnd.android.cursor.dir/playlist");
intent_music.putExtra("withtabs", true); // 显示tab选项卡
intent_music.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent_music);
}
});
}
//实现onTouchEvent触屏函数但点击屏幕时销毁本Activity
@Override
public boolean onTouchEvent(MotionEvent event){
finish();
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.tp:
break;
case R.id.pp:
break;
case R.id.bc:
break;
default:
break;
}
finish();
}
}
package com.example.view.ui;
import com.example.R;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class SelectPicPopupWindow extends Activity implements OnClickListener{
private Button btn_take_photo;
private Button btn_pick_photo;
private Button btn_cancel;
private LinearLayout layout;
private Button btn_yy;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.huachu_item);
btn_take_photo = (Button) this.findViewById(R.id.tp);
btn_pick_photo = (Button) this.findViewById(R.id.pp);
btn_cancel = (Button) this.findViewById(R.id.bc);
btn_yy = (Button)this.findViewById(R.id.yy);
layout=(LinearLayout)findViewById(R.id.pop_layout);
layout.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "提示:点击窗口外部关闭窗口!",
Toast.LENGTH_SHORT).show();
}
});
//添加按钮监听
btn_cancel.setOnClickListener(this);
btn_take_photo.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(i, Activity.DEFAULT_KEYS_DIALER);
}
});
btn_pick_photo.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent picture = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(picture, 1);
}
});
btn_yy.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Intent intent = new Intent(Intent.ACTION_PICK);
// intent.setDataAndType(Uri.EMPTY,"vnd.android.cursor.dir/playlist");
// intent.putExtra("withtabs", true);
// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Intent j = Intent.createChooser(intent, "Choose an application to open with:");
// if(j == intent){
// startActivity(j);
// }else{
// Intent intent_1 = new Intent("android.intent.action.MUSIC_PLAYER");
// startActivity(intent_1);
// }
Intent intent_music = new Intent(Intent.ACTION_PICK);
intent_music.setDataAndType(Uri.EMPTY,"vnd.android.cursor.dir/playlist");
intent_music.putExtra("withtabs", true); // 显示tab选项卡
intent_music.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent_music);
}
});
}
//实现onTouchEvent触屏函数但点击屏幕时销毁本Activity
@Override
public boolean onTouchEvent(MotionEvent event){
finish();
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.tp:
break;
case R.id.pp:
break;
case R.id.bc:
break;
default:
break;
}
finish();
}
}
4、QuickAlphabeticBa.java(这部分是根据别人的开源代码写的,参考)
package com.example.telephone;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class QuickAlphabeticBar extends View{
public QuickAlphabeticBar(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
// 字母表中的字符
private String alphabet[] = { "#", "A", "B", "C", "D", "E", "F", "G", "H",
"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
"V", "W", "X", "Y", "Z", "#", };
// 字母的颜色
private int defaultColor = Color.RED;
private int selectColor = Color.BLUE;
// 被选中的字符
private int selectedIndex = 0;
// 画笔--用于绘制右侧字母
Paint paint = new Paint();
// 选中的字母被改变监听器
private OnTouchLetterChangedListener changedListener;
// 选中的字母被释放监听器
private OnTouchLetterReleasedListener releasedListener;
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 获取当前View的宽度和高度
int width = getWidth();
// 计算单个字符所占高度
int singleLetter = getHeight() / (alphabet.length);
// 自上而下逐一绘制字母表中的每个字符
for (int i = 0; i < alphabet.length; i++) {
// 若没有没选中时显示默认颜色,若被选中显示指定的高亮色
if (i != selectedIndex) {
paint.setColor(defaultColor);
paint.setAntiAlias(true);
paint.setTextSize(40);
} else {
paint.setTextSize(40);
paint.setColor(selectColor);
}
// 计算第i个字符在屏幕中的位置(x,y)
float x = width / 2 - paint.measureText(alphabet[i]) / 2;
float y = singleLetter * (i + 1);
// 在指定位置绘制指定字符
canvas.drawText(alphabet[i], x, y, paint);
// 重置画笔的属性
paint.reset();
}
}
// 设置字母表中的字符
public void setAlphabet(String[] alphabet) {
this.alphabet = alphabet;
}
// 设置字母默认显示的颜色
public void setDefaultColor(int defaultColor) {
this.defaultColor = defaultColor;
}
// 设置字母被选中时显示的颜色
public void setSelectColor(int selectColor) {
this.selectColor = selectColor;
}
// 设置选中字母已改变监听事件
public void setOnTouchLetterChangedListener(
OnTouchLetterChangedListener changedListener) {
this.changedListener = changedListener;
}
// 设置已释放字母选中监听事件
public void setOnTouchLetterReleasedListener(
OnTouchLetterReleasedListener releasedListener) {
this.releasedListener = releasedListener;
}
public interface OnTouchLetterChangedListener {
public void onTouchLetterChangedListener(String s);
}
public interface OnTouchLetterReleasedListener {
public void onTouchLetterReleasedListener();
}
}