Android SMS monitoring (one)--using broadcastreceiver to realize SMS monitoring

Source: Internet
Author: User

Mainactivity as follows:

Package Cc.testsmsbroadcastreceiver;import Cc.testsmsbroadcastreceiver.SMSBroadcastReceiver.MessageListener; Import Android.os.bundle;import Android.widget.textview;import android.app.activity;/** * Demo Description: *  Using Broadcastreceiver to implement listening SMS * * NOTE permission: * <uses-permission android:name= "Android.permission.RECEIVE_SMS"/> * * Details: * http://blog.csdn.net/lfdfhl/article/details/8195400 * */public class Mainactivity extends activity{private TextView    Mtextview; Private Smsbroadcastreceiver msmsbroadcastreceiver; @Overrideprotected void OnCreate (Bundle savedinstancestate) {    Super.oncreate (savedinstancestate); Setcontentview (R.layout.main); init ();}    private void Init () {mtextview= (TextView) Findviewbyid (R.id.textview);    Msmsbroadcastreceiver=new Smsbroadcastreceiver (); Msmsbroadcastreceiver.setonreceivedmessagelistener (New MessageListener () {@Overridepublic void onreceived (String    Message) {mtextview.settext (message);}}); }}

Smsbroadcastreceiver as follows:

Package Cc.testsmsbroadcastreceiver;import Java.text.simpledateformat;import Java.util.date;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import android.telephony.smsmessage;/** * Configure the Broadcast receiver: * <receiver android:name= ". Smsbroadcastreceiver "> * <intent-filter android:priority=" + "> * <action android:name=" ANDROID.P Rovider. Telephony.sms_received "/> * </intent-filter> * </receiver> * * NOTE: * <intent-filter ANDROID:PRI Ority= ">": * Set this broadcast receiver to a level of up to */public class Smsbroadcastreceiver extends Broadcastreceiver {private static Me Ssagelistener mmessagelistener;public Smsbroadcastreceiver () {super ();} @Overridepublic void OnReceive (context context, Intent Intent) {Object [] pdus= (object[]) Intent.getextras (). Get ("PD     US ");    for (Object pdu:pdus) {smsmessage SMSMESSAGE=SMSMESSAGE.CREATEFROMPDU ((Byte []) PDU); String sender=smsmessage.getdisplayoriginatingaddress ();   String Content=smsmessage.getmessagebody ();    Long Date=smsmessage.gettimestampmillis ();    Date Timedate=new date (date);    SimpleDateFormat simpledateformat=new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");        String Time=simpledateformat.format (timedate);    SYSTEM.OUT.PRINTLN ("SMS From:" +sender);    SYSTEM.OUT.PRINTLN ("SMS content:" +content);        SYSTEM.OUT.PRINTLN ("SMS Time:" +time);        mmessagelistener.onreceived (content);    If the text message comes from 5556, no longer passes down the IF ("5556". Equals (Sender)) {System.out.println ("abort");    Abortbroadcast (); }}}//callback interface public interface MessageListener {public void onreceived (String message); public void Setonreceivedmessagelistener (MessageListener messagelistener) {This.mmessagelistener=messagelistener;}}

Main.xml as follows:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "     >    <textview        android:id= "@+id/textview"        android:layout_width= "Wrap_content"        android: layout_height= "Wrap_content"        android:text= "@string/hello_world"        android:layout_centerinparent= "true"/ ></RelativeLayout>

Androidmanifest.xml as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Cc.testsmsbroadcastreceiver "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses -SDK android:minsdkversion= "8" android:targetsdkversion= "8"/> <uses-permission android:name= "a Ndroid.permission.RECEIVE_SMS "/> <application android:allowbackup=" true "android:icon=" @drawabl            E/ic_launcher "android:label=" @string/app_name "android:theme=" @style/apptheme "> <activity            Android:name= "cc.testsmsbroadcastreceiver.MainActivity" android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <categ                Ory android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <receiver android:name= ". SMsbroadcastreceiver "><intent-filter android:priority=" "><action android:name=" Android.provider.Telephony.SMS_RECEIVED "/></intent-filter></receiver> </application></ Manifest>


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.