This is a tool class program for SMS filtering/bulk deletion.
First of all:
1. Display all system SMS.
2. New filter (supports filter by number and filter by content)
3. Filter, bulk Delete.
Main code:
1. Get the system all SMS
Get system all SMS 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. Adding a Filter dialog box
New Filter dialog box private void Showaddfilterdialog () {Alertdialog.builder Builder = new Alertdialog.builder (this); Builder.settitle ("add Filter"); 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 P ublic 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 ("confirm", null); Builder.setnegativebutton ("Cancel", null); Final Alertdialog Dialog = Builder.create (); Dialog.show (); When you click the OK button, if the input is incorrect, you are prompted without closing the dialog box. Therefore, you cannot use normal Setpositivebutton for event monitoring 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, "Enter title", Toast.length_short). Show (); Return } if (Content.equals (")") {Toast.maketext (Mainactivity.this, "Enter filter content", Toast.length_sh ORT). Show (); Return } Dialog.dismiss (); Add to Database Databasehelper helper = new Databasehelper (mainactivity.this); Type: Filter phone number-1, filter SMS content -2 int type = filterphonenum.ischecked ()? 1:2; Filterbean Filterbean = new Filterbean ( -1, title, content, type); Helper.addfilter (Filterbean); Toast.maketext (Mainactivity.this, "add Success", Toast.length_short). Show (); Screening filtersms (Filterbean); } }); }
3. SMS Filter
Filter for eligible SMS 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. Delete the selected SMS
Delete the selected SMS 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.g ET (i). GetId ()); Sb.append (","); Update page Smslist.remove (Adapterlist.get (i)); Adapterlist.remove (i); Map.delete (i); count++; }} toast.maketext (this, "successfully deleted" + count + "text message", Toast.length_short). Show (); Delete 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 ("Actual deletion of SMS" + Count + "bar"); }}). Start (); }
5. To enable Android 4.4 support:
Android 4.4 Added SMS access management, users can choose a "short message program" As the default SMS program, and only the default SMS program can perform the text message database "write" function.
In order to make their programs appear in the system settings "default information program" list so that users can choose, you need to "declare" in Manifest's own programs have complete information functions, such as sending and receiving SMS/MMS, of course, these functions may not be implemented (as long as "declare").
<!--The following code to implement delete SMS on Android 4.4. This code is not required for devices under Android 4.4. -<!--for Android 4.4 start--> <!--Broadcastreceiver, 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> &l T;/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:m Imetype= "Application/vnd.wap.mms-message"/> </intent-filter> </receiver> <!--Activity that allows the user to send new SMS/MMS messages---<activity android:name= ". Kitkatsuppo Rt. Composesmsactivity "> <intent-filter> <action android:name=" Android.intent.action.SEN D "/> <action android:name=" Android.intent.action.SENDTO "/> <category android:n Ame= "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&G T </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=" and Roid.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&G T </service> <!--Android 4.4 end-->
SOURCE Download (Android Studio Project module) (Free points):
http://download.csdn.net/detail/books1958/8124193
Android SMS Filter/Bulk Delete program (Android 2.3~4.4 supported)