本文实例讲述了Android编程实现读取手机联系人、拨号、发送短信及长按菜单操作方法。分享给大家供大家参考,具体如下:

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

2.首先布局代码如下

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

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@drawable/bg"

android:orientation="vertical" >

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" >

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 程序运行的入口文件

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> 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 map = (HashMap) 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> GetContects() {

ArrayList> list = new ArrayList>();

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 map = new HashMap();

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> fillMaps() {

ArrayList> items = new ArrayList>();

ContentResolver cr = getContentResolver();

HashMap> hashMap = new HashMap>();

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 ad = hashMap.get(contactId);

if (ad == null) {

ad = new ArrayList();

ad.add(displayName);

ad.add(PhoneNumber);

// ad.add(address);

hashMap.put(contactId, ad);

} else {

ad.add(PhoneNumber);

}

}

phone.close();

ArrayList 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 tmpMap = new HashMap();

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节点上加入如下代码

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

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

希望本文所述对大家Android程序设计有所帮助。

java 添加手机联系人_Android编程实现读取手机联系人、拨号、发送短信及长按菜单操作方法实例小结...相关推荐

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

    本示例实现了读取手机联系人,拨号.发送短信及长按出现菜单选项的操作↓ 1.Andrid项目结构图↓主要操作图中红色方框内的文件. 2.首先布局代码如下↓ a, main.xml 程序运行的主界面,主要 ...

  2. 基于HTTP协议用JAVA实现读取天气预报和twilio发送短信(无需审核)

    文章目录 一.读取指定城市的天气预报 二.twilio平台java编程实现发送短信 三.总结 四.参考 一.读取指定城市的天气预报 代码如下: package network;import java. ...

  3. Java实现发送手机验证码,发送短信

    要发送短信,我们需要第三方提供的短信接口,这里我使用阿里云的短信服务. 首先进入阿里云的官网,然后注册.登陆之后选择云通信-->短信服务 然后点击免费开通. 开通后进入控制中心,选择右上方的支持 ...

  4. java给手机发短信_java给手机发送短信验证码

    最近在做项目,分配到一个使用手机验证码实现快速登录的需求,好了No B B 了直接进入主题. 首先进入官网(http://www.miaodiyun.com/) 进行注册登录,这个不用教吧. 注册登录 ...

  5. java程序或javaweb(网站)向手机发送短信

    转:http://blog.csdn.net/csh624366188/article/details/7183457 今天闲来无事,在微博上看到一个关于用java实现的一个发送手机短信的程序,看了看 ...

  6. 发送短信验证码到手机(阿里大于平台) java

    使用Eclipse通过阿里大于发送短信验证码至手机 第一次撰写博客,如有不足望提出且多多见谅! 最近项目当中需要用到手机验证码功能.所以一直都在找一家比较不错的短信平台,然而现在短信平台鱼龙混杂,还真 ...

  7. Java实现手机发送短信验证码

    发送短信验证码首先要在互亿无线短信平台去开通短信服务,地址"ihuyi.com" 一定要留好自己申请的API ID,API key和模板 参数说明: 代码示例: pom导包 < ...

  8. 来电黑名单 java 软件_Android8.1 源码修改之通过黑名单屏蔽系统短信功能和来电功能...

    前言 之前写过一篇Android6.0 的屏蔽系统短信功能和来电功能,具体看这里 同样的最近有个新需求,需要将8.1 设备的来电功能和短信功能都屏蔽掉,特殊产品就是特殊定制,那就开始吧. 屏蔽短信功能 ...

  9. java 短信猫发送短信的方法

    http://zghbwjl.blog.163.com/blog/static/12033667220129175158806/ http://www.smscom.cn/sms_javasoft/ ...

最新文章

  1. 网站SEO中内页标签该如何进行优化?
  2. 20165320 第二周学习总结
  3. java附加属性_Java 9附加流
  4. 百度SEO 统计平台推送工具 1.8
  5. Java 编解码问题
  6. JAVA开发面试常问问题总结1
  7. 企业信息安全建设要点梳理
  8. 抛弃 VS Code 我还能用啥编辑器?| 技术头条
  9. PSFTP 常用命令
  10. 【YY手机】用AVR单片机制作手机系列教程-基础篇
  11. macOS图像文件不能正常显示缩略图预览怎么办?
  12. 什么是激光导航扫地机器人?
  13. C语言程序设计 使用结构体类型处理组合数据——用户自定义数据类型
  14. 一个好用的android图片压缩工具类
  15. 一个华裔女孩马天琪的心愿
  16. cstring头文件
  17. 计算机 运行新ie 命令,如何使用命令行卸载IE10浏览器?
  18. 常用HTML代码,字体颜色等
  19. Amesim学习——热传导基础案例:金和铝的导热性比较
  20. 计算机2010版本怎么样的,cad2010版本使用起来怎么样

热门文章

  1. 输入三角形的三条边长,判断其是否能构成三角形,并计算其面积。
  2. #10166. 「一本通 5.3 练习 1」数字游戏
  3. 我天天都在做有氧无氧运动,我的体重虽然下降了,为什么下降的很慢?
  4. 牛客 腾讯算法岗 笔试题整理
  5. ref与shallowRef区别
  6. 【高速公路休息站充电规划】c++
  7. oracle-sql书写
  8. 岁月如歌——记录那些曾感动过我的音乐
  9. mbes多波束回声测深仪基础知识
  10. Python弹球小游戏