5

I'm trying to create BroadcastReceiver without activity/service. While I've no problem registering and executing the code when an activity is present in the code when I remove the activity it fails.

I do register the BroadcastReceiver using the manifest(!) But it is not being called when the activity is removed from the project.

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver android:name="com.ge.test.InstallsListener" >
        <intent-filter>
            <data android:scheme="package" />
            <action android:name="android.intent.action.PACKAGE_ADDED" android:priority="100"/>                
        </intent-filter>
    </receiver>
</application>

Thanks.

Eden
  • 3,696
  • 2
  • 24
  • 24

1 Answers1

11

But it is not being called when the activity is removed from the project.

On Android 3.1 and higher, the user must launch one of your activities before any manifest-registered BroadcastReceiver will work.

See the Android 3.1 release notes, specifically the "Launch controls on stopped applications" section.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks that might be the case. So I've to start a service for this to work? (my application doesn't need a UI) – Eden Jun 12 '13 at 14:17
  • @Eden: "So I've to start a service for this to work?" -- well, nothing will be around to start your service, unless you have some other app on the device that knows to start it. "my application doesn't need a UI" -- in all likelihood, you need it to enable this `BroadcastReceiver`. Beyond that, you need to allow users to control the behavior of your background work, you need to show them your license agreement, you need to show them the documentation, you need to show them contact information for getting technical support, etc. – CommonsWare Jun 12 '13 at 14:20
  • @CommonWare: Thanks for the help. This is exactly the information I needed. However, you have a limited view what applications are, or the scenario I need my application to be installed in. Without going into too much details this application is for a company with 25 employees. So there is no need for license agreement or contact info or UI of any sort. – Eden Jun 13 '13 at 06:36
  • @CommonsWare When you said "The user must launch one of your activities" you mean he just needs to launch the application once after installing, and that application doesn't need to be running in order to receive the broadcast, as long as user don't manually perform a 'force close' in Settings right? I ask because the more threads I read regarding this topic, the more confuse I become :) – Bruce Nov 06 '16 at 05:37
  • @Bruce Correct, although the button in Settings is labeled "Force Stop", at least on English devices. We tend to use "force close" as a reference to an application crash. – CommonsWare Nov 06 '16 at 19:13
  • @CommonsWare Oops my mistake, I meant 'Force Stop' like you said XD. I do notice that occasionally, if I do a swipe remove of my app in the 'most recent apps' screen, the broadcast receiver doesn't get called anymore. But if I immediately re-launch the app, and do a swipe remove again, this time it will work and I couldn't reproduce the behavior. Although so far I'm only encounter this on the emulator and not on the actual device. Will research more to see if this is an emulator bug. Let me know if you think of something. – Bruce Nov 07 '16 at 01:11
  • 1
    @Bruce: Removing a task from the overview screen may cause the process to be terminated, but it should have no impact on manifest-registered receivers. Process termination will get rid of receivers registered via `registerReceiver()`. If you have further concerns in this area, you may want to ask a separate Stack Overflow question. – CommonsWare Nov 07 '16 at 01:15
  • @CommonsWare Thanks for confirming the behavior. Will create a separate question if I still have concerns. Cheers! – Bruce Nov 07 '16 at 01:30
  • @CommonsWare I managed to find out why and how to reproduce the behavior I am seeing. This is because I am debugging the app. When the debugger is attached, removing the app from overview screen causes it to move to 'Force Stop' state (confirmed by going to Settings > app and see that 'Force Stop' button is disabled) every single time. Happens both in emulator and real device. Running the app without attaching the debugger have no issues :) Hope this info helps someone! – Bruce Nov 07 '16 at 01:56