java 获取手机联系人_Android 读取手机联系人、拨号、发送短信及长按菜单的操作...

该示例详细介绍了如何在Android应用中实现读取手机联系人、拨号、发送短信及响应长按事件的功能。包括布局文件`main.xml`、`list_item.xml`和`phonedetails.xml`的设计,以及`MainActivity`和`Detail`类的Java实现。通过动态加载数据到ListView,并设置单击和长按事件,实现联系人详情显示和拨号、发送短信的操作。此外,还讨论了所需的Android权限配置。
摘要由CSDN通过智能技术生成

本示例实现了读取手机联系人,拨号、发送短信及长按出现菜单选项的操作↓

1.Andrid项目结构图↓主要操作图中红色方框内的文件。

09e96119eaa0ba4a752ff9408e649b54.png

2.首先布局代码如下↓

a, main.xml 程序运行的主界面,主要用ListView列表控件展示手机联系人

1 <?xml version="1.0" encoding="utf-8"?>

2

3 android:layout_width="match_parent"

4 android:layout_height="match_parent"

5 android:background="@drawable/bg"

6 android:orientation="vertical" >

7

8

10 android:layout_width="fill_parent"

11 android:layout_height="fill_parent"

12 android:layout_marginLeft="5dip"

13 android:cacheColorHint="#00000000"

14 android:divider="@drawable/divider_horizontal_bright"

15 android:paddingRight="5dip" >

16

17

18

b.list_item.xml ListView的列表项布局文件,相当于展示模版

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

android:id="@+id/imgView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:contentDescription="@string/photo"

android:paddingRight="2dip" />

android:id="@+id/name"

android:layout_width="80dip"

android:layout_height="wrap_content"

android:layout_marginLeft="10dip"

android:paddingTop="8dip"

android:singleLine="false"

android:textAppearance="?android:attr/textAppearanceMedium"

android:textColor="#ffffff" />

android:id="@+id/number"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginRight="6dip"

android:paddingTop="8dip"

android:singleLine="false"

android:textColor="#ffffff"

android:textAppearance="?android:attr/textAppearanceMedium"/>

c,phonedetails.xml 长按菜单显示联系人详细布局界面,示例只做了跳转展示

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/ymw"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:textAppearance="?android:attr/textAppearanceMedium"/>

2.Java实现代码如下↓

a,MainActivity.java 程序运行的入口文件

1 packagecom.example.myandroid;2

3 importjava.util.ArrayList;4 importjava.util.HashMap;5 importjava.util.Iterator;6

7 importandroid.app.Activity;8 importandroid.content.ContentResolver;9 importandroid.content.Intent;10 importandroid.database.Cursor;11 importandroid.net.Uri;12 importandroid.os.Bundle;13 importandroid.provider.ContactsContract;14 importandroid.provider.ContactsContract.CommonDataKinds;15 importandroid.view.ContextMenu;16 importandroid.view.ContextMenu.ContextMenuInfo;17 importandroid.view.MenuItem;18 importandroid.view.View;19 importandroid.view.View.OnCreateContextMenuListener;20 importandroid.widget.AdapterView;21 importandroid.widget.AdapterView.OnItemClickListener;22 importandroid.widget.ListView;23 importandroid.widget.SimpleAdapter;24 importandroid.widget.Toast;25

26 importcom.ymw.details.Detail;27

28 public class MainActivity extendsActivity {29

30

31 @Override32 public voidonCreate(Bundle savedInstanceState) {33 super.onCreate(savedInstanceState);34 setContentView(R.layout.main);35

36 final ListView listView =(ListView) findViewById(R.id.listView);37

38 //生成动态数组,加入数据

39 ArrayList> listItem =fillMaps();40

41 SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItem,42 R.layout.list_item,43 new String[] { "imgView", "name", "number" }, new int[] {44 R.id.imgView, R.id.name, R.id.number });45 listView.setAdapter(listItemAdapter);46

47 //添加单击事件

48 listView.setOnItemClickListener(newOnItemClickListener() {49 @Override50 public void onItemClick(AdapterView> arg0, View arg1, intarg2,51 longarg3) {52 HashMap map = (HashMap) listView53 .getItemAtPosition(arg2);54 String name = map.get("name");55 Toast toast = Toast.makeText(getApplicationContext(), "第"

56 + arg2 + "项" +name, Toast.LENGTH_LONG);57 toast.show();58 String phoneNum = map.get("number");59 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"

60 +phoneNum));61 startActivity(intent);62 }63 });64

65 //添加长按菜单

66 listView.setOnCreateContextMenuListener(newOnCreateContextMenuListener() {67 public voidonCreateContextMenu(ContextMenu menu, View v,68 ContextMenuInfo menuInfo) {69 menu.setHeaderTitle("长按菜单-ContextMenu");70 menu.add(0, 0, 0, "查看详细");71 menu.add(0, 1, 0, "发送信息");72 menu.add(0, 2, 0, "删除联系人");73 }74 });75 }76

77 public booleanonContextItemSelected(MenuItem item) {78 //setTitle("点击了长按菜单里面的第"+item.getItemId()+"个项目");

79 Toast.makeText(getApplicationContext(),80 "选择了" + item.getItemId() + item.getTitle() + "项",81 Toast.LENGTH_LONG).show();82

83 int id =item.getItemId();84 //查看详细

85 if (id == 0) {86 Intent intent = newIntent();87 intent.putExtra("ymw", item.getTitle());88 intent.setClass(MainActivity.this, Detail.class);89 startActivity(intent);90 }91 //发送短信

92 else if (id == 1) {93 Uri uri = Uri.parse("smsto://18664599745");94 Intent intent = newIntent(Intent.ACTION_SENDTO, uri);95 intent.putExtra("sms_body", "ymw-LOVE-yh");96 startActivity(intent);97 }98 //删除联系人

99 else if (id == 2) {100 }101 return super.onContextItemSelected(item);102 }103

