Android開發刪除簡訊

來源:互聯網
上載者:User

標籤:

本人一直有一個需求,想要手機自動攔截黑名單裡連絡人的資訊並自動刪除這些簡訊,手機管家之類的軟體可以攔截但是沒找到能刪除這些簡訊的,於是就萌生了想自己寫一個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開發刪除簡訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.