14

This question has been asked few times in stack overflow, but no solution, yet. I have a broadcast receiver for for receiving USB connected action.The broadcast receiver responsibility is , if I get the intent start my application.In the manifest file I have added the receiver . I have the same logic working in GingerBread, but I ICS its not working. Many questions like , broadcast-not-invoking

1: Android Boot-Up BroadCast Not invoking and broadcastreciever-not-working

If I start my app manually once, then from next time on-wards when USB is connected my App starts automatically. tries to answer the same question but no answer. Is there any solution for this in ICS?

This my receiver

        <receiver android:name="com.test.MyReceiver">
                <intent-filter>
                    <action android:name="android.hardware.usb.action.USB_STATE" />
                    <action android:name="android.net.wifi.STATE_CHANGE" />
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
                </intent-filter>
            </receiver>
    and this is my class
    public class MyReceiver extends BroadcastReceiver {
    .
    .
    .

  public void onReceive(Context context, Intent intent) {
.

Am i wrong anywhere?

Thanks in advance --Kozlov

Community
  • 1
  • 1
Kozlov
  • 564
  • 1
  • 7
  • 21
  • I don't have an answer for you, though it might help if I could look at your code and see if there's anything obvious. I do, however, have an application that works just fine, even with a boot listener in ICS, so I can't say for sure what your issue could be. – waxspin Mar 31 '12 at 03:03
  • Hi waxspin, thanx for ur comment.Edit question with manifest and receiver.Can you please check if anything worng?I have necessary permission as well – Kozlov Mar 31 '12 at 03:22
  • I think that I misunderstood the problem as being one where no one had launched the app yet. I will have to defer to **CommonsWare** below, as it appears he has tested this a fair bit. In my particular case, my app works because it has to be opened at least once for it to be useful to the end user. I guess the only thing you'll be able to do here is to adjust your user experience so that this isn't a problem. I just hadn't noticed it with my app, because my particular case didn't need to be readjusted after the change. – waxspin Mar 31 '12 at 14:56

1 Answers1

12

Is there any solution for this in ICS?

It is working correctly. As of Android 3.1, no BroadcastReceiver will work until the user has manually launched an activity. I blogged about this eight months ago.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • just curious, why would they disable this for Broadcast Recievers, but not content providers? I tested this same scenario under both 2.3 as well as ICS. ContentProvider's will work, BRs will not on ICS, but both work under 2.3. – Ben Jun 04 '12 at 18:00
  • 1
    @Ben: Because content providers are not spontaneously used. This was added to help reduce drive-by malware, stuff that installs, hooks a bunch of system broadcasts, and does stuff without any user involvement. – CommonsWare Jun 04 '12 at 18:03
  • yeah, I guess I can see that argument, but still, seems like they could make it a permission rather than completely changing the way the system works. One of the things I liked about Broadcast Receivers is that they were a message bus built right into the OS with guaranteed delivery (if you use sendOrderedBroadcast). Seems that now, the guarantee is gone, so i'm forced to use CP instead, which is fine, except that I lose that beautiful pub/sub model w/ intent filters. – Ben Jun 04 '12 at 18:08
  • 1
    @Ben: Since AFAIK your code can't be sending a broadcast without also having moved the app out of the stopped state, I fail to see what your problem is. – CommonsWare Jun 04 '12 at 18:15
  • I'm sending the broadcast from another app. My case is a unique, but basically, I'm using the BroadcastReceiver as a pub/sub message bus between multiple apps on the same device. – Ben Jun 04 '12 at 20:15
  • 2
    @Ben if you know you want to allow an application to start even if it is stopped, you can use http://developer.android.com/reference/android/content/Intent.html#FLAG_INCLUDE_STOPPED_PACKAGES just keep in mind that what you do by this is move the application into the launched state, so this should really be done only when there is some connection to the user between what you are broadcasting and the app that is now started. – hackbod Jun 05 '12 at 04:42
  • @hackbod, do you know how we can set an Intent-Flag on an Intent-Filter (in AndroidManifest.xml)? – Hossein Shahdoost Jul 22 '13 at 10:47
  • if your receivers are registered in AndroidManifest then yes, your app will still receive it. On the other hand, if you are registering via code (in service/activity), then the app won't receive it – Usman Riaz Jun 04 '14 at 10:15