繁体   English   中英

使用Intent标志启动活动会在启动后停止服务

[英]Starting Activity With Intent Flags Stops Service After It Starts

在我的应用中,有一项活动通过与

Intent intent = new Intent(MainActivity.this, OtherClass.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startService(new Intent(MainActivity.this, Service.class));
startActivity(intent);

问题在于,在意图上设置的标志使它成为可能,以便在服务类中调用onStartCommand()函数之后, onDestroy()函数立即在同一服务类中运行。 我需要在意图上设置标志,以使用户无法返回MainActivity。 有谁知道我可以在不停止服务的情况下实现这种效果?

尝试在另一个进程中运行该服务。

在清单中添加

<service
            android:name=".MyService"
            android:enabled="true"
            android:process=":my_process" />

我最终调用了finish(),当在启动意图时使用它可以达到预期的效果。

暂无
暂无

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

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