Android text message filtering/batch deletion programs (Android 2.3-supported ~ 4.4)
This is a tool for text message filtering/batch deletion.
First:
1. Display All System text messages.
2. Create a filter (by number or by content)
3. Filter and batch Delete.
Main Code:
1. Get all SMS messages from the system
// Retrieve all system SMS 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. Add filter dialog box
// Create a 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 Rad IoButton 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 (Editor Info. TYPE_CLASS_TEXT) ;}}); builder. setView (view); builder. setPositiveButton ("OK", null); builder. setNegativeButton ("cancel", null); final AlertDialog dialog = builder. create (); dialog. show (); // when you click "OK", if the input is incorrect, a prompt is displayed. the dialog box is not closed. therefore, the common setPositiveButton cannot be used to listen to the dialog. getButton (AlertDialog. BUTTON_POSITIVE ). setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {String t Itle = titleET. getText (). toString (). trim (); String content = contentET. getText (). toString (). trim (); if (title. equals ("") {Toast. makeText (MainActivity. this, "Enter the title", Toast. LENGTH_SHORT ). show (); return;} if (content. equals ("") {Toast. makeText (MainActivity. this, "Enter the filtering content", Toast. LENGTH_SHORT ). show (); return;} dialog. dismiss (); // Add to database DatabaseHelper helper = new DatabaseHelper (MainActivity. thi S); // type: Filter mobile 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, "added successfully", Toast. LENGTH_SHORT ). show (); // filter filterSms (filterBean );}});}
3. Text message filtering
// Filter the qualified SMS 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. Delete selected SMS messages
// Delete the selected SMS 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 (","); // update the page smsList. remove (adapterList. get (I); adapterList. remove (I); map. delete (I); count ++ ;}} Toast. makeText (this, "successfully deleted" + count + "SMS", 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 ("delete SMS" + count + "messages ");}}). start ();}
5. Support for Android 4.4:
Android 4.4 adds SMS permission management. Users can select an "SMS program" as the default SMS program. Only the default SMS program can perform the "write" function of the SMS database.
To enable your programs to appear in the "default information program" list set by the system, you need to "declare" your programs in Manifest to have complete information functions, such as sending and receiving text messages/sending MMS, of course, these functions can not be implemented (as long as "declare ).
Download the source code (Android Studio project Module) (without points ):
Http://download.csdn.net/detail/books1958/8124193