Forum Xamarin.Android

BroadcastReceiver is never called

Artur.4076Artur.4076 USUniversity ✭✭
edited June 2015 in Xamarin.Android

I wrote BroadcastReceiver. I want Android to run my app after reboot. So I did following:
1) added permission to manifest file:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

2) wrote the broadcast receiver in following way

    [BroadcastReceiver]
    [IntentFilter(new[] { Intent.ActionBootCompleted })]
    public class BootReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
        System.Diagnostics.Debugger.Break ();
            Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();
            writeFile();
        }

        public static void writeFile()
        {
            StreamWriter w = new StreamWriter(new FileStream("/sdcard/Temp/boot.txt", FileMode.OpenOrCreate));
            w.WriteLine("Hello, World!");
            w.Flush();
            w.Close();
        }
    }
}

But it's never called: the file is never created and toast not shown.

Answers

  • Could System.Diagnostics.Debugger.Break (); be blocking the thread?

    Also would a Toast work that way? Wouldn't you need to run it on a UI thread? (I don't use Toast so I'm not too sure actually).

    I think you should also use OnReceive to spawn a new thread, instead of doing the work inside the OnReceive method. Again, this is merely speculation!

    Hopefully someone more knowledgeable in this area will help you out better -but there's a few starting points.

    Cheers,
    Le-roy

  • Artur.4076Artur.4076 USUniversity ✭✭

    To clarify: I test it in following way:
    1) I uninstall my app
    2) I run the app using Xamarin Studio
    3) I close the app (using back button)
    4) I explicitly terminate the app by closing the app process
    5) reboot the device
    6) wait and check whether file was created
    7) nothing happens

  • Artur.4076Artur.4076 USUniversity ✭✭

    From link below I found that: "If your app installed on external storage(SD card), you will never receive Boot Complete action. So you have to specify android:installLocation="internalOnly" in the manifest tag.".
    And my app is installed on the SD card:) I will try to move to the internal and try again soon.
    http://stackoverflow.com/questions/5051687/broadcastreceiver-not-receiving-boot-completed

  • Artur.4076Artur.4076 USUniversity ✭✭

    The problem was in "4) I explicitly terminate the app by closing the app process". If app is forced to close android by security reasons does not allow it to handle broadcast messages.

  • MichalFrantaMichalFranta CZMember ✭✭

    Hi, thank you for this thread.
    In my case, it was also a problem in Section 4. I think it is not due to security but because the application simply must be running in order to receive boot_completed after the restart. Or am I wrong? If not, what happens when the user downloads the application from the store and it will not be running? Then after restart the restart boot_completed do not receive.

Sign In or Register to comment.