I want to implement a getSimstate() listener in my service class. I'm not able to do that.
I know it can be done using sim listeners in Telephony class, but I didn't find any code to implement it.
Is code available to listen to wheth开发者_JAVA技巧er a SIM is ready or not?
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) {
}
});
in Manifest File
<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 class
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 <--");
}
}
}
精彩评论