Android: launching Sim Sim time for HTC One V

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) { // TODO Auto-generated method stub Intent intent=new Intent(context, SimService.class); context.startService(intent); } } 

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 { /** Called when the activity is first created. */ 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> 
+4
source share
1 answer

Starting with Android 3.1, during installation, all applications are installed on STOPPED STATE . The application starts from STOPPED STATE when the user launches the application for the first time. The application is also placed in STOPPED STATE when the user uses the application manager to manually stop it.

Since your application contains only the Service component and the BroadcastReceiver component, it is obviously never started by the user by the user (there is no Activity to start the user). Therefore, your application is never inferred from STOPPED STATE .

Your BroadcastReceiver will never be launched because the system does not send broadcast Intent applications to STOPPED STATE .

Read more about it here . See the section entitled “Launching controls on stopped applications”. Pay particular attention to this quote:

Please note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcasts. It does this to prevent broadcasts from the background of services from unintentionally or unnecessary component launches of stopped applications. The background service or application can override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES flag to broadcast intentions that must be allowed to activate the application.

Applications are in a stopped state when they are first installed, but not yet launched and when they are manually stopped by the user (in the "Application Management" section).

0
source

Source: https://habr.com/ru/post/1438703/


All Articles