Android sends and receives text messages to intercept text messages.
Overview:
To be honest, some operations related to text messages in Android are a relatively quick start. The reason why I want to write this blog is that I want to share this blog with more people because there is something in development, at the same time, I made a record on the text message of the Android system in the future, so I wrote this long-winded article. If you think this is a good thing, you are welcome to add it to your favorites so that you can conveniently view the content updated in this article in the future. Next, I will learn about the text message operations in the Android system from three aspects in the title.
Text message sending
Due to the excellent encapsulation of SMS sending methods in Android, the development of SMS sending is very simple.
public static void sendMessage(Context context, String content, String phoneNumber) { SmsManager sms = SmsManager.getDefault(); PendingIntent pi = PendingIntent.getBroadcast(context, 0, new Intent(), 0); sms.sendTextMessage(phoneNumber, null, content, pi, null); }
Don't forget to worry about permission issues:
Receive SMS
It is more complicated to receive short messages. The complicated reasons should also be easy to understand-receiving is uncontrollable. That is to say, our mobile phone does not know when a short message will be sent. Because it is too passive, there is something powerful in the Android mechanism that you admire, that is, the broadcast receiver. We register a broadcast receiver, and then let the broadcast receiver listen to the event of whether short messages arrive at all times. In this way, the passive triggering event is perfectly solved. Let's take a look at this process:
Public void onReceive (Context context, Intent intent) {if (intent. getAction (). equals ("android. provider. telephony. SMS_RECEIVED ") {Bundle bundle = intent. getExtras (); Object messages [] = (Object []) bundle. get ("pdus"); SmsMessage smsMessage [] = new SmsMessage [messages. length]; for (int n = 0; n <messages. length; n ++) {smsMessage [n] = SmsMessage. createFromPdu (byte []) messages [n]);} showToast (context, "SMS content:" + smessage [0]. getMessageBody ());}}
Add permission:
And perform a static registration in manifest:
Text message interception
It is much easier to intercept text messages based on the ability to receive text messages. Because I can receive the message, I only need to become the first recipient and stop spreading the received text message downward. This intercepts the short message.
Raise priority:
Blocking propagation: