android 发短信 调到联系人

原文找不到了。看了别人的一些。自己加了一点。做一个代码备份。自己看就可以了。先上一下图吧。看看一效果。


通过上边选择联系人。把选择过的联系显示到EditView 中。而发送短信时。可以获取联系人的电话号码。

发短信的的类SendmessageActivity.java

package com.hkrt.action;import java.util.List;import android.app.Activity;
import android.app.PendingIntent;
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.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;public class SendmessageActivity extends Activity {private EditText editNum;private EditText editContext;private String phoneNum;private int LIANXR=10;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);editNum =(EditText)this.findViewById(R.id.input_num);editContext =(EditText)this.findViewById(R.id.input_content);Button button =(Button)this.findViewById(R.id.button);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {
//				String phoneNum = editNum.getText().toString();String context = editContext.getText().toString();SmsManager sms = SmsManager.getDefault();//发送信息的管理器PendingIntent sentIntent = PendingIntent.getBroadcast(SendmessageActivity.this, 0, new Intent(), 0);//PendingIntent也是一个意图//把短信内容进行每70个字节分割List<String> conts= sms.divideMessage(context);for(String str:conts){sms.sendTextMessage(phoneNum, null, str, sentIntent, null);}//信息发送完后提示Toast.makeText(SendmessageActivity.this, "短信发送完成", Toast.LENGTH_LONG).show();}});Button btnContact  = (Button)this.findViewById(R.id.btnContact);btnContact.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(Intent.ACTION_PICK,  ContactsContract.Contacts.CONTENT_URI);startActivityForResult(intent, LIANXR);}});}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);ContentResolver resol = getContentResolver();if (resultCode == Activity.RESULT_OK) {String name;Uri contactData = data.getData();Cursor c = managedQuery(contactData, null, null, null, null);c.moveToFirst();name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));editNum.setText(name);// 取得联系人id,每个条目都有一个唯一的id(主键)String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));// 取得联系人的号码Cursor phone = resol.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);while (phone.moveToNext()) {phoneNum = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));System.out.println("phonum:"+phoneNum);}}}}
main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent">
<TextView  android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello"/><EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/input_num"/><Button android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="contact"android:id="@+id/btnContact"/><TextView  android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/input_context"/><EditText android:layout_height="wrap_content"android:layout_width="fill_parent"android:id="@+id/input_content" android:minLines="3"android:gravity="top"/><Button android:text="@string/button"android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button"/></LinearLayout>
调用联系人ContactPick.java

package com.hkrt.action;import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;public class ContactPick extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Intent orgIntent = getIntent();Uri queryUri = orgIntent.getData();final Cursor c = managedQuery(queryUri,null,null,null,null);String[] fromColumns = new String[] { ContactsContract.Contacts.DISPLAY_NAME };int[] toLayoutIDs = new int[] { R.id.itemTextView };SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.listitemlayout, c, fromColumns, toLayoutIDs);ListView lv = (ListView) findViewById(R.id.contactListView);lv.setAdapter(adapter);lv.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int pos,long id) {c.moveToPosition(pos);int rowId = c.getInt(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));Uri outURI = Uri.parse(ContactsContract.Contacts.CONTENT_URI .toString()+ rowId);Intent outData = new Intent();outData.setData(outURI);setResult(Activity.RESULT_OK, outData);finish();}});}}

对应的两个xml .contact.xml 和listitemlayout.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><ListView android:id="@+id/contactListView"android:layout_width="fill_parent"android:layout_height="wrap_content" />
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:id="@+id/itemTextView" android:layout_width="wrap_content"android:layout_height="wrap_content" android:padding="10px"android:textSize="16px" android:textColor="#FFF" /></LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.hkrt.action"android:versionCode="1"android:versionName="1.0"><uses-sdk android:minSdkVersion="8" /><application android:icon="@drawable/send" android:label="@string/app_name"><uses-library android:name="android.test.runner" /><activity android:name=".SendmessageActivity"  android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".ContactPick" android:label="@string/app_name"><action android:name="android.intent.action.PICK" /><category android:name="android.intent.category.DEFAULT" /></activity></application><instrumentation android:name="android.test.InstrumentationTestRunner"android:targetPackage="com.hkrt.action" android:label="Tests for My App" /><uses-permission android:name="android.permission.SEND_SMS"/><uses-permission android:name="android.permission.READ_CONTACTS" /><uses-permission android:name="android.permission.WRITE_CONTACTS" /></manifest>

