关于broadcastreceiver:在android中检查SIM卡状态时”两次调用registerBroadcastReceiver”

"Called registerBroadcastReceiver twice" when checking Sim card state in android

我有一个广播接收器,它使用 TelephonyManager 检查 SIM 卡状态何时发生变化。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    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 服务中的 onHandleIntent :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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(),我只在清单中有这个:

1
2
3
4
5
6
7
8
<receiver
    android:name="com.trackyourandroid.SimStateListener"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
       
    </intent-filter>
</receiver>

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


1
2
3
4
5
6
7
8
9
10
11
12
13
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) {
        }
    }
}