Android intercepts SMS messages and blocks system notifications

Source: Internet
Author: User

There are several key points to intercept text messages:

 

1. Android receives text messages in broadcast mode.

2. You only need to add the "receive" SMS permission to your manifest. xml file.

View plainprint?
  1. <Uses-Permission Android: Name = "android. Permission. receive_sms"> </uses-Permission>

<Uses-Permission Android: Name = "android. Permission. receive_sms"> </uses-Permission> 

3. Write a broadcast receiving class.

View plainprint?
  1. Public class smsreceiveandmask extends broadcastreceiver {
  2. Private string tag = "smsreceiveandmask ";
  3. @ Override
  4. Public void onreceive (context, intent ){
  5. }

Public class smsreceiveandmask extends broadcastreceiver {private string tag = "smsreceiveandmask"; @ overridepublic void onreceive (context, intent ){} 

4. Intent-filter should be added to the receiver tag of manifest. XML, and action is

View plainprint?
  1. <Action Android: Name = "android. provider. telephony. sms_received"/>

<Action Android: Name = "android. provider. telephony. sms_received"/> 

5. It is important to add the priority on the intent-filter so that you can receive SMS messages that take precedence over the system or other software.

View plainprint?
  1. <Cycler Android: Name = ". smsreceiveandmask">
  2. <Intent-filter Android: Priority = "1000">
  3. <Action Android: Name = "android. provider. telephony. sms_received"/>
  4. </Intent-filter>
  5. </Cycler>

<Cycler Android: Name = ". smsreceiveandmask "> <intent-filter Android: Priority =" 1000 "> <action Android: Name =" android. provider. telephony. sms_received "/> </intent-filter> </Cycler> 

6. when your program receives the SMS to be blocked, use this. abortbroadcast (); to end broadcasting and send it to other programs, so that the system will not receive text message broadcast and notification will not prompt

View plainprint?
  1. // Step 3: Cancel
  2. If (flags_filter ){
  3. This. abortbroadcast ();
  4. }

// Step 3: cancel if (flags_filter) {This. abortbroadcast ();} 

 

The source code is as follows:

Manifest. xml

View plainprint?
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Package = "com. hwttnet. Test. smsreceiveandmask" Android: versioncode = "1"
  4. Android: versionname = "1.0" type = "codeph" text = "/codeph">
  5. <Uses-SDK Android: minsdkversion = "3"/>
  6. <Uses-Permission Android: Name = "android. Permission. receive_sms"> </uses-Permission>
  7. <Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">
  8. <Cycler Android: Name = ". smsreceiveandmask">
  9. <Intent-filter Android: Priority = "1000">
  10. <Action Android: Name = "android. provider. telephony. sms_received"/>
  11. </Intent-filter>
  12. </Cycler>
  13. </Application>
  14. </Manifest>

<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com. hwttnet. test. smsreceiveandmask "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <uses-SDK Android: minsdkversion =" 3 "/> <uses-Permission Android: name = "android. permission. receive_sms "> </uses-Permission> <application Android: icon =" @ drawable/icon "Android: Label =" @ string/app_name "> <receiver er Android: Name = ". smsreceiveandmask "> <intent-filter Android: Priority =" 1000 "> <action Android: Name =" android. provider. telephony. sms_received "/> </intent-filter> </receiver> </Application> </manifest> 

 

Broadcastreceiver class:

 

View plainprint?
  1. Package com. hwttnet. Test. smsreceiveandmask;
  2. Import Android. App. activity;
  3. Import Android. content. broadcastreceiver;
  4. Import Android. content. context;
  5. Import Android. content. intent;
  6. Import Android. OS. Bundle;
  7. Import Android. telephony. smsmessage;
  8. Import Android. util. log;
  9. Public class smsreceiveandmask extends broadcastreceiver {
  10. Private string tag = "smsreceiveandmask ";
  11. @ Override
  12. Public void onreceive (context, intent ){
  13. Log. V (TAG, ">>>>>> onreceive start ");
  14. // Step 1. Obtain the text message content and sender
  15. Stringbuilder body = new stringbuilder (); // text message content
  16. Stringbuilder number = new stringbuilder (); // SMS sender
  17. Bundle bundle = intent. getextras ();
  18. If (bundle! = NULL ){
  19. Object [] _ PDUS = (object []) bundle. Get ("PDUS ");
  20. Smsmessage [] Message = new smsmessage [_ PDUS. Length];
  21. For (INT I = 0; I <_ PDUS. length; I ++ ){
  22. Message [I] = smsmessage. createfrompdu (byte []) _ PDUS [I]);
  23. }
  24. For (smsmessage currentmessage: Message ){
  25. Body. append (currentmessage. getdisplaymessagebody ());
  26. Number. append (currentmessage. getdisplayoriginatingaddress ());
  27. }
  28. String smsbody = body. tostring ();
  29. String smsnumber = number. tostring ();
  30. If (smsnumber. Contains ("+ 86 ")){
  31. Smsnumber = smsnumber. substring (3 );
  32. }
  33. // Step 2: Check whether the text message content meets the filtering conditions.
  34. Boolean flags_filter = false;
  35. If (smsnumber. Equals ("10086") {// Block messages sent from 10086
  36. Flags_filter = true;
  37. Log. V (TAG, "sms_number.equals (10086 )");
  38. }
  39. // Step 3: Cancel
  40. If (flags_filter ){
  41. This. abortbroadcast ();
  42. }
  43. }
  44. Log. V (TAG, ">>>>>> onreceive end ");
  45. }
  46. }

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.