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

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 读取手机联系人、拨号、发送短信及长按菜单的操作的更多相关文章

  1. Siemens3508手机AT指令发送短信的实验

    凡夫 最近利用Siemens3508旧手机做了AT指令发送短信的实验.有人可能认为我费那么大劲折腾累不累,告诉你这可是废物再利用,可以利用旧手机里的GSM/GPRS模块做无线远程多点分布数据采集.监控 ...

  2. android中调用系统的发送短信、发送邮件、打电话功能

    1 调用发送短信功能: Uri smsToUri = Uri.parse("smsto:");  Intent sendIntent = new Intent(Intent.ACT ...

  3. 脚本控制向Android模拟拨打电话,发送短信,定位设置功能

    做行为触发的时候要向模拟器实现拨打电话,发送短信,定位设置的的功能,可以很方便通过telnet localhost  5554实现. 写个脚本很快的搞定了.网上资料很多,脚本的很少,也所积点德啦. 写 ...

  4. Android 倒计时按钮,倒计时发送短信验证码…

    Android基础之——CountDownTimer类,轻松实现倒计时功能https://www.cnblogs.com/yfceshi/p/6853746.html android中获取验证码后出现 ...

  5. 微信开发之移动手机WEB页面(HTML5)Javascript实现一键拨号及短信发送功能

    在做一个微信的微网站中的一个便民服务电话功能的应用,用到移动web页面中列出的电话号码,点击需要实现调用通讯录,网页一键拨号的拨打电话功能. 如果需要在移动浏览器中实现拨打电话,发送email,美国服 ...

  6. Android开发中使用Intent跳转到系统应用中的拨号界面、联系人界面、短信界面

    现在开发中的功能需要直接跳转到拨号.联系人.短信界面等等,查找了很多资料,自己整理了一下. 首先,我们先看拨号界面,代码如下: Intent intent =new Intent(); intent. ...

  7. [HTML] 微信开发之移动手机WEB页面(HTML5)Javascript实现一键拨号及短信发送功能

    在做一个微信的微网站中的一个便民服务电话功能的应用,用到移动web页面中列出的电话号码,点击需要实现调用通讯录,网页一键拨号的拨打电话功能. 如果需要在移动浏览器中实现拨打电话,发送email,美国服 ...

  8. Android开发之Intent跳转到系统应用中的拨号界面、联系人界面、短信界面

    现在开发中的功能需要直接跳转到拨号.联系人.短信界面等等,查找了很多资料,自己整理了一下. 1.跳转到拨号界面,代码如下: 1)直接拨打 Intent intentPhone = new Intent ...

  9. Android面试收集录 电话、短信和联系人、多媒体技术

    1.请写出调用系统拨号界面? Intent intent=new Intent(Intent.ACTION_DIAL,Uri.pase("tel:12345678910")); s ...

随机推荐

  1. commons-logging日志实现解耦

    一.需要解耦      日志是实际应用中的一个重要部分,日志系统也有许多开源的实现,如java.util.logging, logback, log4j系列等.      在使用日志系统时,如果与具体 ...

  2. oracle for update和for update nowait 的区别

    原文地址:http://www.cnblogs.com/quanweiru/archive/2012/11/09/2762223.html 1.for update 和 for update nowa ...

  3. Leetcode 105. 从前序与中序遍历序列构造二叉树

    题目链接 题目描述 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder ...

  4. 怎么把myeclipse项目导入IDEA中

    先把myeclipse下的项目拷贝到IDEA的部署目录中,把一些不用的配置文件删除,只留下一个干净的项目 打开IDEA,点击import Project,引入一个项目,选择IDEA部署目录下刚拷贝过去 ...

  5. 矩阵儿快速幂 - POJ 3233 矩阵力量系列

    不要管上面的标题的bug 那是幂的意思,不是力量... POJ 3233 Matrix Power Series 描述 Given a n × n matrix A and a positive in ...

  6. Android输入法弹出时覆盖输入框问题

    本文来自网易云社区 作者:孙有军 当一个activity中含有输入框时,我们点击输入框,会弹出输入法界面,整个界面的变化效果与manifest中对应设置的android:windowSoftInput ...

  7. Careercup - Microsoft面试题 - 5188169901277184

    2014-05-12 06:12 题目链接 原题: Write a function to retrieve the number of a occurrences of a substring(ev ...

  8. linux环境搭建系列之Apache MQ安装

    1.创建文件夹 #mkdir MQ 2.解压 #tar -vxf apache-activemq-5.14.3-bin.tar.gz 3.进入解压后的目录 # cd apache-activemq-5 ...

  9. 启用hyper后无法打开vmware

    十万火急,想办法先让虚拟机能够打开,毕竟经常用. 网上看了无数教程都是让在控制面板中关闭hyper-v,然而并没有用. 找了好久说是不能那样关闭,得用指令.管理员运行powershell,输入下列指令 ...

  10. jmeter+ANT+Jekins性能自动生成测试报告脚本(模板),加入:Median TIme、90%、95%、99%、QPS、以及流量显示

    <?xml version="1.0"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/T ...