In my previous articles, I wrote examples of real-time text message retrieval and text message (Session) deletion.
Related links:
SMS information Real-time acquisition: http://blog.csdn.net/etzmico/article/details/6860692
SMS (Session) Deletion: http://blog.csdn.net/etzmico/article/details/7077123
Therefore, the code for the above two methods will not be written in this article. You can click the link to view the code.
This time they are combined, but some small details need to be noted,
First, declare contentresolver in the activity class and define it in oncreate.
static ContentResolver cr;cr = getContentResolver();
Secondly, in the short message receiving mechanism of Android, the system first listens to the message, then stores the message in the inbox, and prompts in the notification. The system does not immediately store the message in the inbox and prompt after listening to the text message, therefore, pay special attention to the deletion.
New timer (). Schedule (New timertask () {@ overridepublic void run () {// Delete SMS code}, 5000 );
We can make such an extension because there is no information in the inbox when the onreceive method in the text message receiving trigger class is received, even if the priority is minimized, it will not help.
Therefore, we can perform a delay operation on the method for deleting text messages. According to my own test, five seconds is wonderful, because many machines will become slow in system operation for various reasons ...... If the machine gets stuck again, you can add more events ...... Here, 5000 is 5000 milliseconds, that is, 5 seconds.
In addition, to avoid text message deletion, we can determine if and add conditions for the method.
Stringbuffer smsaddress. append (message. getdisplayoriginatingaddress (); // the sender's phone number stringbuffer smscontent. append (message. getdisplaymessagebody (); // SMS content
In this way, the text message will not be deleted by mistake.
At last, the system prompts three permissions in manifest. xml.
<uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.WRITE_SMS" /> <uses-permission android:name="android.permission.READ_SMS" />
Demo download: http://download.csdn.net/detail/etzmico/3975608