启动 Activity 而不将应用程序置于前台
我正在为我的媒体播放器开发锁屏替代品,但我在某一方面遇到了麻烦。
锁定屏幕本身是一项新活动,每当屏幕关闭时,我的媒体服务都会启动该活动。问题是 Activity 使用应用程序上下文来启动,这导致它在用户解锁后将应用程序带到前面。
我尝试使用服务上下文来启动活动,但我相信这不起作用,因为服务本身链接到应用程序。
以下是启动该活动的服务中的代码:
Intent mLock = new Intent(context, LockScreen.class);
mLock.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(mLock);
有人对如何启动此活动并使我的应用程序保持在后台有任何建议吗?
I am working on a lock screen replacement for my media player, and I am having trouble with one aspect.
The lock screen itself is a new activity that is launched by my media service whenever the screen turns off. The problem is that the Activity uses the applications context to launch, which causes it to bring the app to the front after the user unlocks.
I have tried using the services Context to start the activity, but I believe this does not work because the service itself is linked to the application.
Below is the code in the service that launches the activity:
Intent mLock = new Intent(context, LockScreen.class);
mLock.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(mLock);
Does anyone have any suggestions on how I can launch this activity and keep my app in the background?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在清单中设置锁定屏幕的任务关联性。这应该可以在不使用 FLAG_ACTIVITY_MULTIPLE_TASK 的情况下解决您的问题。
In the manifest set the task affinity of the lock screen. That should sort you out without using FLAG_ACTIVITY_MULTIPLE_TASK.
经过大量搜索后,我通过添加
FLAG_ACTIVITY_MULTIPLE_TASK
获得了我想要的结果。这允许锁定活动开始和结束,而无需将父应用程序带到前面。我将继续测试这个解决方案并发布我发现的任何缺点。如果其他人想对此发表意见,请随意……我知道这面旗帜过去曾引起过批评。
谢谢,
乔什
After a lot of searching I was able to get the results I desired by adding a
FLAG_ACTIVITY_MULTIPLE_TASK
. This allows the lock activity to start and end without bringing the parent application to the front.I will continue to test this solution and post any drawbacks I find. If anyone else would like to chime in on this feel free...I know this flag has drawn criticism in the past.
Thanks,
Josh
我对你的问题有点不清楚。但我希望我做对了。
如果您想激活锁定活动,并且当它解锁时,您不想看到调用锁定活动之前运行的活动(例如活动 X),则在调用锁定活动之前关闭此活动(X) 。
如果您希望 X 即使在锁定活动处于活动状态时也运行,但在解锁时关闭,则使用标志。解锁后设置一个标志,并允许 X 定期检查该标志状态。
希望我能够帮助你。
I'm a little bit unclear about your question. But I hope I got it right.
In case you want to activate the lock activity and when it is unlocked you don't want to see the activity(say activity X) that was running before the lock activity was called, then close this activity(X) before calling the lock activity.
If you want X to run even when the lock activity is active, but closed when it is unlocked, then use flags. Set a flag once it is unlocked and allow X to periodically check for this flag status.
Hope I was able to help you.