Android编程实现读取手机联系人、拨号、发送短信及长按菜单操作方法实例小结
程序员文章站
2023-11-28 21:44:04
本文实例讲述了android编程实现读取手机联系人、拨号、发送短信及长按菜单操作方法。分享给大家供大家参考,具体如下:
1.andrid项目结构图↓主要操作图中红色方框内...
本文实例讲述了android编程实现读取手机联系人、拨号、发送短信及长按菜单操作方法。分享给大家供大家参考,具体如下:
1.andrid项目结构图↓主要操作图中红色方框内的文件。
2.首先布局代码如下
a, main.xml 程序运行的主界面,主要用listview列表控件展示手机联系人
<?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:background="@drawable/bg" android:orientation="vertical" > <listview android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginleft="5dip" android:cachecolorhint="#00000000" android:divider="@drawable/divider_horizontal_bright" android:paddingright="5dip" > </listview> </linearlayout>
b.list_item.xml listview的列表项布局文件,相当于展示模版
<?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 长按菜单显示联系人详细布局界面,示例只做了跳转展示
<?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 程序运行的入口文件
package com.example.myandroid; import java.util.arraylist; import java.util.hashmap; import java.util.iterator; import android.app.activity; import android.content.contentresolver; import android.content.intent; import android.database.cursor; import android.net.uri; import android.os.bundle; import android.provider.contactscontract; import android.provider.contactscontract.commondatakinds; import android.view.contextmenu; import android.view.contextmenu.contextmenuinfo; import android.view.menuitem; import android.view.view; import android.view.view.oncreatecontextmenulistener; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.listview; import android.widget.simpleadapter; import android.widget.toast; import com.ymw.details.detail; public class mainactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); final listview listview = (listview) findviewbyid(r.id.listview); // 生成动态数组,加入数据 arraylist<hashmap<string, object>> listitem = fillmaps(); simpleadapter listitemadapter = new simpleadapter(this, listitem, r.layout.list_item, new string[] { "imgview", "name", "number" }, new int[] { r.id.imgview, r.id.name, r.id.number }); listview.setadapter(listitemadapter); // 添加单击事件 listview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { hashmap<string, string> map = (hashmap<string, string>) listview .getitematposition(arg2); string name = map.get("name"); toast toast = toast.maketext(getapplicationcontext(), "第" + arg2 + "项" + name, toast.length_long); toast.show(); string phonenum = map.get("number"); intent intent = new intent(intent.action_call, uri.parse("tel:" + phonenum)); startactivity(intent); } }); // 添加长按菜单 listview.setoncreatecontextmenulistener(new oncreatecontextmenulistener() { public void oncreatecontextmenu(contextmenu menu, view v, contextmenuinfo menuinfo) { menu.setheadertitle("长按菜单-contextmenu"); menu.add(0, 0, 0, "查看详细"); menu.add(0, 1, 0, "发送信息"); menu.add(0, 2, 0, "删除联系人"); } }); } public boolean oncontextitemselected(menuitem item) { // settitle("点击了长按菜单里面的第"+item.getitemid()+"个项目"); toast.maketext(getapplicationcontext(), "选择了" + item.getitemid() + item.gettitle() + "项", toast.length_long).show(); int id = item.getitemid(); // 查看详细 if (id == 0) { intent intent = new intent(); intent.putextra("ymw", item.gettitle()); intent.setclass(mainactivity.this, detail.class); startactivity(intent); } // 发送短信 else if (id == 1) { uri uri = uri.parse("smsto://18664599745"); intent intent = new intent(intent.action_sendto, uri); intent.putextra("sms_body", "ymw-love-yh"); startactivity(intent); } // 删除联系人 else if (id == 2) { } return super.oncontextitemselected(item); } // 获取手机联系人列表方法一 public arraylist<hashmap<string, object>> getcontects() { arraylist<hashmap<string, object>> list = new arraylist<hashmap<string, object>>(); cursor cursor = getcontentresolver().query( contactscontract.contacts.content_uri, null, null, null, contactscontract.contacts.display_name + " collate localized asc"); if (cursor.movetofirst()) { int idcolumn = cursor.getcolumnindex(contactscontract.contacts._id); int namecolum = cursor .getcolumnindex(contactscontract.contacts.display_name); do { string contactid = cursor.getstring(idcolumn); string displaynamestring = cursor.getstring(namecolum); // 查看有多少电话号码 没有则返回为0 int phonecount = cursor .getint(cursor .getcolumnindex(contactscontract.contacts.has_phone_number)); if (phonecount > 0) { // 获得联系人的电话号码 cursor phones = getcontentresolver().query( contactscontract.commondatakinds.phone.content_uri, null, contactscontract.commondatakinds.phone.contact_id + "=" + contactid, null, null); hashmap<string, object> map = new hashmap<string, object>(); map.put("imgview", r.drawable.ic_launcher); map.put("name", displaynamestring); list.add(map); } } while (cursor.movetonext()); if (cursor != null) cursor.close(); } return list; } // 获取联系人方法二 public arraylist<hashmap<string, object>> fillmaps() { arraylist<hashmap<string, object>> items = new arraylist<hashmap<string, object>>(); contentresolver cr = getcontentresolver(); hashmap<string, arraylist<string>> hashmap = new hashmap<string, arraylist<string>>(); cursor phone = cr.query(commondatakinds.phone.content_uri, new string[] { commondatakinds.phone.contact_id, commondatakinds.phone.display_name, commondatakinds.phone.number, commondatakinds.phone.data1 // commondatakinds.structuredpostal.data3, }, null, null, null); while (phone.movetonext()) { string contactid = phone.getstring(phone .getcolumnindex(commondatakinds.phone.contact_id)); string displayname = phone.getstring(phone .getcolumnindex(commondatakinds.phone.display_name)); string phonenumber = phone .getstring(phone .getcolumnindex(contactscontract.commondatakinds.phone.number)); string address = phone.getstring(phone .getcolumnindex(commondatakinds.phone.data1)); // 以contactid为主键,把同一人的所有电话都存到一起。 arraylist<string> ad = hashmap.get(contactid); if (ad == null) { ad = new arraylist<string>(); ad.add(displayname); ad.add(phonenumber); // ad.add(address); hashmap.put(contactid, ad); } else { ad.add(phonenumber); } } phone.close(); arraylist<string> tmplist; string tmpstr = ""; int k; iterator iter = hashmap.entryset().iterator(); while (iter.hasnext()) { hashmap.entry entry = (hashmap.entry) iter.next(); object key = entry.getkey(); object val = entry.getvalue(); tmplist = (arraylist) val; tmpstr = ""; for (k = 1; k < tmplist.size(); k++) { tmpstr = tmpstr + tmplist.get(k) + ','; } tmpstr = getstring(tmpstr); hashmap<string, object> tmpmap = new hashmap<string, object>(); tmpmap.put("name", tmplist.get(0)); tmpmap.put("number", tmpstr); tmpmap.put("imgview", r.drawable.ic_launcher); items.add(tmpmap); } return items; } private string getstring(string str) { string strlast = ""; int i = str.lastindexof(","); if (i > 0) { strlast = str.substring(0, str.length() - 1); } return strlast.replace(" ", "").replace(",", "\n").replace("+86", ""); } }
b,detail.java 主界面长按菜单显示联系人详细的跳转界面,接受主界面传来的参数
package com.ymw.details; import com.example.myandroid.r; import android.app.activity; import android.content.intent; import android.os.bundle; import android.widget.textview; public class detail extends activity { @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(com.example.myandroid.r.layout.phonedetails); intent intent = getintent(); string strpara = intent.getstringextra("ymw"); textview tview = (textview) findviewbyid(r.id.ymw); tview.settext(strpara); } }
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智能手机运行本程序可以看到手机联系人列表,
单击某个联系人会直接拨号,长按某个联系人会出现菜单选项,可以选择发送短信。
希望本文所述对大家android程序设计有所帮助。