简体   繁体   English

Android AlarmManager 为什么调用 BroadcastReceiver?

[英]Android AlarmManager why calling BroadcastReceiver?

I'm currently working on an app that handles alarms.我目前正在开发一个处理警报的应用程序。 I have reach the point where I can set the alarms using the AlarmManager and everything seems to work fine, but.. In all the examples that I found, and even in the Android official docs, I have seen people using a BroadcastReceiver for the PendingIntent, and then, calling an activity or whatever they need when the alarm fires.我已经到了可以使用 AlarmManager 设置警报的地步,一切似乎都正常,但是.. 在我找到的所有示例中,甚至在 Android 官方文档中,我看到人们使用 BroadcastReceiver 作为 PendingIntent ,然后在警报响起时调用活动或他们需要的任何东西。 However, I have try to just pass a simple activity to the PendingIntent for the AlarmManager like this:但是,我尝试将一个简单的活动传递给 AlarmManager 的 PendingIntent,如下所示:

Intent intent = new Intent (getApplicationContext(), AlarmActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity (this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set (AlarmManager.RTC_WAKEUP, timeToTrigger.getTimeInMillis(), pendingIntent);

And after testing alarms in several contexts (app in foreground, app not running, etc..) I found that the activity is always been called correctly.在几种情况下(应用程序在前台,应用程序未运行等)测试警报后,我发现活动总是被正确调用。

So, my doubt is as simple as: Why do people use a BroadcastReceiver and in the onReceive method call an activity if you can just simple call the activity directly?所以,我的疑问很简单:如果你可以简单地直接调用活动,为什么人们使用 BroadcastReceiver 并在 onReceive 方法中调用活动?

Android frowns on this behaviour. Android 不赞成这种行为。 You shouldn't interrupt the user if he is doing something else.如果他正在做其他事情,你不应该打断他。 This is why you don't want to launch an Activity from AlarmManager .这就是您不想从AlarmManager启动Activity的原因。 Usually, you launch a BroadcastReceiver , which, if it wants to get the user's attention, will post a Notification .通常,您启动一个BroadcastReceiver ,如果它想引起用户的注意,它将发布一个Notification The user can then open the app via the Notification whenever he wants to.然后,用户可以随时通过Notification打开应用程序。

Also, often you just want to perform some background process (like fetching data from a server, or updating some statistics), which doesn't require any user-interaction.此外,您通常只想执行一些后台进程(例如从服务器获取数据或更新一些统计信息),这不需要任何用户交互。 In this case you would also launch a BroadcastReceiver or Service and not an Activity .在这种情况下,您还将启动BroadcastReceiverService而不是Activity

Starting with Android 10 there are more restrictions about background apps launching activities.从 Android 10 开始,对后台应用程序启动活动有更多限制。 Therefore, with current versions of Android it has become more difficult to do this.因此,使用当前版本的 Android 变得更加困难。 See https://developer.android.com/guide/components/activities/background-starts请参阅https://developer.android.com/guide/components/activities/background-starts

So basically, even though it does work, it is not considered "respecting the user" and it probably won't work in the future.所以基本上,即使它确实有效,但它不被认为是“尊重用户”,并且将来可能不会有效。

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

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