First, the goal
1, using the radio receiver to accept the security number sent by SMS;
2, according to the text message content to complete the corresponding instruction operation (here is temporarily replaced by toast).
Second, the Code implementation
1. Create a new Smsreceiver class under receiver package to inherit Broadcastreceiver, and in the function manifest file (androidmanifest.xml) in the <application> Register the receiver within the name of its full path name, the intent filter (<intent-filter>) in the Set priority (Android:priority) is higher (such as 1000), activity (<action>) for SMS acceptance.
The code to register receiver is as follows:
1 <receiver android:name= "Com.example.mobilesafe.receiver.SMSReceiver" >2 < Intent-filter android:priority= ">3 <action android:name=" android.provider.Telephony.SMS_ RECEIVED "/>4 </intent-filter> 5 </receiver>
View Code
2, increase the access to receive text messages: <uses-permission android:name= "Android.permission.RECEIVE_SMS"/>.
3, in the new Smsreceiver class in the OnReceive method to implement the acceptance of text messages and according to their content to complete the relevant operation (can refer to the relevant template code)
①. Define the Sharedpreferences object in the main code to read the security number in the configuration file, and then in the OnReceive method through the first parameter context (context) getsharedpreferences (String name , int mode) method gets the configuration file named "config", mode is 0;
②. Get the Map collection within the intent by the Getextras () method of the second incoming parameter intent of the OnReceive method, and get the collection of text messages via get (String key), where the key is "PDUs", The return value is an object array (because the text message has many bars and requires a strong turn);
③. Iterates over the acquired object array, which can be traversed with enhanced for loops. In the loop:
1). Obtain each text message (Smsmessage) object through the CREATEFROMPDU (byte[] PDU) method of the Smsmessage (Android.telephony.SmsMessage) object;
2). Gets the sender information of the message string type by the Getoriginatingaddress () method of the text message (Smsmessage) object;
3). Get the text message of the SMS string type via the Getmessagebody () method of the text message (Smsmessage) object;
4). Gets the GetString (string key, String defvalue) method of the Sharedpreferences object to get the value corresponding to the key name "Security_number" in the configuration file. And the Equals method is used to determine whether the value is equal to the sender.
5). If equal, determine whether the text message content is equal to the corresponding instruction code, if it is equal to complete the corresponding instruction operation (available if...else if ...) Statement or switch statement)
(The operation of the instructions is given in a follow-up, where toast is replaced first)
6). Call the Abortbroadcast () method to close the broadcast after the relevant instruction operation is complete, preventing the user from accepting the SMS (via receiver's priority precedence).
New Smsreceiver Class Code:
1 Public classSmsreceiverextendsBroadcastreceiver {2 Privatesharedpreferences sp;3 4 @Override5 Public voidOnReceive (Context context, Intent Intent) {6SP = context.getsharedpreferences ("config", 0); 7 //get an object array of all SMS messages by PDUs8Object[] Objs = (object[]) Intent.getextras (). Get ("PDUs"); 9 //iterate through the object array to get every message insideTen for(Object obj:objs) { One //get specific text messages for each message ASmsmessage SMS = SMSMESSAGE.CREATEFROMPDU (byte[]) obj); - //Get SMS Sender Information -String sender =sms.getoriginatingaddress (); theString BODY =sms.getmessagebody (); - if(Sp.getstring ("Security_number", "" "). Equals (sender) { - if("#*location*#". Equals (body)) { - //Get GPS Location +Toast.maketext (context, "GPS position obtained", 0). Show (); - abortbroadcast (); +}Else if("#*alarm*#". Equals (body)) { A //Play alarm Music atToast.maketext (Context, "alarm music played", 0). Show (); - abortbroadcast (); -}Else if("#*wipedata*#". Equals (body)) { - //remotely destroying data -Toast.maketext (context, "data has been remotely destroyed", 0). Show (); - abortbroadcast (); in}Else if("#*lockscreen*#". Equals (body)) { - //Remote Lock Screen toToast.maketext (context, "Remote lock screen", 0). Show (); + abortbroadcast (); - } the } * } $ }Panax Notoginseng}View Code
Android instance-Mobile security Defender (29)-Accept the instructions sent by the security number