104 //获取手机联系人列表方法一

105 public ArrayList>GetContects() {106 ArrayList> list = new ArrayList>();107 Cursor cursor =getContentResolver().query(108 ContactsContract.Contacts.CONTENT_URI,109 null,110 null,111 null,112 ContactsContract.Contacts.DISPLAY_NAME113 + " COLLATE LOCALIZED ASC");114

115 if(cursor.moveToFirst()) {116 int idColumn =cursor.getColumnIndex(ContactsContract.Contacts._ID);117 int nameColum =cursor118 .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);119 do{120 String contactId =cursor.getString(idColumn);121 String disPlayNameString =cursor.getString(nameColum);122

123 //查看有多少电话号码 没有则返回为0

124 int phoneCount =cursor125 .getInt(cursor126 .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));127

128 if (phoneCount > 0) {129 //获得联系人的电话号码

130 Cursor phones =getContentResolver().query(131 ContactsContract.CommonDataKinds.Phone.CONTENT_URI,132 null,133 ContactsContract.CommonDataKinds.Phone.CONTACT_ID134 + "=" + contactId, null, null);135 HashMap map = new HashMap();136 map.put("imgView", R.drawable.ic_launcher);137 map.put("name", disPlayNameString);138 list.add(map);139

140 }141 } while(cursor.moveToNext());142 if (cursor != null)143 cursor.close();144 }145 returnlist;146 }147

148 //获取联系人方法二

149 public ArrayList>fillMaps() {150 ArrayList> items = new ArrayList>();151

152 ContentResolver cr =getContentResolver();153 HashMap> hashMap = new HashMap>();154 Cursor phone =cr.query(CommonDataKinds.Phone.CONTENT_URI,155 newString[] { CommonDataKinds.Phone.CONTACT_ID,156 CommonDataKinds.Phone.DISPLAY_NAME,157 CommonDataKinds.Phone.NUMBER,158 CommonDataKinds.Phone.DATA1159 //CommonDataKinds.StructuredPostal.DATA3,

160 }, null, null, null);161 while(phone.moveToNext()) {162 String contactId =phone.getString(phone163 .getColumnIndex(CommonDataKinds.Phone.CONTACT_ID));164 String displayName =phone.getString(phone165 .getColumnIndex(CommonDataKinds.Phone.DISPLAY_NAME));166 String PhoneNumber =phone167 .getString(phone168 .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));169 String address =phone.getString(phone170 .getColumnIndex(CommonDataKinds.Phone.DATA1));171

172 //以contactId为主键,把同一人的所有电话都存到一起。

173 ArrayList ad =hashMap.get(contactId);174 if (ad == null) {175 ad = new ArrayList();176 ad.add(displayName);177 ad.add(PhoneNumber);178 //ad.add(address);

179

180 hashMap.put(contactId, ad);181 } else{182 ad.add(PhoneNumber);183 }184

185 }186 phone.close();187

188 ArrayListtmpList;189 String tmpStr = "";190 intk;191 Iterator iter =hashMap.entrySet().iterator();192 while(iter.hasNext()) {193 HashMap.Entry entry =(HashMap.Entry) iter.next();194 Object key =entry.getKey();195 Object val =entry.getValue();196

197 tmpList =(ArrayList) val;198 tmpStr = "";199 for (k = 1; k < tmpList.size(); k++) {200 tmpStr = tmpStr + tmpList.get(k) + ',';201 }202 tmpStr =GetString(tmpStr);203 HashMap tmpMap = new HashMap();204 tmpMap.put("name", tmpList.get(0));205 tmpMap.put("number", tmpStr);206 tmpMap.put("imgView", R.drawable.ic_launcher);207 items.add(tmpMap);208 }209 returnitems;210 }211

212 privateString GetString(String str) {213

214 String strLast = "";215 int i = str.lastIndexOf(",");216 if (i > 0) {217 strLast = str.substring(0, str.length() - 1);218 }219 return strLast.replace(" ", "").replace(",", "\n").replace("+86", "");220 }221

222 }

b,Detail.java 主界面长按菜单显示联系人详细的跳转界面,接受主界面传来的参数

1 packagecom.ymw.details;2

3 importcom.example.myandroid.R;4

5 importandroid.app.Activity;6 importandroid.content.Intent;7 importandroid.os.Bundle;8 importandroid.widget.TextView;9

10 public class Detail extendsActivity {11 @Override12 protected voidonCreate(Bundle savedInstanceState) {13 //TODO Auto-generated method stub

14 super.onCreate(savedInstanceState);15 setContentView(com.example.myandroid.R.layout.phonedetails);16

17 Intent intent =getIntent();18 String strPara = intent.getStringExtra("ymw");19

20 TextView tView =(TextView) findViewById(R.id.ymw);21 tView.setText(strPara);22 }23 }

3.获取手机联系人和拨号发短信等需要配置权限↓

在AndroidManifest.xml文件中的application节点上加入如下代码

4.使用Android模拟器或连接Android智能手机运行本程序可以看到手机联系人列表,

单击某个联系人会直接拨号,长按某个联系人会出现菜单选项,可以选择发送短信。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值