首页 > 代码库 > Android版短信 筛选/批量删除 程序(支持Android 2.3~4.4)

Android版短信 筛选/批量删除 程序(支持Android 2.3~4.4)

这是一个短信筛选/批量删除的工具类程序。

首先上图:

1.展示全部系统短信。


2.新建筛选器(支持按号码筛选和按内容筛选)

3.筛选,批量删除。


主要代码:

1.获取系统全部短信

//获取系统全部短信
    private List<SmsBean> 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<SmsBean> smsList = new ArrayList<SmsBean>();
        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<SmsBean> adapterSmsList = new ArrayList<SmsBean>();
        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<SmsBean> 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 4.4上删除短信。对于Android 4.4以下的设备无需这些代码。-->
        <!--for Android 4.4 start-->
        <!-- BroadcastReceiver that listens for incoming SMS messages -->
        <receiver
            android:name=".kitkatsupport.SmsReceiver"
            android:permission="android.permission.BROADCAST_SMS">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_DELIVER" />
            </intent-filter>
        </receiver>

        <!-- BroadcastReceiver that listens for incoming MMS messages -->
        <receiver
            android:name=".kitkatsupport.MmsReceiver"
            android:permission="android.permission.BROADCAST_WAP_PUSH">
            <intent-filter>
                <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
                <data android:mimeType="application/vnd.wap.mms-message" />
            </intent-filter>
        </receiver>

        <!-- Activity that allows the user to send new SMS/MMS messages -->
        <activity android:name=".kitkatsupport.ComposeSmsActivity">
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <action android:name="android.intent.action.SENDTO" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data android:scheme="sms" />
                <data android:scheme="smsto" />
                <data android:scheme="mms" />
                <data android:scheme="mmsto" />
            </intent-filter>
        </activity>

        <!-- Service that delivers messages from the phone "quick response" -->
        <service
            android:name=".kitkatsupport.HeadlessSmsSendService"
            android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
                <category android:name="android.intent.category.DEFAULT" />

                <data android:scheme="sms" />
                <data android:scheme="smsto" />
                <data android:scheme="mms" />
                <data android:scheme="mmsto" />
            </intent-filter>
        </service>
        <!--Android 4.4 end-->

源码下载(Android Studio项目Module)(免积分):

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


Android版短信 筛选/批量删除 程序(支持Android 2.3~4.4)