本文实例讲述了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" ><ListVIEwandroID: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" ><ImageVIEwandroID:ID="@+ID/imgVIEw"androID:layout_wIDth="wrap_content"androID:layout_height="wrap_content"androID:contentDescription="@string/photo"androID:paddingRight="2dip" /><TextVIEwandroID: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" /><TextVIEwandroID: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" ><TextVIEwandroID: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 {@OverrIDepublic 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() {@OverrIDepublic 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,"查看详细");menu.add(0,1,"发送信息");menu.add(0,2,"删除联系人");}});}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,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);// 查看有多少电话号码 没有则返回为0int phoneCount = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));if (phoneCount > 0) {// 获得联系人的电话号码Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ "=" + contactID,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);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 {@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {// Todo auto-generated method stubsuper.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程序设计有所帮助。
总结以上是内存溢出为你收集整理的Android编程实现读取手机联系人、拨号、发送短信及长按菜单 *** 作方法实例小结全部内容,希望文章能够帮你解决Android编程实现读取手机联系人、拨号、发送短信及长按菜单 *** 作方法实例小结所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)