I want to be notified when a SIM card is removed from an Android phone.
My Application runs on GingerBread, until it runs on HTC One V, which runs on ICS and above. Here is my code:
1) Receiver class
package com.TestIt; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class SimEventReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent systemIntent) {
2) Class of service
package com.TestIt; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.telephony.TelephonyManager; import android.util.Log; import android.widget.Toast; public class SimService extends Service { TelephonyManager tele; @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); Log.d("Kanishk", "In Service OnCreate"); int simState = tele.getSimState(); switch (simState) { case TelephonyManager.SIM_STATE_ABSENT: Log.d("kanishk", "onCreate1"); TestItActivity.simState(this); break; case TelephonyManager.SIM_STATE_READY: Log.d("kanishk", "onCreate2"); Toast.makeText(this, "Now Sim is ok", Toast.LENGTH_LONG).show(); break; case TelephonyManager.SIM_STATE_UNKNOWN: Log.d("kanishk", "onCreate3"); Toast.makeText(this, "not Known", Toast.LENGTH_LONG).show(); break; } } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } }
3) Activity package com.TestIt;
import android.R; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.util.Log; public class TestItActivity extends Activity { private static final String tag = "Activity"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(tag, "OnCreate"); setContentView(R.layout.main); } public static void simState() { Log.d(tag, "Sim State"); } }
4) manifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.systemEvent" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".SystemEventActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".BroadCastReceiverS" android:enabled="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> <service android:name=".MyService" > </service> </application> </manifest>
source share