Third-party text message applications in Android receive text messages and save them to the system database.

Source: Internet
Author: User
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 );
}

}

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.