如何从服务杀活动

2023-09-07 02:57:24 作者:也许没有也许

我有一个主要活动A1运行。它设置一个报警A1,在时刻t1熄灭。报警接收器AR1映入A1报警,并创建一个服务S1。业务S1创建对话活动A2和还设置一个报警A2,在时刻t2熄灭。另一个报警接收机AR2捕捉A2报警,我需要AR2杀对话活动A2。

I have a main activity A1 running. It sets an alarm a1 that goes off at time t1. An alarm receiver AR1 that catches the a1 alarm and creates a service S1. Service S1 creates a dialog activity A2 and also sets an alarm a2 that goes off at time t2. Another alarm receiver AR2 catches the a2 alarm and I need AR2 to kill the dialog activity A2.

我实在看不出该服务可以以杀死它,或者调用其完成()方法获得活动的句柄。

I cannot see how the service can get a handle on the activity in order to kill it or call its finish() method.

推荐答案

首先,从弹出的活动的BroadcastReceiver ,因为它会被视为形式拙劣,除非在特殊情况下,中断的不管它是什么,他们正在做的中间用户。我要承担,就目前而言,你正在编写针对Android的第一千三百三十七闹钟应用程序,因此弹出一个活动从的BroadcastReceiver 基于一个 AlarmManager 报警是一个合理的选择(假定用户通过你的应用程序中的preferences要求的话)。

First, popping up activities from a BroadcastReceiver is considered poor form except in unusual circumstances, since it will interrupt the user in the middle of whatever it is they are doing. I am going to assume, for the moment, that you are writing the 1,337th alarm clock application for Android, and therefore popping up an activity from a BroadcastReceiver based on an AlarmManager alarm is a reasonable choice (assuming the user requested it via preferences within your app).

鉴于这种情况,你不能杀从服务的活动。

Given that, you cannot "kill" an activity from a service.

不过,你可以发送邮件到活动中,如果它正在运行,然后再启动它,如果它没有运行。

However, you can send a message to the activity, if it is running, and then start it if it is not running.

例如,您可以发送一个有序的广播。有活动实施高优先级的的BroadcastReceiver 为您播出,并将其自动更新,以反映新的报警。实现一个低优先级的BroadcastReceiver 其他地方(例如,在你的清单),并将它启动对话活动情况,活动不可用,或采取基于其他一些行动用户preferences(如,只显示一个通知)。

For example, you could send an ordered broadcast. Have the activity implement a high-priority BroadcastReceiver for your broadcast, and have it update itself to reflect the new alarm. Implement a low-priority BroadcastReceiver elsewhere (e.g., in your manifest), and have it start the dialog activity in case the activity is not available, or take some other action based on user preferences (e.g., simply display a Notification).

相关推荐
 
精彩推荐
图片推荐