2

I want to start the app after boot completing in Background. I don't want to show the UI.

This is my code, it starts the app as if I clicked its icon, but I need to hide it!

[BroadcastReceiver]
[IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted },
    Categories = new[] { Android.Content.Intent.CategoryDefault }
)]
public class ReceiveBoot: BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {

        if ((intent.Action != null) &&
            (intent.Action ==
                Android.Content.Intent.ActionBootCompleted))
        { 

            Android.Content.Intent start = new Android.Content.Intent(context, typeof(Main_Activity));
            start.AddFlags(ActivityFlags.NewTask);
            start.AddFlags(ActivityFlags.FromBackground);
            context.ApplicationContext.StartActivity(start);

        }
    }
}
William Barbosa
  • 4,936
  • 2
  • 19
  • 37
Mouhannad Bar
  • 101
  • 2
  • 6
  • 4
    *I don't want to show the UI*. Then you don't need a Activity. Probably a service. – Raghunandan Aug 18 '15 at 11:28
  • 1
    in short not possible, if you have has atleast 1 activity, starting it after reboot will show UI. If it does not contain and UI then it can be started as a service. For [How to start app after reboot](http://stackoverflow.com/a/2974763/1061944) – Murtaza Khursheed Hussain Aug 18 '15 at 11:28

1 Answers1

3

If you need an Activity with no UI, you probably want a service.

Move the logic you need to execute at boot from your Activity to a Service (more info on it here).

Then, in order to start it, just change your intent a to use typeof(MyServiceClass), set whichever flag you may need and call StartService instead of StartActivity

William Barbosa
  • 4,936
  • 2
  • 19
  • 37