简体   繁体   English

在后台启动时启动android活动

[英]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. 如果您需要没有UI的Activity ,则可能需要一项服务。

Move the logic you need to execute at boot from your Activity to a Service (more info on it here ). 将您需要在启动时执行的逻辑从“ Activity转移到“ Service此处有更多信息)。

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 然后,为了启动它,只需更改您的意图以使用typeof(MyServiceClass) ,设置您可能需要的任何标志,然后调用StartService而不是StartActivity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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