读取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(); } }
时间: 2024-10-10 18:11:21