Android instance-Mobile security Defender (25)-Check the phone SIM card for changes

Source: Internet
Author: User

First, the goal

1, the use of the broadcast receiver (phone restart will be issued when the corresponding broadcast) to get the SIM serial number when the boot, and the comparison has been saved. The corresponding operation is done according to the comparison results.

2. (optional) force the SIM card to be bound in the second step of the anti-Theft Setup wizard before the next set-up is required.

3. To get data from another activity, click "Select Contacts" to jump to the new class (select a contact) and get contact data from it.

Second, the code implementation.

1. Create a new package (named Com.example.mobilesafe.receiver) under the package to receive the broadcast. Create a new class (named Bootcompletereceiver.java) under the new package and inherit the broadcast recipient (Broadcastreceiver).

2. Register the newly created broadcast receive class (named Bootcompletereceiver.java) in the configuration file (Androidmanifest.xml). New recipient <receiver> Tag under <activity> tag, name (Android:name) for new broadcast accept class full path name,<receiver> label New Intent Filter Label < Intent-filter>, intention to filter tags <intent-filter> new action tag <action>, action tag <action> the name of the broadcast that needs attention (Android: Name) is Android.intent.action.BOOT_COMPLETED.

Broadcast Registration code:

1 <receiver android:name= "Com.example.mobilesafe.receiver.BootCompleteReceiver" >2             <intent-filter>3                 <action android:name= "Android.intent.action.BOOT_COMPLETED"/>4              </intent-filter>5         </receiver>
View Code

3. In the newly created broadcast receive Class (Bootcompletereceiver.java), the serial number of the SIM in the saved File (config) is obtained through the Sharedpreferences object (configuration parameter is Simserialnumber), by Telephonyman The Ager object gets the current SIM sequence number and then compares it. According to the comparison of the results of the corresponding operation.

1  PackageCom.example.mobilesafe.receiver;2 3 ImportAndroid.content.BroadcastReceiver;4 ImportAndroid.content.Context;5 Importandroid.content.Intent;6 Importandroid.content.SharedPreferences;7 ImportAndroid.telephony.TelephonyManager;8 ImportAndroid.widget.Toast;9 Ten  Public classBootcompletereceiverextendsBroadcastreceiver { One      A     Privatesharedpreferences sp; -     PrivateTelephonymanager TM; -  the @Override -      Public voidOnReceive (Context context, Intent Intent) { -SP = context.getsharedpreferences ("config", context. Mode_private); -TM =(Telephonymanager) context.getsystemservice (context. Telephony_service); +         //get the saved SIM card serial number -String Savedsim = sp.getstring ("Simserialnumber",NULL); +         //get the current SIM card serial number AString Newsim =Tm.getsimserialnumber ();  at         if(Newsim.equals (Savedsim)) { -             //Serial numbers equal -              -}Else{ -             //serial numbers are not equal -         } in     } -}
View Code

4. (optional) mandatory binding of SIM card. In the second step of the wizard, slide the activity Master code to the next method (Slidenext ()), and the SP gets the value of the GetString parameter (simserialnumber) for the SIM binding condition in the Get configuration file (config), and if it returns NULL, You cannot proceed to the next step.

The code is as follows:

1  Public voidSlidenext () {2         3String sim = sp.getstring ("Simserialnumber",NULL);4         if(Textutils.isempty (SIM)) {5Toast.maketext ( This, "SIM card not Bound", 0). Show ();6             return;7         }8Intent Intent =NewIntent (setupwizard_ui_2. This, Setupwizard_ui_3.class);9 startactivity (intent);Ten finish (); One overridependingtransition (r.anim.tran_in, r.anim.tran_out);  A}
View Code

5. Click the "Select Contacts" button to enter the activity of the selected contact, and finally get the data from the activity.

①. Set up a Click event for select Contacts; ②. Connect two activity via Intent in the Click event Method (the wizard sets the third step, select the contact); ③. Startactivityforresult Intent, int Requestcode) method starts, the parameter intent is the intention of connecting two activity, int requestcode is the result code returned, here first set to 0.

Click event Method

public void SelectContact (view view) {Intent Intent = new Intent (setupwizard_ui_3.this,selectcontactactivity.class); Startactivityforresult (Intent, 0);}

Android instance-Mobile security Defender (25)-Check the phone SIM card for changes

Related Article

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.