demo 目录图:





转载于:https://www.cnblogs.com/java20130726/archive/2012/01/09/3218322.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/410147.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

常用方法 DataTable转换为Entitys

备注&#xff1a;摘自网上 有附地址 public static List<T> DataTableToEntities<T>(this DataTable dt) where T : class, new(){if (null dt || dt.Rows.Count 0) { return null; }List<T> entities new List<T>();List<string> columnName…

[html] 使用history路由方式时,你有自己动手配置过服务器端吗?为什么要配服务器端?怎么配?

[html] 使用history路由方式时&#xff0c;你有自己动手配置过服务器端吗&#xff1f;为什么要配服务器端&#xff1f;怎么配&#xff1f; history路由会请求服务器&#xff0c;因此需要服务器配合返回一个固定的index.html页面 以nginx配置为例&#xff1a; location / { try…

优麒麟桌面闪烁_稳定性持续增强,优麒麟 19.10.1 发布

优麒麟开源操作系统通过研发用户友好的桌面环境以及特定需求的应用软件&#xff0c;为全球用户提供更稳定更易用的 Linux 开源桌面操作系统。此次发布的优麒麟 19.10.1 版本为更新版本&#xff0c;内核版本升级至5.3.0.19.22&#xff0c; 火狐浏览器升级至70.0 版本&#xff0c…

智能机器人及其应用ppt课件_机器人视觉技术在建筑智能化生产中的应用

近年来&#xff0c;随着工业智能化的发展&#xff0c;有许多智能施工机器人走向了建筑领域&#xff0c;但是这些机器人在建筑行业相关部件产线推进过程中&#xff0c;发现传统的机器人应用方法不能很好的满足实际生产的需求。例如建筑行业的钢结构部件&#xff0c;都是些大型且…

云计算第二阶段shell脚本

pstree           #查看进程树 cat /etc/shells           #查看系统安装的所有shell解释器 yum -y install ksh  #安装新的解释器 一、shell脚本的结构 1、声明解释器 #!/bin/bash 2、注释脚本的…

[html] 怎样在文本框中禁用中文输入法?

[html] 怎样在文本框中禁用中文输入法&#xff1f; 用文本框的 ime-mode css 属性松开键时检查文本框的的值&#xff0c;只保留 Unicode 编码在 0 &#xff5e; 255 的字符把所有双字节字符替换为空把中文字符替换为空个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知…

SQLSERVER复制订阅中的数据库版本选择

设想一种场景&#xff1a; 两台发布SQL&#xff1a;数据库版本分别是SQL2005,SQL2008一台分发SQL: 数据库版本SQL2005一台订阅SQL: 版本待定 问 在这种情况下订阅服务器应该采用哪种版本的SQLSERVER? 2005还是2008&#xff1f; 这里假定一个前题&#xff1a;发布上的SQL2008并…

做系统ghost步骤图解_用好这工具,小孩都能会重装系统!

之前“事儿哥”曾给小伙伴们介绍过一款bios模拟器&#xff0c;(点击查看《比游戏模拟器罕见一百倍&#xff01;99%的人没见过&#xff01;真实用&#xff01;》)&#xff0c;让你可以轻松模拟操作bios&#xff0c;而不影响真实的电脑&#xff0c;不怕把电脑弄坏了&#xff01;今…

python一些方便excel行操作的函数(一)

