Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android版短信 篩選/批量刪除 程序(支持Android 2.3~4.4)

Android版短信 篩選/批量刪除 程序(支持Android 2.3~4.4)

編輯:關於Android編程

這是一個短信篩選/批量刪除的工具類程序。

首先上圖:

1.展示全部系統短信。

\

2.新建篩選器(支持按號碼篩選和按內容篩選)<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGltZyBzcmM9"/uploadfile/Collfiles/20141106/20141106081335131.png" alt="\">

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


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved