android 添加手機簡訊,擷取手機簡訊,刪除手機簡訊和修改手機簡訊

來源:互聯網
上載者:User

標籤:des   android   c   style   class   blog   

注意添加許可權:

    <uses-permission android:name="android.permission.READ_SMS"></uses-permission>    <uses-permission android:name="android.permission.WRITE_SMS"></uses-permission>

代碼如下:

//更新簡訊private void UpdateSMS(){/*update支援的協議有很多:SMS_RAW_MESSAGE    SMS_STATUS_PENDING    SMS_ALL    SMS_FAILED    SMS_QUEUED    SMS_INBOX    SMS_SENT    SMS_DRAFT    SMS_OUTBOX    SMS_CONVERSATIONS    SMS_ALL_ID    SMS_INBOX_ID    SMS_FAILED_ID    SMS_SENT_ID    SMS_DRAFT_ID    SMS_OUTBOX_ID    SMS_CONVERSATIONS_ID    SMS_STATUS_ID*/ContentValues cv = new ContentValues();    cv.put("thread_id", "2");    cv.put("address", "00000");    cv.put("person", "11");    //cv.put("date", "11111111");  cv.put("body", "hello,modify content333333333");getContentResolver().update(Uri.parse("content://sms/sent/3"), cv, null,null);  //getContentResolver().update(Uri.parse("content://sms/sent/3"), cv, "id=?",new String[]{"3"});   }//刪除簡訊private void DeleteSMS(){/** 其中,delete方法中支援的協議為:* SMS_ALL 根據參數中的條件刪除sms表資料 * SMS_ALL_ID 根據_id刪除sms表資料* SMS_CONVERSATIONS_ID 根據thread_id刪除sms表資料,可以帶其它條件 * SMS_RAW_MESSAGE 根據參數中的條件刪除 raw表* SMS_STATUS_PENDING 根據參數中的條件刪除 sr_pending表* SMS_SIM 從Sim卡上刪除資料* 試一下SMS_CONVERSATIONS_ID:"content://sms/conversations/3 ",刪除thread_id="3", _id="5"的資料 在eclipse中的Emulator* Control中,以13800給模擬器發送三條資料,然後以13900發送一條* this.getContentResolver().delete* (Uri.parse("content://sms/conversations/3"), "_id=?", new* String[]{"5"}); 成功刪除一條資料。* 在資料庫中每個寄件者的thread_id雖然一樣,但不是固定的,如果把一個寄件者的全部資料刪除掉,* 然後換一個新號碼傳送簡訊時,thread_id是以資料庫中最大的id+1賦值的。*//*看了一下android的原始碼,sms支援的協議有: sURLMatcher.addURI("sms", null, SMS_ALL); sURLMatcher.addURI("sms", "#", SMS_ALL_ID); sURLMatcher.addURI("sms", "inbox", SMS_INBOX); sURLMatcher.addURI("sms", "inbox/#", SMS_INBOX_ID); sURLMatcher.addURI("sms", "sent", SMS_SENT); sURLMatcher.addURI("sms", "sent/#", SMS_SENT_ID); sURLMatcher.addURI("sms", "draft", SMS_DRAFT); sURLMatcher.addURI("sms", "draft/#", SMS_DRAFT_ID); sURLMatcher.addURI("sms", "outbox", SMS_OUTBOX); sURLMatcher.addURI("sms", "outbox/#", SMS_OUTBOX_ID); sURLMatcher.addURI("sms", "undelivered", SMS_UNDELIVERED); sURLMatcher.addURI("sms", "failed", SMS_FAILED); sURLMatcher.addURI("sms", "failed/#", SMS_FAILED_ID); sURLMatcher.addURI("sms", "queued", SMS_QUEUED); sURLMatcher.addURI("sms", "conversations", SMS_CONVERSATIONS); sURLMatcher.addURI("sms", "conversations/*", SMS_CONVERSATIONS_ID); sURLMatcher.addURI("sms", "raw", SMS_RAW_MESSAGE); sURLMatcher.addURI("sms", "attachments", SMS_ATTACHMENT); sURLMatcher.addURI("sms", "attachments/#", SMS_ATTACHMENT_ID); sURLMatcher.addURI("sms", "threadID", SMS_NEW_THREAD_ID); sURLMatcher.addURI("sms", "threadID/*", SMS_QUERY_THREAD_ID); sURLMatcher.addURI("sms", "status/#", SMS_STATUS_ID); sURLMatcher.addURI("sms", "sr_pending", SMS_STATUS_PENDING); sURLMatcher.addURI("sms", "sim", SMS_ALL_SIM); sURLMatcher.addURI("sms", "sim/#", SMS_SIM);*///getContentResolver().delete(Uri.parse("content://sms/failed/3"), "_id=?", new String[]{"99"});   getContentResolver().delete(Uri.parse("content://sms/#"),"_id=?", new String[]{"99"});  }//添加簡訊private void AddSMS(){/** insert支援的協議:* * SMS_ALL * SMS_INBOX * SMS_FAILED * SMS_QUEUED * SMS_SENT * SMS_DRAFT* SMS_OUTBOX * SMS_RAW_MESSAGE * SMS_STATUS_PENDING * SMS_ATTACHMENT* SMS_NEW_THREAD_ID* * 向sms表插入資料時,type是根據協議來自動化佈建,* 如果傳入的資料中沒有設定date時,自動化佈建為當前系統時間;非SMS_INBOX協議時,read標誌設定為1* SMS_INBOX協議時,系統會自動查詢並設定PERSON threadId為null或者0時,系統也會自動化佈建* * 一直為造不了"發送失敗"的郵件而發愁,現在來做一個: content://sms/failed*/String id=null;ContentValues cv = new ContentValues();   for(int i = 0;i<5;i++){id.valueOf(i);cv.put("_id", id);    cv.put("thread_id", "0");    cv.put("address", "9999");    cv.put("person", "888");    //cv.put("date", "9999"); cv.put("protocol", "0"); cv.put("read", "1"); cv.put("status", "-1"); cv.put("body", "@@@@@@@@@"); getContentResolver().insert(Uri.parse("content://sms/sent"), cv);cv.clear();}  }//查詢簡訊內容private void GetSMS(){/** content://sms/inbox 收件匣 content://sms/sent 已發送* content://sms/draft 草稿 content://sms/outbox 寄件匣* content://sms/failed 發送失敗 content://sms/queued 待發送列表* 資料庫中sms相關的欄位如下: *  _id 一個自增欄位,從1開始 thread_id 序號,同一發信人的id相同*            同一個連絡人的thread_id是一樣的,通過遍曆thread id和對比就能知道一個連絡人有幾條簡訊;*            當然了,Threads表裡面有個message_out的欄位也是顯示有條連絡人的,但是Threads表還沒有找到可以讀取的辦法;*            *  address 寄件者手機號碼 *  person 連絡人清單裡的序號,陌生人為null ;即rawcontact表的contact_id,或者contact表的_id;*            可通過此id關聯連絡人;但是person對應結果不穩定,應該使用PhoneLookup.CONTENT_FILTER_URI*  date 發件日期*  protocol 協議,分為: 0 SMS_RPOTO, 1 MMS_PROTO *  read 是否閱讀 0未讀, 1已讀*  status 狀態 -1接收,0 complete, 64 pending, 128 failed *  type ALL = 0; INBOX = 1; SENT =2; DRAFT = 3; OUTBOX = 4; FAILED = 5; QUEUED = 6; *  body 簡訊內容*  service_center 簡訊服務中心號碼編號 *  subject 簡訊的主題 *  reply_path_present  TP-Reply-Path locked*/Log.i("SMS","開始擷取簡訊");String[] projection = new String[] {          "_id",          "address",          "person",          "date",          "protocol",          "read",          "status",          "body",          "service_center",          "subject"         };Cursor cur = getContentResolver().query(Uri.parse("content://sms"), projection, null,null, "date desc"); StringBuilder str=new StringBuilder();        if (cur.moveToFirst()) {        String name;             String phoneNumber;                   String sms;            long date;            String id;            int idColumn = cur.getColumnIndex("_id");            int nameColumn = cur.getColumnIndex("person");            int phoneColumn = cur.getColumnIndex("address");            int smsColumn = cur.getColumnIndex("body");            int dateColumn=cur.getColumnIndex("date");            do {                // Get the field values                name = cur.getString(nameColumn);                             phoneNumber = cur.getString(phoneColumn);                sms = cur.getString(smsColumn);                date = cur.getLong(dateColumn);                String temp = long2String(date);                id = cur.getString(idColumn);                //date = String2Long(date);                str.append("{");                str.append(name+",");                str.append(phoneNumber+",");                str.append(sms+",");                str.append(temp+",");                str.append(id);                str.append("}");                                                if (null==sms)                sms="";            } while (cur.moveToNext());        }        else        {            str.append("no result!");        }        Log.i("SMS",str.toString());        Toast.makeText(context, str.toString(), Toast.LENGTH_SHORT).show();        System.out.print("簡訊結果:"+str.toString());        cur.close();}private String long2String(long time) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss:SSSS");return sdf.format(new Date(time));}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.