Third-party text message applications in Android receive text messages and save them to the system database.
/**
* Inherit the broadcast Receiver
* @ Author Administrator
*
*/
Public class smsreceiver extends broadcastreceiver {
Private long date;
Private context;
Private Final int six = 6;
Private string phonenumber;
Private stringbuilder messagebody = new stringbuilder ();
Private final string smsaction = "android. provider. telephony. sms_received ";
/**
* Receive text messages
*/
// @ Override
Public void onreceive (context, intent ){
String action = intent. getaction ();
If (action. Equals (smsaction )){
Bundle bundle = intent. getextras ();
If (null! = Bundle ){
Object [] PDUS = (object []) bundle. Get ("PDUS ");
Smsmessage [] MSG = new smsmessage [PDUS. Length];
For (INT I = 0; I <PDUS. length; I ++ ){
MSG [I] = smsmessage. createfrompdu (byte []) PDUS [I]);
}
For (smsmessage currmsg: MSG ){
Messagebody. append (currmsg. getdisplaymessagebody ());
Phonenumber = currmsg. getdisplayoriginatingaddress ();
Date = currmsg. gettimestampmillis ();
}
String time = abstractactivity. formattimestampstring (
Context, date );
String sender = abstractactivity. querynamebynum (phonenumber,
Context, false );
Addsmstodb (phonenumber, messagebody. tostring ());
}
}
}
/**
* Add to system SMS Database
*/
Private void addsmstodb (string address, string content ){
Contentvalues values = new contentvalues ();
Values. Put ("date", system. currenttimemillis ());
Values. Put ("read", 0); // 0 indicates unread information.
Values. Put ("type", 1); // 1 indicates the inbox information.
Values. Put ("Address", address );
Values. Put ("body", content );
Smsapp. Context. getcontentresolver (). insert (URI. parse ("content: // SMS "),
Values );
}
}