简体   繁体   English

Android:BroadcastReceiver上的AlarmManager生命周期

[英]Android: lifecycle of AlarmManager on BroadcastReceiver

I want to wake an activity on a specific time. 我想在特定时间唤醒一项活动。 To do so, I (also) registered AlarmManager on a Boot complete BroadcastReceiver. 为此,我(也)在Boot complete BroadcastReceiver上注册了AlarmManager。

As far as I know, only the Service can last forever to be alive in the background to wake an Activity. 据我所知,只有服务可以永远持续在后台活着才能唤醒活动。 Activities and BroadcastReceiver can die on inactivity. 活动和BroadcastReceiver可以在不活动时死亡。

The following code I have doesn't run a service. 我有以下代码不运行服务。 However, it seems like it's working (Alarm works even if I launch it after 24 hours). 但是,它似乎正在工作(即使我在24小时后启动它也会起作用)。 Is this safe? 这样安全吗? Or should I launch a service? 或者我应该推出一项服务? If this is safe to use, what's the logic behind this? 如果这是安全的,这背后的逻辑是什么? why is AlarmManger created by BroadcastReceiver doesn't get destroyed by Android lifecycle manager? 为什么由BroadcastReceiver创建的AlarmManger不会被Android生命周期管理器破坏?

public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if(Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            registerAlarm(context);
        }
    }

    private void registerAlarm(Context context)
    {
        Intent intent = new Intent(context, Alarm.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am =(AlarmManager)context.getSystemService(Activity.ALARM_SERVICE);
        long nextAlarm = System.currentTimeMillis() + 10000; //Some time later.
        am.set(AlarmManager.RTC_WAKEUP, nextAlarm, pendingIntent);
    }
}

Is this safe? 这样安全吗?

AlarmManager does not care what sort of PendingIntent you use. AlarmManager不关心你使用什么样的PendingIntent

Users may or may not appreciate an activity appearing out of nowhere, but that is a separate issue. 用户可能会或可能不会欣赏突然出现的活动,但这是一个单独的问题。

As far as I know, only the Service can last forever to be alive in the background to wake an Activity. 据我所知,只有服务可以永远持续在后台活着才能唤醒活动。

Services do not last forever. 服务不会永远持续下去。

Activities and BroadcastReceiver can die on inactivity. 活动和BroadcastReceiver可以在不活动时死亡。

No. Your process can "die on inactivity", which includes all components in that process. 不会。您的流程可能会“死于不活动”,其中包括该流程中的所有组件。

why is AlarmManger created by BroadcastReceiver doesn't get destroyed by Android lifecycle manager? 为什么由BroadcastReceiver创建的AlarmManger不会被Android生命周期管理器破坏?

Because AlarmManager is a system service, maintained by the OS, not by your app. 因为AlarmManager是一个系统服务,由操作系统维护,而不是由您的应用程序维护。

Services can be killed by Android anytime. Android可以随时杀死服务。 For a detailed analysis on the options available to trigger recurring tasks or long running tasks on android, take a look at this thread - Scheduling recurring task in Android 有关可用于在android上触发重复任务或长时间运行任务的选项的详细分析,请查看此线程 - 在Android中安排定期任务

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

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