Android版簡訊 篩選/大量刪除 程式(支援Android 2.3~4.4)

來源:互聯網
上載者:User

Android版簡訊 篩選/大量刪除 程式(支援Android 2.3~4.4)

這是一個簡訊篩選/大量刪除的工具類程式。

首先:

1.展示全部系統簡訊。


2.建立篩選器(支援按號碼篩選和按內容篩選)

3.篩選,大量刪除。


主要代碼:

1.擷取系統全部簡訊

//擷取系統全部簡訊    private List getAllSms() {        final String SMS_URI_ALL = "content://sms/";        Uri uri = Uri.parse(SMS_URI_ALL);        String[] projection = new String[]{"_id", "address", "body", "date"};        Cursor cursor = getContentResolver().query(uri, projection, null, null, "date desc");        int index_Id = cursor.getColumnIndex("_id");        int index_Address = cursor.getColumnIndex("address");        int index_Body = cursor.getColumnIndex("body");        int index_Date = cursor.getColumnIndex("date");        List smsList = new ArrayList();        while (cursor.moveToNext()) {            SmsBean bean = new SmsBean();            bean.setId(cursor.getInt(index_Id));            bean.setFromNum(cursor.getString(index_Address));            bean.setContent(cursor.getString(index_Body));            bean.setTime(cursor.getLong(index_Date));            smsList.add(bean);        }        cursor.close();        return smsList;    }

2.添加篩選器的對話方塊

//建立過濾器的對話方塊    private void showAddFilterDialog() {        AlertDialog.Builder builder = new AlertDialog.Builder(this);        builder.setTitle("添加過濾器");        View view = LayoutInflater.from(this).inflate(R.layout.add_filter_dialog, null);        final EditText titleET = (EditText) view.findViewById(R.id.add_filter_dialog_title);        final EditText contentET = (EditText) view.findViewById(R.id.add_filter_dialog_content);        final RadioButton filterPhoneNum = (RadioButton) view.findViewById(R.id.add_filter_dialog_phoneNum);        filterPhoneNum.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                contentET.setText("");                if (isChecked) {                    contentET.setInputType(EditorInfo.TYPE_CLASS_NUMBER);                } else {                    contentET.setInputType(EditorInfo.TYPE_CLASS_TEXT);                }            }        });        builder.setView(view);        builder.setPositiveButton("確認", null);        builder.setNegativeButton("取消", null);        final AlertDialog dialog = builder.create();        dialog.show();        //點擊確定按鈕時,如果輸入不正確,則進行提示,不關閉對話方塊.所以此處不能使用普通的setPositiveButton進行事件監聽        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                String title = titleET.getText().toString().trim();                String content = contentET.getText().toString().trim();                if (title.equals("")) {                    Toast.makeText(MainActivity.this, "請輸入標題", Toast.LENGTH_SHORT).show();                    return;                }                if (content.equals("")) {                    Toast.makeText(MainActivity.this, "請輸入篩選內容", Toast.LENGTH_SHORT).show();                    return;                }                dialog.dismiss();                //添加至資料庫                DatabaseHelper helper = new DatabaseHelper(MainActivity.this);                //type:篩選手機號-1,篩選簡訊內容-2                int type = filterPhoneNum.isChecked() ? 1 : 2;                FilterBean filterBean = new FilterBean(-1, title, content, type);                helper.addFilter(filterBean);                Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_SHORT).show();                //篩選                filterSms(filterBean);            }        });    }

3.簡訊篩選

//篩選合格簡訊    private void filterSms(FilterBean filterBean) {        if (smsList == null) {            return;        }        titleTV.setText(filterBean.getTitle());        List adapterSmsList = new ArrayList();        for (SmsBean sms : smsList) {            String filterContent = (filterBean.getType() == 1 ? sms.getFromNum() : sms.getContent())                    .replace(" ", "").replace("-", "");            if (checkSms(filterContent, filterBean.getContent())) {                adapterSmsList.add(sms);            }        }        adapter.setList(adapterSmsList);        adapter.getSelectedMap().clear();        adapter.notifyDataSetChanged();        ignoreChange = true;        selectAllCB.setChecked(false);        ignoreChange = false;    }
4.刪除已選擇的簡訊

//刪除已選擇的簡訊    private void deleteSelectedSms() {        List adapterList = adapter.getList();        SparseBooleanArray map = adapter.getSelectedMap();        final StringBuilder sb = new StringBuilder();        int count = 0;        for (int i = adapterList.size() - 1; i >= 0; i--) {            if (map.get(i)) {                sb.append(adapterList.get(i).getId());                sb.append(",");                //更新頁面                smsList.remove(adapterList.get(i));                adapterList.remove(i);                map.delete(i);                count++;            }        }        Toast.makeText(this, "成功刪除了" + count + "條簡訊", Toast.LENGTH_SHORT).show();        //刪除        new Thread(new Runnable() {            @Override            public void run() {                String SMS_URI_ALL = "content://sms/";                Uri uri = Uri.parse(SMS_URI_ALL);                String whereClause = "_id in(" + sb.substring(0, sb.length() - 1) + ")";                int count = getContentResolver().delete(uri, whereClause, null);                System.out.println("實際刪除簡訊" + count + "條");            }        }).start();    }

5.為實現對Android 4.4的支援:
Android 4.4增加了簡訊許可權的管理,使用者可以選擇一個“簡訊程式”作為預設的簡訊程式,而只有預設的簡訊程式可以執行簡訊資料庫“寫”的功能。
為了使自己的程式能夠出現在系統設定“預設資訊程式”的列表中從而供使用者選擇,需要在Manifest中“聲明”自己的程式具備完善的資訊功能,如收發簡訊/發多媒體訊息,當然,這些功能可以不實現(只要“聲明”了即可)。


                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                

源碼下載(Android Studio項目Module)(免積分):

http://download.csdn.net/detail/books1958/8124193


相關文章

聯繫我們

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