1

Unable to get my Xamarin.Android app to fire Toast after boot. I checked many accepted solutions but none seem to resolve my problem. I've also tried various "working" examples but haven't had any luck so clearly I'm missing something.

Device: Samsung Galaxy S3 API: 19

Android Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.novak" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="16" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <application android:label="ReceiverApp">
    <receiver android:enabled="true"
        android:exported="true"
        android-permission="android.permission.RECEIVE_BOOT_COMPLETED"
        android:name="com.novak.BootReceiver" >

      <intent-filter >
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        <category android:name="android.intent.category.DEFAULT" />
      </intent-filter>
    </receiver>
  </application>
</manifest>

BootReceiver.cs

using Android.App;
using Android.Widget;
using Android.Content;

namespace ReceiverApp
{

    [BroadcastReceiver]
    [IntentFilter(new[] { Intent.ActionBootCompleted })]
    public class BootReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            Toast.MakeText(context, "Receiver", ToastLength.Long).Show();
        }
    }
}
Bob
  • 993
  • 2
  • 10
  • 21
  • Set [BroadcastReceiver (Enabled = true)] attribute in the BootReceiver class, see example here: https://forums.xamarin.com/discussion/19448/example-for-broadcastreceiver-in-android – Alexandre Apr 01 '17 at 13:28
  • 1
    Added [BroadcastReceiver (Enabled = true)] but still not working. Is there another way to test if it's working? Maybe I'm just not seeing the Toast. – Bob Apr 01 '17 at 17:38

2 Answers2

0

It would appear that what I want to do is not possible. Once my app is installed the app sits in a stopped state and must be executed manually the first time before it can receive broadcasts

[How to start a Service when .apk is Installed for the first time

Community
  • 1
  • 1
Bob
  • 993
  • 2
  • 10
  • 21
0

P.S: On real device it takes a while to fire the event, ie: 2 minutes after unlock the screen on my Samsung. On emulator it takes very short.

I wrote the code below to notice when it will fire:

Autorun.cs - Just add this file into your project, that's it, it will start after the boot. No manifest modification needed.

using Android;
using Android.App;
using Android.Content;

// we need to ask permission to be notified of these events
[assembly: UsesPermission (Manifest.Permission.ReceiveBootCompleted)]

namespace XamarinCookbook
{
  // we want this to fire when the device boots
  [BroadcastReceiver]
  [IntentFilter (new []{ Intent.ActionBootCompleted })]
  public class ServiceStarter : BroadcastReceiver
  {
    public override void OnReceive (Context context, Intent intent)
    {

      #region Start chrome

      var mesaj = "Autorun started";
      Android.Widget.Toast.MakeText(Android.App.Application.Context, mesaj, Android.Widget.ToastLength.Long).Show();

      var uri = Android.Net.Uri.Parse("https://500px.com");
      var intent1 = new Intent(Intent.ActionView, uri);
      intent1.AddFlags(ActivityFlags.NewTask);

      intent1.SetPackage("com.android.chrome");
      try
      {
        context.StartActivity(intent1);
      }
      catch (ActivityNotFoundException ex)
      {
        //Chrome browser not installed
        intent.SetPackage(null);
        context.StartActivity(intent1);
      }

      #endregion

      /*
            #region Real code
            // just start the service
            var myIntent = new Intent (context, typeof(XamarinService));
          context.StartService (myIntent);

      #endregion
      */
    }
  }
}

VS Solution:

https://drive.google.com/open?id=1iYZQ2YCvBkyym9-2FvU7KXoBKUWsMdlT

Nime Cloud
  • 6,162
  • 14
  • 43
  • 75
  • What API are you using... there were changes in API Level 26 https://developer.android.com/guide/components/broadcast-exceptions – user1034912 Jan 10 '22 at 14:54