访问 SMS收件箱是另一个常见的需求。首先,需要将读取 SMS 的权限
<uses-permission android:name="android.permission.READ_SMS"/>
添加到描述文件中。添加此权限后就可以读取SMS收件箱中的 短消息了。
要读取 SMS 消息,必须对SMS收件箱执行查询,下面是我们的 代码清单。
布局文件
<?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/row"
android:layout_width="180dip"
android:layout_height="30dip"
android:textSize="10pt"
android:singleLine="true"
/>
</LinearLayout>
我们自定义的ListActivity
package xiaohang.zhimeng;
import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;
public class SMSINboxDemo extends ListActivity {
private ListAdapter adapter;
private static final Uri SMS_INBOX = Uri.parse("content://sms/inbox");
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor c = getContentResolver()
.query(SMS_INBOX, null, null, null, null);
startManagingCursor(c);
String[] columns = new String[] { "body" };
int[] names = new int[] { R.id.row };
adapter = new SimpleCursorAdapter(this, R.layout.main, c, columns,
names);
setListAdapter(adapter);
}
}
上面的代码打开 SMS收件箱并创建了一个列表,列表中的每一项都包含 SMS消息的正文部分。我们的布局文件就只包含了一个简单的 TextView,它包含列表项中每条消息的正文。要获得消息列表,可以创建指向 SMS收件箱的 URI (content://sms/inbox),然后执行简单查询。然后对 SMS消息的正文进行过滤,并设置 ListActivity的列表 适配器。执行上面的代码将看到收件箱中的消息 ,效果图 如下。
请大家确保自己的收件箱中有 SMS消息。
因为可以访问SMS收件箱,所以将能够访问其他与SMS 相关的文件夹,比如已发送文件夹或草稿箱文件夹。访问收件箱与访问其它文件夹的唯一区别就在于所指定的 URI。例如,可以对 content://sms/sent 执行查询来访问已发送的文件夹。以下是完整的 SMS文件夹列表和每个文件夹的URI。
所有文件夹:content://sms/all
收件箱:content://sms/inbox
已发送:content://sms/sent
草稿:content://sms/draft
发件箱:content://sms/outbox
发送失败:content://sms/failed
排队消息:content://sms/queued
未送达:content://sms/undelivered
对话:content://sms/conversations
源码已上传
- 大小: 22.9 KB
分享到:
相关推荐
Android SMS 发送 接收及格式
基于android手机系统的 短信 sms 发送接收
android4.0 sms源码, 很多新特性, 可以参考啊。 老乔年轻时说, 有名的艺术家是抄袭, 伟大的艺术家是偷窃。 what are you waiting for ? let's do it !
Programe SMS on Android.
android 通讯录 SMS 支付SIM卡和手机中的SIM联系人的操作
android-sms-gateway, 在Android上,运行简单的短信网关 Android短信网关应用这是一个简单的小应用程序,可以将Google云消息( GCM ) 事件转换为你手机上的outgoing消息。正在启动按照 ...
能得到收件箱中的短信数量。大家手机编程的看看吧
本程序的主要功能是短信语音播报,主要运行于android平台,能自动播报预设联系人发来的短信,能给我们的生活带来很大的方便。
linphone android app 短消息版
Android之SMS短信和Contacts联系人实例
android sms 工具 可以测试使用
Android 发送短信并且保存短信记录。
android短信监听器,可转发收到的短信。支持短信设置接收机号码。仅供技术交流。-android SMS spy,transfer SMS to a receiver。The receiver can be set by commmand SMS
Android 短信SMS发送代码流程
Android SMS 短信内容显示在一个 泡泡 形状的窗口中.zip Android SMS 短信内容显示在一个 泡泡 形状的窗口中.zip Android SMS 短信内容显示在一个 泡泡 形状的窗口中.zip Android SMS 短信内容显示在一个 泡泡 形状...
android 收取短信时间,内容等。。。。。。
对于学习android短信信息知识的问题,有一些小型的解决,可以参考
android手机提取短信,sqlite3数据库开发源码
android demo,SMS短信通知。android demo,SMS短信通知。