在广播接收器中准备好SIM卡时启动应用程序

时间:2014-02-10 13:16:36

标签: android broadcastreceiver telephonymanager

我想在SIM READY STATE上启动一个应用程序

我的广播接收器类执行BOOT_COMPLETED但无法广播SIM_STATE_CHANGED

 @Override
        public void onReceive(Context context,Intent intent) {


          if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
              TelephonyManager telephoneMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                int simState = telephoneMgr.getSimState();

                switch (simState) {
                    case TelephonyManager.SIM_STATE_ABSENT:
                        Log.i("SimStateListener", "Sim State absent");
                        break;
                    case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
                        Log.i("SimStateListener", "Sim State network locked");
                        break;
                    case TelephonyManager.SIM_STATE_PIN_REQUIRED:
                        Log.i("SimStateListener", "Sim State pin required");
                        break;
                    case TelephonyManager.SIM_STATE_PUK_REQUIRED:
                        Log.i("SimStateListener", "Sim State puk required");
                        break;
                    case TelephonyManager.SIM_STATE_UNKNOWN:
                        Log.i("SimStateListener", "Sim State unknown");
                        break;
                    case TelephonyManager.SIM_STATE_READY:
                        Log.i("SimStateListener", "Sim State ready");
                        Intent i = new Intent(context, MainClass.class);
                        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(i);   

            }

but if i run it like 

    @Override
        public void onReceive(Context context,Intent intent) {

              if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
                            Intent i = new Intent(context, MainClass.class);
                            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            context.startActivity(i);   

                }

启动我的应用程序,如何在SIM状态准备就绪时启动我的应用程序?

1 个答案:

答案 0 :(得分:0)

在您的清单中,添加READ_PHONE_STATE权限

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

并将其添加到<receiver>

<intent-filter>
    <action android:name="android.intent.action.SIM_STATE_CHANGED" />
</intent-filter>
相关问题