简体   繁体   中英

Starting android activity on boot in Background

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);

        }
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM