Android intercepts and deletes the SMS.

Source: Internet
Author: User

Directly give the code:

Package COM. ZTE. android. greenweb. launcher. service; import Org. JSON. jsonexception; import Org. JSON. jsonobject; import android. content. broadcastreceiver; import android. content. context; import android. content. intent; import android. database. cursor; import android.net. uri; import android. OS. bundle; import android. telephony. smsmessage; public class interceptsmsreciever extends broadcastreceiver {// broadcast message type public static final string sms_received_action = "android. provider. telephony. sms_received "; @ overridepublic void onreceive (context, intent) {string msgcontent =" "; string functiontype =" "; bundle = intent. getextras (); object messages [] = (object []) bundle. get ("PDUS"); smsmessage [] = new smsmessage [messages. length]; for (INT n = 0; n <messages. length; n ++) {smsmessage [N] = smsmessage. createfrompdu (byte []) messages [N]); msgcontent = smsmessage [N]. getmessagebody (); try {jsonobject JSON = new jsonobject (msgcontent); functiontype = JSON. getstring ("functiontype"); If (functiontype. equalsignorecase ("timelymanage") // {string opennet = JSON. getstring ("actiontype"); intent in = new intent (); In. putextra ("opennet", opennet); context. sendbroadcast (intent); deletesms (context, msgcontent); this. abortbroadcast () ;}} catch (jsonexception e) {e. printstacktrace () ;}} public void deletesms (context, string smscontent) {try {// uri of the system sms inbox URI uri = Uri. parse ("content: // SMS/inbox"); // inbox // query all text messages in the inbox. cursor isread = context. getcontentresolver (). query (Uri, null, "Read =" + 0, null, null); While (isread. movetonext () {// string phone = // isread. getstring (isread. getcolumnindex ("Address ")). trim (); // get the sender string body = isread. getstring (isread. getcolumnindex ("body ")). trim (); // obtain the information content if (body. equals (smscontent) {int id = isread. getint (isread. getcolumnindex ("_ id"); context. getcontentresolver (). delete (URI. parse ("content: // SMS"), "_ id =" + id, null) ;}} catch (exception e) {e. printstacktrace ();}}}

<! -- Register the consumer er and set the priority --> <consumer er Android: Name = "com. ZTE. android. greenweb. launcher. service. interceptsmsreciever "Android: exported =" true "> <intent-filter Android: Priority =" 1000 "> <action Android: Name =" android. provider. telephony. sms_received "/> <category Android: Name =" android. intent. category. default "/> </intent-filter> </Cycler>

Permission:

 <uses-permission android:name="android.permission.RECEIVE_SMS" />    <uses-permission android:name="android.permission.READ_SMS" />    <uses-permission android:name="android.permission.WRITE_SMS" />

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.