From 874cb7f9572aeb3e2441eca44e44f097752cef14 Mon Sep 17 00:00:00 2001 From: Naccl Date: Fri, 14 Jan 2022 19:19:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E6=9B=B4=E6=8D=A2SI?= =?UTF-8?q?M=E5=8D=A1=E5=90=8E=EF=BC=8C=E5=8D=A1=E6=A7=BD=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E4=BB=8D=E6=98=AF=E4=B8=8A=E4=B8=80=E5=BC=A0=E5=8D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更换SIM卡,如果不杀后台并重启,则发送出的「卡槽信息」仍然是刚启动应用时读取的SIM卡 --- .../idormy/sms/forwarder/MyApplication.java | 6 +++ .../forwarder/receiver/SimStateReceiver.java | 41 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 app/src/main/java/com/idormy/sms/forwarder/receiver/SimStateReceiver.java diff --git a/app/src/main/java/com/idormy/sms/forwarder/MyApplication.java b/app/src/main/java/com/idormy/sms/forwarder/MyApplication.java index 42b0f834..a4cfe050 100644 --- a/app/src/main/java/com/idormy/sms/forwarder/MyApplication.java +++ b/app/src/main/java/com/idormy/sms/forwarder/MyApplication.java @@ -4,10 +4,12 @@ import android.app.ActivityManager; import android.app.Application; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.content.SharedPreferences; import android.os.Build; import android.util.Log; +import com.idormy.sms.forwarder.receiver.SimStateReceiver; import com.idormy.sms.forwarder.sender.SendHistory; import com.idormy.sms.forwarder.service.BatteryService; import com.idormy.sms.forwarder.service.FrontService; @@ -70,6 +72,10 @@ public class MyApplication extends Application { Intent batteryServiceIntent = new Intent(this, BatteryService.class); startService(batteryServiceIntent); + //SIM卡插拔状态广播监听 + IntentFilter simStateFilter = new IntentFilter(SimStateReceiver.ACTION_SIM_STATE_CHANGED); + registerReceiver(new SimStateReceiver(), simStateFilter); + //友盟统计 sharedPreferencesHelper = new SharedPreferencesHelper(this, "umeng"); //设置LOG开关,默认为false diff --git a/app/src/main/java/com/idormy/sms/forwarder/receiver/SimStateReceiver.java b/app/src/main/java/com/idormy/sms/forwarder/receiver/SimStateReceiver.java new file mode 100644 index 00000000..cee0d37c --- /dev/null +++ b/app/src/main/java/com/idormy/sms/forwarder/receiver/SimStateReceiver.java @@ -0,0 +1,41 @@ +package com.idormy.sms.forwarder.receiver; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.util.Log; + +import com.idormy.sms.forwarder.MyApplication; +import com.idormy.sms.forwarder.utils.PhoneUtils; + +public class SimStateReceiver extends BroadcastReceiver { + + private static final String TAG = "SimStateReceiver"; + public static final String ACTION_SIM_STATE_CHANGED = "android.intent.action.SIM_STATE_CHANGED"; + private static final String EXTRA_SIM_STATE = "ss"; + private static final String SIM_STATE_LOADED = "LOADED"; + + /** + * 更换SIM卡,如果不杀后台并重启,则发送出的「卡槽信息」仍然是刚启动应用时读取的SIM卡 + * 增加这个Receiver,接收SIM卡插拔状态广播,自动更新缓存 + * + * @param context + * @param intent + */ + @Override + public void onReceive(Context context, Intent intent) { + String receiveAction = intent.getAction(); + Log.d(TAG, "onReceive intent " + receiveAction); + if (ACTION_SIM_STATE_CHANGED.equals(receiveAction)) { + //SIM状态的额外信息 + String state = intent.getExtras().getString(EXTRA_SIM_STATE); + Log.d(TAG, state); + //只需要最后一个SIM加载完毕的 LOADED 状态 + if (SIM_STATE_LOADED.equals(state)) { + //刷新SimInfoList + MyApplication.SimInfoList = PhoneUtils.getSimMultiInfo(); + Log.d(TAG, MyApplication.SimInfoList.toString()); + } + } + } +}