Previously, I made some simple analysis on the text message database in the Android system and wrote some simple code to operate the text message database in the Android system. For details, see:
Analysis on reading Android text message from Android text message Database
In fact, in the Android system, SMS operations are not limited to operating the android SMS database. We can also intercept SMS messages received by Android or text messages sent by mobile phones.
The Android system uses the Broadcast Mechanism in a superb way, and the text message is no exception. When the Android mobile phone receives an external text message, it will send a broadcast to the system. Of course, this broadcast can be intercepted before it is received by the system:
The Android system receives the message broadcast: Android. provider. telephony. sms_received
As long as we receive the broadcast and extract the text message, we can intercept the external text message: the extracted information includes the phone number of the sender and the complete text information of the SMS. Therefore, when we intercept the SMS from the Android system, different processes can be performed based on different numbers, or different processes can be performed based on different text message content: for example, saving text messages of a specific number to a password-protected database, by storing text messages containing specific words in a specified database, you can implement the privacy protection function of text messages. Of course, you can also intercept spam messages and pick up harassing text messages ......
In fact, implementing these functions is very simple. We only need to implement these functions in the androidmanifest of the program. register a receiver in XML and broadcast it to the system SMS: Android. provider. telephony. sms_received is associated, and then the text message content can be read in the explorer. Of course, do not forget to add it to androidmanifest. permissions in XML:
Some common permissions include the following, but I have introduced them more. You can see the functions at a Glance:
<Uses-Permission Android: Name = "android. Permission. read_contacts">
</Uses-Permission>
<Uses-Permission Android: Name = "android. Permission. write_contacts">
</Uses-Permission>
<Uses-Permission Android: Name = "android. Permission. read_sms">
</Uses-Permission>
<Uses-Permission Android: Name = "android. Permission. write_sms">
</Uses-Permission>
<Uses-Permission Android: Name = "android. Permission. write_external_storage">
</Uses-Permission>
<Uses-Permission Android: Name = "android. Permission. call_phone">
</Uses-Permission>
<Uses-Permission Android: Name = "android. Permission. read_phone_state">
</Uses-Permission>
<Uses-Permission Android: Name = "android. Permission. receive_sms">
</Uses-Permission>
<Uses-Permission Android: Name = "android. Permission. send_sms">
</Uses-Permission>
The registration process of the receiver who receives the SMS is as follows:
<Cycler Android: Name = "com. Contact. Main. cycler. incommingsmsreceiver">
<Intent-filter Android: Priority = "10000">
<Action Android: Name = "android. Intent. Action. phone_state"/>
<Action Android: Name = "android. provider. telephony. sms_received"/>
<Action Android: Name = "android. Intent. Action. boot_completed"/>
</Intent-filter>
</Cycler>
The next step is to process the broadcast. The text message logs intercepted during the operation are as follows:
The source code is as follows:
Package com. Contact. Main. Cycler;
Import com. Contact. Main. util. dateutils;
Import com. Contact. Main. util. log;
Import Android. content. broadcastreceiver;
Import Android. content. context;
Import Android. content. intent;
Import Android. OS. Bundle;
Import Android. telephony. smsmessage;
Public class incommingsmsreceiver extends broadcastreceiver {
Public static final string action_sms_received = "android. provider. telephony. sms_received ";
Private string tag = incommingsmsreceiver. Class. getsimplename ();
@ Override
Public void onreceive (context, intent ){
String action = intent. getaction ();
If (action_sms_received.equals (Action )){
Handreceivedsms (context, intent );
}
Log. D (TAG, "SMS in ");
}
Private Boolean handreceivedsms (context, intent ){
Boolean result = false;
Final smsmessage [] msgs = getmessages (intent );
If (null! = MSGs ){
For (smsmessage: MSGs ){
Handlesmessage (context, smsmessage );
}
}
Return result;
}
Private Final Static smsmessage [] getmessages (final intent ){
Final bundle = intent. getextras ();
Smsmessage [] msgs = new smsmessage [] {};
If (null! = Bundle ){
Msgs = getmessagesfromintent (intent );
}
Return msgs;
}
Public static smsmessage [] getmessagesfromintent (final intent ){
Final object [] messages = (object []) intent. getserializableextra ("PDUS ");
Final byte [] [] pduobjs = new byte [messages. Length] [];
For (INT I = 0; I <messages. length; I ++ ){
Pduobjs [I] = (byte []) messages [I];
}
Final byte [] [] PDUS = new byte [pduobjs. Length] [];
Final int pducount = PDUS. length;
Final smsmessage [] msgs = new smsmessage [pducount];
For (INT I = 0; I <pducount; I ++ ){
PDUS [I] = pduobjs [I];
Msgs [I] = smsmessage. createfrompdu (PDUS [I]);
}
Return msgs;
}
Private void handlesmessage (context, smsmessage MSG ){
Final string number = msg. getoriginatingaddress ();
Final string smscontent = msg. getdisplaymessagebody ();
Long date = system. currenttimemillis ();
String mdate [] = dateutils. convertdate (date );
Log. D (tag,
"------" + "\ N"
+ "Number:" + number + "\ n"
+ "Content:" + smscontent + "\ n"
+ "Time:" + mdate [0] + "-" + mdate [1] + "\ n"
+ "------");
}
Address: http://www.cppcode.com/archives/2012/03/31/257.html