import collections class headhandler():def __init__(self,mylist):self.mystorage{}self.mylist mylistdef delempty(self):去除重复:return:while "" in self.mylist:self.mylist.remove("")def formatmydata(self,i):try:ii.replace("&#xff…

nginx php空白页 fastcgi_param

原文地址&#xff1a;http://hi.baidu.com/%CA%E6%B7%F4%BC%D1%B2%C5%CA%C7%CD%F5%B5%C0/blog/item/77e622509c15dd06377abe4e.html 今天安装完nginx 后 发现html页面能正常浏览 但是php文件的页面打开后是一篇空白 看php-fpm日志 看nginx日志都没找到问题 上网搜索了半天终于解…

[html] 如何让<p>测试 空格</p>这两个词之间的空格变大?

[html] 如何让测试 空格这两个词之间的空格变大&#xff1f; 这边有这么两种方法&#xff1a;通过给p标签设置word-spacing&#xff0c;将这个属性设置成自己想要的值。将这个空格用一个span标签包裹起来&#xff0c;然后设置span标签的letter-spacing或者word-spacing。我分别…

git 为什么不能断点_跟我一起学docker(九)--持续系统集成了解下git

什么是持续集成&#xff1f;持续集成(Continuous integration&#xff0c;简称CI)。根据敏捷大师Martin Fowler的定义&#xff0c;“持续集成是一种软件开发实践。在持续集成中&#xff0c;团队成员频繁集成他们的工作成果&#xff0c;一般每人每天至少集成一次&#xff0c;也可…

selenium定位元素的方法_selenium定位元素之冻结窗口

我们在使用selenium进行元素定位的时候&#xff0c;会发现有很多需要操作的元素是随着鼠标移动与移出才会显示与隐藏&#xff0c;不能通过页面窗口实时看到自己的定位是否准确。这就给我们定位工作带来了一些困扰。这种情况下我们可以采用冻结当前窗口方式&#xff0c;以达到自…

DB Query Analyzer中的事务管理在DB2中的应用

DB Query Analyzer中的事务管理在DB2中的应用 马根峰 ( 广东联合电子收费股份有限公司, 广州 510300) 摘要 事务控制是数据库应用系统中的关键技术之一&#xff0c;概述了事务控制的概念以及《DB Query Analyzer》中的事务控制&#xff0c;以一个具体的实例&#xff0c;给…

servlet返回数据给html_Servlet 简介

Servlet 简介 博客说明 文章所涉及的资料来自互联网整理和个人总结&#xff0c;意在于个人学习和经验汇总&#xff0c;如有什么地方侵权&#xff0c;请联系本人删除&#xff0c;谢谢&#xff01; 简介 Java Servlet 是运行在 Web 服务器或应用服务器上的程序&#xff0c;它是作…

linux 64位 寻址空间_Ubuntu 20.04(64位)如何配置gcc-3.4用于编译linux-0.11

首先下载gcc-3.4地址&#xff1a;http://old-releases.ubuntu.com/ubuntu/pool/main/g/gcc-3.4/下载三个文件&#xff0c;分别是cpp-3.4_3.4.6-6ubuntu2_amd64.debgcc-3.4-base_3.4.6-6ubuntu2_amd64.debgcc-3.4_3.4.6-6ubuntu2_amd64.deb也可打开想要保存到的文件夹后&#xf…

[html] 如何使用普通元素拥有像textarea元素一样缩放?

[html] 如何使用普通元素拥有像textarea元素一样缩放&#xff1f; resize:both;个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xff0c; 但坚持一定很酷。欢迎大家一起讨论 主目录 与歌谣一起通关前端面试题

python瞎练

需求&#xff1a;有不规则列表 singlelist3 [ 总计, 每吨人工&#xff1a;, 总人工, 1748.07, 金额]&#xff0c;如果当前元素为字符串且该元素的下一个相邻位置仍为字符串&#xff0c;那么请在该元素后面插入数字0&#xff0c;如同 singlelist3 [ 总计,0.00&#xff0c; 每吨…

发布一个jQuery插件:formStorage

中午休息时,没有睡意,没事写了个jquery插件:formStorage. 原理很简单,通过本地存储机制(userData或者localStorage),存储表单中元素的状态到本地. 需要时可以把所存储的状态还原到表单元素上. 其中也用了json数据格式, 之前想对插件中所需的功能,从零开始写,但是觉得太冗余,没必…

简述python是怎么处理异常的-Python异常处理知识点总结

python提供了两个非常重要的功能来处理python程序在运行中出现的异常和错误。你可以使用该功能来调试python程序。 异常处理: 本站Python教程会具体介绍。 断言(Assertions):本站Python教程会具体介绍。 python标准异常 异常名称 描述 BaseException 所有异常的基类 SystemExit…