標籤:
本人一直有一個需求,想要手機自動攔截黑名單裡連絡人的資訊並自動刪除這些簡訊,手機管家之類的軟體可以攔截但是沒找到能刪除這些簡訊的,於是就萌生了想自己寫一個android軟體的想法。
加上物聯網的興起,安卓裝置開發肯定前景很好。 隨後又得知 安卓開發可以用java語言,就用eclipse,呵呵,本人還是有些java基礎的,於是乎,本人就開始撲騰了。
把eclipse sdk adt搭建好之後,本人就開始了開發這款自動攔截並刪除黑名單簡訊的軟體。都已經實現。
在編寫過程中,刪除簡訊的模組倒是坑了我挺長時間,原因是函數調用的時候參數值沒寫好,網上的版本多,當時crt+v的坑了我好久,現在把刪除簡訊的模組發下。在My Phone上試過,親測可用的。
public void deleteSMS( String smscontent) { try { // 準備系統簡訊收信箱的uri地址 Uri uri = Uri.parse("content://sms/inbox");// 收信箱 // 查詢收信箱裡所有的簡訊 String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" ,"thread_id"};//"_id", "address", "person",, "date", "type String where = " address = ‘10001‘"; Cursor curs = getContentResolver().query(uri, projection, where,null, "date desc"); if(curs.moveToFirst()) { do{ // String phone = // isRead.getString(isRead.getColumnIndex("address")).trim();//擷取發信人 String body =curs.getString(curs.getColumnIndex("body")).trim();// 擷取資訊內容 if (body.contains(smscontent)) { int id = curs.getInt(curs.getColumnIndex("_id")); getContentResolver().delete(Uri.parse("content://sms/"),"_id=?",new String[]{ String.valueOf(id)}); } }while (curs.moveToNext()); } curs.close(); } catch (Exception e) { e.printStackTrace(); } }
演算法思路是:把寄件者是10001的所有簡訊中,包含某個字串的簡訊給刪除掉,想要把簡訊內容換成別的 _id、寄件者之類的修改下函數就可以了啊。
調用這個模組的函數我是用一個button的 onClickListener來實現的。
BtnDelMsg.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub deleteSMS("尊敬的客戶,截"); } });
一個比較清晰簡潔的參考連結
http://wx1985113.iteye.com/blog/1231094
總結:
刪除簡訊函數參數的3種寫法
getContentResolver().delete(Uri.parse("content://sms/"),"_id=?",new String[]{ String.valueOf(id)});
getContentResolver().delete(Uri.parse("content://sms/"),"_id="+id,null);
getContentResolver().delete(Uri.parse("content://sms/"+id),null,null);
Android開發刪除簡訊