`
byandby
  • 浏览: 1690671 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

android 访问SMS收件箱

阅读更多
    访问 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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics