讀取android系統短信

 android讀取短信比較簡單,咱們能夠分類讀取短信,好比全部短信,收件箱短信,發件箱短信,草稿笨短信java

注:讀取短信時,必定要指明要讀哪些字段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();
            
        }
    }
}
相關文章
相關標籤/搜索