Read the sim card serial number, save it, once the sim has changed, think it is the thief's sim card, issued a warning
Read sim card serial number
Gets the telephonymanager object, via getsystemservice (telephony_service)
Call the Getsimserialnumber () method of the Telephonymanager object to get the serial number of String type
Keep the serial number inside the sharedpreences.
Need to add permission Android.permission.READ_PHONE_STATE
Detect if the sim card is changed
Use the broadcast receiver to monitor the phone's start-up broadcast
Create a new package xxx.receiver, create a new class Bootcompletereceiver.java
Manifest file register, add <receiver> node, set name android:name="xxx"
Add Intent Filters <intent-filter> nodes
Add actions <action/> nodes, set the listener action name Android.intent.action.BOOT_COMPLETED
In the onreceive () method inside the Bootcompletereceiver class , the broadcast is received and the current sim is read Card serial number, compare the previous saved sim card serial number, if not the same, send SMS to the security number
Permission Android:perission is required for monitoring boot boot . Receive_boot_completed
Bootcompletereceiver.java
PackageCom.qingguow.mobilesafe.receiver;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;Importandroid.content.SharedPreferences;ImportAndroid.telephony.TelephonyManager;ImportAndroid.widget.Toast; Public classBootcompletereceiverextendsBroadcastreceiver {PrivateTelephonymanager TM; PrivateSharedpreferences sp; @Override Public voidonreceive (Context cotext, Intent arg1) {SP=cotext.getsharedpreferences ("config", context.mode_private); //read the current SIM card serial numbertm=(Telephonymanager) Cotext.getsystemservice (Context.telephony_service); String Relsim=Tm.getsimserialnumber (); //read the previous savedString savesim=sp.getstring ("Sim", "" "); //send text messages after judgment if(Savesim.equals (Relsim)) {}Else{ //send alarm short to security numberSystem.out.println ("SIM card Changed"); Toast.maketext (Cotext,"SIM card Changed", 1). Show (); } }}
Secguide2activity.java
PackageCom.qingguow.mobilesafe;Importandroid.content.Intent;Importandroid.content.SharedPreferences;ImportAndroid.content.SharedPreferences.Editor;ImportAndroid.os.Bundle;ImportAndroid.telephony.TelephonyManager;Importandroid.text.TextUtils;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportCom.qingguow.mobilesafe.ui.SettingItemView; Public classSecguide2activityextendsbasesecguideactivity {PrivateSettingitemview Siv_sim; PrivateSharedpreferences sp; PrivateTelephonymanager Telephonymanager; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.LAYOUT.ACTIVITY_LOST_FIND2); Siv_sim=(Settingitemview) Findviewbyid (R.id.siv_sim); SP= getsharedpreferences ("config", mode_private); Telephonymanager=(Telephonymanager) Getsystemservice (Telephony_service); //set the state based on the saved dataString sim = sp.getstring ("Sim", "" "); if(Textutils.isempty (SIM)) {siv_sim.setchecked (false); } Else{siv_sim.setchecked (true); } //Binding SIM cardSiv_sim.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {String sim=Telephonymanager.getsimserialnumber (); Editor Editor=Sp.edit (); String sImSp= Sp.getstring ("Sim", "" "); if(Textutils.isempty (sImSp)) {siv_sim.setchecked (true); Editor.putstring ("Sim", SIM); } Else{siv_sim.setchecked (false); Editor.putstring ("Sim", "" "); } editor.commit (); } }); } Public voidNextStep (View v) {shownext (); } Public voidPrestep (View v) {showpre (); } @Override Public voidShownext () {Intent Intent=NewIntent ( This, Secguide3activity.class); StartActivity (Intent); Finish (); } @Override Public voidShowpre () {Intent Intent=NewIntent ( This, Secguide1activity.class); StartActivity (Intent); Finish (); }}
[Android] Mobile defender binding SIM card