浅析Android手机卫士sim卡绑定
程序员文章站
2024-02-27 23:17:15
读取sim卡的序列号,保存起来,一旦sim发生变更了,认为是小偷的sim卡,发出警告
读取sim卡序列号
获取telephonymanager对象,通过gets...
读取sim卡的序列号,保存起来,一旦sim发生变更了,认为是小偷的sim卡,发出警告
读取sim卡序列号
获取telephonymanager对象,通过getsystemservice(telephony_service)
调用telephonymanager对象的getsimserialnumber()方法,得到string类型的序列号
把序列号保存在sharedpreences里面
需要加权限 android.permission.read_phone_state
检测sim卡是否变更
使用广播接收者,监听手机的开机广播
新建一个包 xxx.receiver,新建一个类bootcompletereceiver.java
清单文件注册一下,添加<receiver>节点,设置名称android:name=”xxx”
添加意图过滤器<intent-filter>节点
添加动作<action/>节点,设置监听动作名称android.intent.action.boot_completed
在bootcompletereceiver类里面的onreceive()方法里,接收到广播,读取当前的sim卡序列号,比较之前保存的sim卡序列号,如果不一样就发送短信给安全号码
监听开机启动需要权限 android:perission.receive_boot_completed
bootcompletereceiver.java
package com.qingguow.mobilesafe.receiver; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.content.sharedpreferences; import android.telephony.telephonymanager; import android.widget.toast; public class bootcompletereceiver extends broadcastreceiver { private telephonymanager tm; private sharedpreferences sp; @override public void onreceive(context cotext, intent arg1) { sp=cotext.getsharedpreferences("config", context.mode_private); //读取当前的sim卡序列号 tm=(telephonymanager) cotext.getsystemservice(context.telephony_service); string relsim=tm.getsimserialnumber(); //读取之前保存的 string savesim=sp.getstring("sim", ""); //判断后发短信 if(savesim.equals(relsim)){ }else{ //发送报警短息给安全号码 system.out.println("sim卡变更了"); toast.maketext(cotext, "sim卡变更了", 1).show(); } } }
secguide2activity.java
package com.qingguow.mobilesafe; import android.content.intent; import android.content.sharedpreferences; import android.content.sharedpreferences.editor; import android.os.bundle; import android.telephony.telephonymanager; import android.text.textutils; import android.view.view; import android.view.view.onclicklistener; import com.qingguow.mobilesafe.ui.settingitemview; public class secguide2activity extends basesecguideactivity { private settingitemview siv_sim; private sharedpreferences sp; private telephonymanager telephonymanager; @override protected void oncreate(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); // 根据保存的数据设置状态 string sim = sp.getstring("sim", ""); if (textutils.isempty(sim)) { siv_sim.setchecked(false); } else { siv_sim.setchecked(true); } // 绑定sim卡 siv_sim.setonclicklistener(new onclicklistener() { @override public void onclick(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 void nextstep(view v) { shownext(); } public void prestep(view v) { showpre(); } @override public void shownext() { intent intent = new intent(this, secguide3activity.class); startactivity(intent); finish(); } @override public void showpre() { intent intent = new intent(this, secguide1activity.class); startactivity(intent); finish(); } }
上一篇: activiti获取流程图实例