繁体   English   中英

在后台启动时启动android活动

[英]Starting android activity on boot in Background

我想在后台启动完成后启动应用程序。 我不想显示用户界面。

这是我的代码,就像我单击其图标一样,它启动了应用程序,但是我需要将其隐藏!

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

        }
    }
}

如果您需要没有UI的Activity ,则可能需要一项服务。

将您需要在启动时执行的逻辑从“ Activity转移到“ Service此处有更多信息)。

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

暂无
暂无

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

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