gpt4 book ai didi

android - "Called registerBroadcastReceiver twice"在 android 中检查 Sim 卡状态时

转载 作者:搜寻专家 更新时间:2023-11-01 08:51:46 24 4
gpt4 key购买 nike

我有一个广播接收器,它使用 TelephonyManager 检查何时更改 Sim 卡状态。

    public void onReceive(final Context context, Intent intent) {

TelephonyManager telephoneMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int simState = telephoneMgr.getSimState();
String sim = Integer.toString(simState);

switch (simState) {
case TelephonyManager.SIM_STATE_ABSENT:
System.out.println("*******************************************Sim State absent******************************");
Log.i("SimStateListener", "Sim State absent");
break;
case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
System.out.println("*******************************************SIM_STATE_NETWORK_LOCKED******************************"+simState);
Log.i("SimStateListener", "Sim State network locked");
break;
case TelephonyManager.SIM_STATE_PIN_REQUIRED:
System.out.println("*******************************************SIM_STATE_PIN_REQUIRED******************************"+simState);
Log.i("SimStateListener", "Sim State pin required");
break;
case TelephonyManager.SIM_STATE_PUK_REQUIRED:
System.out.println("*******************************************SIM_STATE_PUK_REQUIRED******************************"+simState);
Log.i("SimStateListener", "Sim State puk required");
break;
case TelephonyManager.SIM_STATE_UNKNOWN:
System.out.println("*******************************************SIM_STATE_UNKNOWN******************************"+simState);
Log.i("SimStateListener", "Sim State unknown");
break;
case TelephonyManager.SIM_STATE_READY:
Log.i("SimStateListener", "Sim State ready");
try{
String imsi = telephoneMgr.getSubscriberId();
Log.i("SubscriberId", "SubscriberId " + imsi);
if(imsi.equals("MY IMSI")){
Log.i("SimStateListener", "Sim card is the same");

}
else{

Log.i("SimStateListener", "Sim card is changed, Call the police");

// if phone is connected to the internet
if(isNetworkAvailable(context)){
Log.i("internet", "phone is connected");
Intent simChangedService = new Intent(context, SimChangedService.class);
SimChangedService.startYourService(context, simChangedService);
} // if phone is not connected to the internet, turns on 3G
else{
Log.i("internet", "phone is not connected");
setMobileDataEnabled(context, true);
Log.i("internet", "Mobile data enabled");
Intent simChangedService = new Intent(context, SimChangedService.class);
SimChangedService.startYourService(context, simChangedService);
}


}


} catch (Exception e) {
Log.i("SubscriberId", "SubscriberId is null");
Log.i("SubscriberId", e.getMessage());
}
break;
}

这是 Intent Service 中的 onHandleIntent :

public class SimChangedService extends IntentService {
public static void startYourService(Context ctxt, Intent i) {
getLock(ctxt.getApplicationContext()).acquire();
ctxt.startService(i);
}
protected void onHandleIntent(Intent arg0) {
// TODO Auto-generated method stub

Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();

try{
getPhoneLocation();

ArrayList<String> locationList = new ArrayList<String>();
locationList.add(longt);
locationList.add(lat);

//Executes an AsyncTask to send an email
SendEmailTask sendTask = new SendEmailTask(this);
sendTask.execute(locationList);
//Toast.makeText(this, "i have executed sendTask", Toast.LENGTH_LONG).show();
Log.i("AsyncTask", "i have executed sendTask");
scheduleAlarm();
}finally{

PowerManager.WakeLock lock = getLock(this.getApplicationContext());
if (lock.isHeld()) {
lock.release();
Log.i("wakelock", "lock is released");
}
}
}
}

我不调用 registerReceiver(),我在 list 中只有这个:

<receiver
android:name="com.trackyourandroid.SimStateListener"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.SIM_STATE_CHANGED" />
</intent-filter>
</receiver>

一切正常,但我在 LogCat 中遇到错误:“调用了 registerBroadcastReceiver 两次”。有谁知道解决方案吗?谢谢。

最佳答案

class Bla extends BroadcastReceiver {

static int mSimState = -1;


public void onReceive(final Context context, Intent intent) {
int simState = telephoneMgr.getSimState();
if(simState != mSimState) {
mSimState = simState;
//………the rest of your code………switch (simState) {
}
}
}

关于android - "Called registerBroadcastReceiver twice"在 android 中检查 Sim 卡状态时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22583908/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com