android sim卡听众

时间:2011-02-08 09:08:41

标签: android

全部

我想在我的服务类中实现一个getSimstate()侦听器。我无法做到这一点。

我知道可以在Telephony类中使用sim侦听器来完成。但我没有得到任何代码来实现它。

如果有人有代码要听,那么枯萎已经准备好了......请给我......

2 个答案:

答案 0 :(得分:2)

final TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

        tm.listen(new PhoneStateListener() {
            @Override
            public void onServiceStateChanged(ServiceState serviceState) {
                //Code in here executed when the sim state has changed
            }

            @Override
            public void onDataConnectionStateChanged(int state) {

            }
});

答案 1 :(得分:1)

清单文件中的

   <receiver
        android:name=".SimChangedReceiver"
        android:enabled="true"
        android:process=":remote" >
        <intent-filter>
            <action android:name="android.intent.action.SIM_STATE_CHANGED" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

SimChangedReceiver类

public class SimChangedReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equalsIgnoreCase("android.intent.action.SIM_STATE_CHANGED")) {
            Log.d("SimChangedReceiver", "--> SIM state changed <--");   
        }
    }
}