读取android系统短信

 android读取短信比较简单,我们可以分类读取短信,比如所有短信,收件箱短信,发件箱短信,草稿笨短信

注:读取短信时,一定要指明要读哪些字段

 

package com.test.testmsg;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.telephony.SmsMessage;
import android.util.Log;

public class MainActivity extends Activity {/**
     * 所有的短信
     */
    public static final String SMS_URI_ALL = "content://sms/";
    /**
     * 收件箱短信
     */
    public static final String SMS_URI_INBOX = "content://sms/inbox";
    /**
     * 发件箱短信
     */
    public static final String SMS_URI_SEND = "content://sms/sent";
    /**
     * 草稿箱短信
     */
    public static final String SMS_URI_DRAFT = "content://sms/draft";
/*
*    _id:短信序号,如100   
*   thread_id:对话的序号,如100,与同一个手机号互发的短信,其序号是相同的   
*   address:发件人地址,即手机号,如+8613811810000   
*   person:发件人,如果发件人在通讯录中则为具体姓名,陌生人为null   
*   date:日期,long型,如1256539465022,可以对日期显示格式进行设置   
*   protocol:协议0SMS_RPOTO短信,1MMS_PROTO彩信      read:是否阅读0未读,1已读   
*   status:短信状态-1接收,0complete,64pending,128failed   
*   type:短信类型1是接收到的,2是已发出      body:短信具体内容   
*   service_center:短信服务中心号码编号,如+8613800755500
*/
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getLocalMsg();
    }


    private void getLocalMsg() {
        //短信中要查询的字段
        String[] projection = new String[]{"address","date","body"};
        
        //Long above = 
        SimpleDateFormat dateFormat = new SimpleDateFormat(   
                "yyyy-MM-dd hh:mm:ss"); 
        Date d = null;
        try {
             d = dateFormat.parse("2014-03-18 00:00:00");
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Cursor cursor = this.managedQuery(
                Uri.parse(SMS_URI_INBOX), 
                projection, 
                "date > ?",
                new String[]{d.getTime()+""}, 
                "date desc");
        
        if(cursor != null){
            //发送者的号码
            int addressColumn = cursor.getColumnIndex("address");
            //int dateColumn = cursor.getColumnIndex("date");
            //短信内容
            int bodyColumn = cursor.getColumnIndex("body");
            while(cursor.moveToNext()){
                //String date = cursor.getString(dateColumn);
                String address = cursor.getString(addressColumn);
                String body = cursor.getString(bodyColumn);
                Log.i("KwokTag", address + " " + body);
            }
            cursor.close();
            
        }
    }
}

 

posted @ 2014-03-18 17:28  Vincent_Guo  阅读(1151)  评论(0编辑  收藏  举报