Android 读取手机联系人、拨号、发送短信及长按菜单的操作
本示例实现了读取手机联系人,拨号、发送短信及长按出现菜单选项的操作↓
1.Andrid项目结构图↓主要操作图中红色方框内的文件。
2.首先布局代码如下↓
a, main.xml 程序运行的主界面,主要用ListView列表控件展示手机联系人
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="@drawable/bg" 6 android:orientation="vertical" > 7 8 <ListView 9 android:id="@+id/listView" 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 </ListView> 17 18 </LinearLayout>
b.list_item.xml ListView的列表项布局文件,相当于展示模版
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 |
<?xml version= "1.0"
encoding= "utf-8" ?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:orientation= "horizontal"
> <ImageView android:id= "@+id/imgView" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:contentDescription= "@string/photo" android:paddingRight= "2dip"
/> <TextView 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"
/> <TextView 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" /> </LinearLayout> |
c,phonedetails.xml 长按菜单显示联系人详细布局界面,示例只做了跳转展示
1
2
3
4
5
6
7
8
9
10
11
12
13 |
<?xml version= "1.0"
encoding= "utf-8" ?> <LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical"
> <TextView android:id= "@+id/ymw" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:textAppearance= "?android:attr/textAppearanceMedium" /> </LinearLayout> |
2.Java实现代码如下↓
a,MainActivity.java 程序运行的入口文件
1 package com.example.myandroid; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.Iterator; 6 7 import android.app.Activity; 8 import android.content.ContentResolver; 9 import android.content.Intent; 10 import android.database.Cursor; 11 import android.net.Uri; 12 import android.os.Bundle; 13 import android.provider.ContactsContract; 14 import android.provider.ContactsContract.CommonDataKinds; 15 import android.view.ContextMenu; 16 import android.view.ContextMenu.ContextMenuInfo; 17 import android.view.MenuItem; 18 import android.view.View; 19 import android.view.View.OnCreateContextMenuListener; 20 import android.widget.AdapterView; 21 import android.widget.AdapterView.OnItemClickListener; 22 import android.widget.ListView; 23 import android.widget.SimpleAdapter; 24 import android.widget.Toast; 25 26 import com.ymw.details.Detail; 27 28 public class MainActivity extends Activity { 29 30 31 @Override 32 public void onCreate(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<HashMap<String, Object>> 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(new OnItemClickListener() { 49 @Override 50 public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 51 long arg3) { 52 HashMap<String, String> map = (HashMap<String, String>) listView 53 .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(new OnCreateContextMenuListener() { 67 public void onCreateContextMenu(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 boolean onContextItemSelected(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 = new Intent(); 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 = new Intent(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<HashMap<String, Object>> GetContects() { 106 ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>(); 107 Cursor cursor = getContentResolver().query( 108 ContactsContract.Contacts.CONTENT_URI, 109 null, 110 null, 111 null, 112 ContactsContract.Contacts.DISPLAY_NAME 113 + " COLLATE LOCALIZED ASC"); 114 115 if (cursor.moveToFirst()) { 116 int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID); 117 int nameColum = cursor 118 .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 = cursor 125 .getInt(cursor 126 .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_ID 134 + "=" + contactId, null, null); 135 HashMap<String, Object> map = new HashMap<String, Object>(); 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 return list; 146 } 147 148 // 获取联系人方法二 149 public ArrayList<HashMap<String, Object>> fillMaps() { 150 ArrayList<HashMap<String, Object>> items = new ArrayList<HashMap<String, Object>>(); 151 152 ContentResolver cr = getContentResolver(); 153 HashMap<String, ArrayList<String>> hashMap = new HashMap<String, ArrayList<String>>(); 154 Cursor phone = cr.query(CommonDataKinds.Phone.CONTENT_URI, 155 new String[] { CommonDataKinds.Phone.CONTACT_ID, 156 CommonDataKinds.Phone.DISPLAY_NAME, 157 CommonDataKinds.Phone.NUMBER, 158 CommonDataKinds.Phone.DATA1 159 // CommonDataKinds.StructuredPostal.DATA3, 160 }, null, null, null); 161 while (phone.moveToNext()) { 162 String contactId = phone.getString(phone 163 .getColumnIndex(CommonDataKinds.Phone.CONTACT_ID)); 164 String displayName = phone.getString(phone 165 .getColumnIndex(CommonDataKinds.Phone.DISPLAY_NAME)); 166 String PhoneNumber = phone 167 .getString(phone 168 .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 169 String address = phone.getString(phone 170 .getColumnIndex(CommonDataKinds.Phone.DATA1)); 171 172 // 以contactId为主键,把同一人的所有电话都存到一起。 173 ArrayList<String> ad = hashMap.get(contactId); 174 if (ad == null) { 175 ad = new ArrayList<String>(); 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 ArrayList<String> tmpList; 189 String tmpStr = ""; 190 int k; 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<String, Object> tmpMap = new HashMap<String, Object>(); 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 return items; 210 } 211 212 private String 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 package com.ymw.details; 2 3 import com.example.myandroid.R; 4 5 import android.app.Activity; 6 import android.content.Intent; 7 import android.os.Bundle; 8 import android.widget.TextView; 9 10 public class Detail extends Activity { 11 @Override 12 protected void onCreate(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节点上加入如下代码
<!--添加权限-->
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
4.使用Android模拟器或连接Android智能手机运行本程序可以看到手机联系人列表,
单击某个联系人会直接拨号,长按某个联系人会出现菜单选项,可以选择发送短信。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。