Merge pull request #105 from Naccl/main

修复:更换SIM卡后,卡槽信息仍是上一张卡
This commit is contained in:
pppscn 2022-01-15 08:40:52 +08:00 committed by GitHub
commit fc7a2353f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 0 deletions

View File

@ -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

View File

@ -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());
}
}
}
}