1, first we have to write a radio receiver, when our mobile phone received a text message, the system will automatically send a broadcast, we just need to receive this broadcast can be
2, in the broadcast, we rewrite the OnReceive () method, through the intent written inside the bundle can get the text message content,
3, the manifest file inside we must add the permission, otherwise cannot receive.
4, in order to prevent our broadcasts cannot receive, we write the broadcast receiver's permission must be big, just in case, I set 1000.
The following code, inside the comment is also more detailed:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Manifestxmlns:android= "Http://schemas.android.com/apk/res/android"3 Package= "Com.example.fanlei.cutnotedemo" >4 5 //Receive SMS6 <uses-permissionAndroid:name= "Android.permission.RECEIVE_SMS"/>7 <Application8 Android:allowbackup= "true"9 Android:icon= "@drawable/ic_launcher"Ten Android:label= "@string/app_name" One Android:theme= "@style/apptheme" > A <!--Action:name = The name is fixed - - <receiverAndroid:name=". Notereceiver "> - <Intent-filterandroid:priority= "+"> the <ActionAndroid:name= "Android.provider.Telephony.SMS_RECEIVED"/> - </Intent-filter> - </receiver> - <Activity + Android:name=". Mainactivity " - Android:label= "@string/app_name" > + <Intent-filter> A <ActionAndroid:name= "Android.intent.action.MAIN" /> at - <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> - </Intent-filter> - </Activity> - </Application> - </Manifest>
Write a class that inherits Broadcastreceiver
1 PackageCom.example.fanlei.cutnotedemo;2 3 ImportAndroid.content.BroadcastReceiver;4 ImportAndroid.content.Context;5 Importandroid.content.Intent;6 ImportAndroid.os.Bundle;7 ImportAndroid.telephony.SmsMessage;8 ImportAndroid.widget.Toast;9 Ten ImportJava.text.SimpleDateFormat; One Importjava.util.Date; A - /** - * Broadcast Receivers the */ - Public classNotereceiverextendsBroadcastreceiver { - - Private Static FinalString sms_received_action = "Android.provider.Telephony.SMS_RECEIVED"; + @Override - Public voidOnReceive (Context context, Intent Intent) { +String action =intent.getaction (); A //Judging broadcast Messages at if(Action.equals (sms_received_action)) { -Bundle bundle =Intent.getextras (); - //if it is not empty - if(Bundle! =NULL){ - //Convert content inside PDUs to object[] array -Object pdusdata[] = (object[]) bundle.get ("PDUs"); in //Parsing SMS -smsmessage[] msg =NewSmsmessage[pdusdata.length]; to for(inti = 0;i < msg.length;i++){ + bytePdus[] = (byte[]) pdusdata[i]; -Msg[i] =SMSMESSAGE.CREATEFROMPDU (PDUs); the } *StringBuffer content =NewStringBuffer ();//Get SMS Content $StringBuffer PhoneNumber =NewStringBuffer ();//Get AddressPanax NotoginsengStringBuffer Receivedata =NewStringBuffer ();//Get Time - //Analyze SMS specific parameters the for(Smsmessage temp:msg) { + Content.append (Temp.getmessagebody ()); A Phonenumber.append (Temp.getoriginatingaddress ()); theReceivedata.append (NewSimpleDateFormat ("Yyyy-mm-dd hh:mm:ss. SSS ") +. Format (NewDate (Temp.gettimestampmillis () )); - } $ /** $ * There are a lot of things to do here, such as we intercept according to the phone number (cancel the broadcast continue to spread), etc. - */ -Toast.maketext (context,phonenumber.tostring () +content+receivedata, Toast.length_long). Show ();//SMS Content the } - }Wuyi } the}
android--Get SMS content, intercept SMS