简体   繁体   English

从服务启动活动到前台

[英]Starting an activity into foreground from a service

Basically, I want to pop up a dialog into foreground from a background service (I know using notification would be more user-friendly, but my app really needs immediate response from user). 基本上,我想从后台服务弹出一个前景对话框(我知道使用通知会更加用户友好,但我的应用程序确实需要用户立即响应)。 I have my service working. 我有我的服务工作。 Then I define an activity that contains the dialog using android:theme="@android:style/Theme.Translucent.NoTitleBar" . 然后我使用android:theme="@android:style/Theme.Translucent.NoTitleBar"定义一个包含对话框的活动。

In the service code, I start the dialog activity like this: 在服务代码中,我启动对话框活动,如下所示:

Intent intent = new Intent(getApplicationContext(), PopUpDialogActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

The dialog can pop up if I'm in my app. 如果我在我的应用程序中,则会弹出对话框。 But if I'm not in my app, the dialog doesn't pop up in foreground (I have to enter my app to see it). 但如果我不在我的应用程序中,对话框不会弹出前景(我必须输入我的应用程序才能看到它)。 How can I start it directly into user focus? 如何直接将其启动到用户关注点?

If your service is bound to an activity then it will die when you exit your activity or app. 如果您的服务绑定到某个活动,那么当您退出活动或应用时,它将会死亡。 Use onPause() and onResume() to unbind and bind to the service so that it doesn't get killed when you exit/return. 使用onPause()和onResume()取消绑定并绑定到服务,以便在退出/返回时不会被杀死。

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

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