I have a very simple IntentReceiver to receive event when time changes. Here’s the code:
public class IntentRec extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("IntentRec", intent.getAction());
}
}
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<receiver android:name=".IntentRec">
<intent-filter>
<action android:name="android.intent.action.TIME_SET"/>
</intent-filter>
</receiver>
<activity android:name="MyActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
However, I receive the intent while the application is running. But if I shutdown (using Force Close) the app, onReceive is not called. So my question is, do I receive intents only when listener app is running? I thought that intents was designed to run target listener class when the app was not running.
Thanks
The solution is simple: stop clicking Force Close.
On Android 3.1+, Force Close will prevent anything in your app from running again, until the user runs an activity of yours manually, or something else (e.g., third-party app) starts up your app.
UPDATE
I suspect that you are being confused by multiple meanings of the word “stopped”. Let’s walk through the process, avoiding the word “stopped”, to see if it helps.
When your app is first installed on an Android device, is in a state known to some as “snicklefritzed”. While the app is in this “snicklefritzed” state, no manifest-registered
BroadcastReceiverwill work. To move an app out of the “snicklefritzed” state, some third-party app (like the home screen launcher) must explicitly request to run something in your app (like an activity). So, the normal course of events is that the user downloads your app, clicks on the launcher icon for it, and your app is moved into the “normal” state and away from the “snickelfritzed” state. While in the “normal” state, yourBroadcastReceiverwill work fine.Let’s suppose that your
BroadcastReceiveris for theACTION_BOOT_COMPLETEDbroadcast. The “snicklefritzed” state has nothing to do with whether your app is presently running or not — it is dependent only upon if your app has ever run or not. Hence, if the user installs your app, but reboots their phone before doing anything with your app, yourACTION_BOOT_COMPLETEDreceiver will not get control at boot time. If, however, the user runs something in your app, then reboots the phone, your receiver will receive theACTION_BOOT_COMPLETEDbroadcast as normal.Normally, apps move out of the “snicklefritzed” state and never return to that state. One thing that will cause an app to be “snicklefritzed” again is if the user clicks on Force Close for this app in Settings. Here, the user is expressly telling Android that your app is misbehaving and should not run again until the user says otherwise. If, of course, the user launches your activity again, you move back to “normal” state.