- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
所以我有一个 BroadcastReceiver 和 AlarmManager。
假设我像这样创建 Pending Intents:
Intent i;
i = new Intent(context, MyReceiver.class);
i.setAction(MyReceiver.ACTION_1);
i.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
pendingIntent1 = PendingIntent.getBroadcast(context, 1, i, PendingIntent.FLAG_UPDATE_CURRENT);
i = new Intent(context, MyReceiver.class);
i.setAction(MyReceiver.ACTION_2);
i.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
pendingIntent2 = PendingIntent.getBroadcast(context, 2, i, PendingIntent.FLAG_UPDATE_CURRENT);
然后像这样安排闹钟:
now = SystemClock.elapsedRealtime();
long time1 = now + 10 * 1000;
long time2 = time1 + 60 * 1000;
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, time1, pendingIntent1);
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, time2, pendingIntent2);
我现在遇到的问题是我的广播接收器非常可靠地接收到 ACTION_1
的广播,而 ACTION_2
通常不会被传送。因此,onReceive
很少或从不以持有操作 ACTION_2
的 Intent 执行。怎么会这样?我想,*_WAKEUP
确保广播无论如何都在传送?
[2015 年 9 月 15 日更新] - 出于测试目的,我试图在我的 onReceive
方法中打印出一条日志消息。还是行不通。 - 我已经尝试在 AlarmManager 上使用 setExact。还是行不通。 - 我什至尝试过使用 WakefulBroadcastReceiver
。还是行不通。 - 然而,我发现设备在电池充电状态下可以可靠地唤醒。什么会导致这个问题? 我到处都读到,如果警报管理器通过未决 Intent 触发广播接收器,则保证执行广播接收器(并且不会做太多事情在 onReceive
中)。我的手机上是否可能有一些我无法真正反对的积极节能政策(如果没有获得长唤醒锁,请参阅评论)?
[更新 09/19/2015] 我刚刚在 Google Play 上测试了一些闹钟应用程序 ( https://play.google.com/store/apps/details?id=com.alarmclock.xtreme.free ),它也不能可靠地唤醒手机。我想,这真的是一个错误,而不是我的错。我想我会坚持使用唤醒锁解决方案。
最佳答案
我遇到了同样的问题,我找到的解决方案是仅通过操作字符串创建 Intent ,并在 list 中注册该接收器操作。尝试将 Intent 更改为这样的内容:
i = new Intent("com.app.ACTION_ONE");
然后在您的 list 文件中将以下内容添加到您的接收器:
<intent-filter>
<action android:name="com.app.ACTION_ONE" />
</intent-filter>
我的猜测是,如果您不向接收者注册至少 1 个操作,他就会在应用程序终止时死亡。
希望它能成功,祝你好运。
关于android - AlarmManager 并不总是执行 BroadcastReceiver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31971669/
我想在每次触发 AlarmManager 时额外更改 Intent。这可能吗,我如何在 AlarmManager 触发后立即调用它? 代码: public void startCollector(){
public class AlarmManagerClass extends BroadcastReceiver { public static boolean haveInternet
我正在寻找执行简单任务的最有效方法。作为一名新的 android 开发人员,我不太确定这些策略中的哪一个在内存效率方面最适合我的应用程序。我想其中一些方法可能会导致我不知道的线程问题。 所有三个解决方
我的应用程序有一个实时模块,它应该每 60 秒 ping 服务器一次。否则,连接将断开,设备将需要重新连接。 第一次尝试是使用 Thread.Sleep 使 ping 线程以所需的频率运行。第二次尝试
我有一组在重启后需要保留的警报。我试过在启动接收器上使用,但它们不会重新启动。我不确定我是否了解启动接收器以及如何重新启动所有警报。我已经有一个接收器来接收我的通知,但不知道我是否可以使用同一个接收器
我有一个要在其中创建警报的 Android 应用程序。这是一个正常的闹钟,应该在指定的时间叫醒你。与任何警报一样,您还可以通过在创建警报时选择声音文件来指定它将使用哪种警报声音。 闹钟效果很好,它在正
我的任务是定期读取后端的手机传感器(例如 WiFi、加速度计)。 我目前的解决方案是使用 AlarmManager。 具体来说,我们有: 在“主”程序(一个 Activity )中,我们使用 Pend
public void SetAlarm(Context context, int sec) { AlarmManager am=(AlarmManager)context.getSyst
我已经通过警报管理器制作了简单的警报,问题是当插入很多秒时,它无法及时工作,这是我在这里声明的 这是代码 mo = ((Integer.parseInt(mons.getText().toStrin
我在应用程序中设置了一个闹钟,每 X 分钟触发一次通知。第一次有效,但随后就不重复了。 我正在开发的 API 是 API 18 (Android 4.3)。 MainActivity.class pu
我很难专心于对 AlarmManager 进行编程。 下面的代码几乎只是将当前日期分配给文本字段并获取一些 XML 并使用 setListAdapter 显示它。我希望这段代码在启动时运行,然后每 3
我对如何停止 AlarmManager 有疑问。我想在“x”小时开始并在“y”小时停止,由用户设置。我知道“.cancel”功能的存在,但不知道如何设置“y”小时内的停止时间。 创建时 final E
我的应用程序有一个 AlarmManager 设置为每 60 秒触发一次。它会触发一个 fragment ,该 fragment 检查当前时间并查找应用程序日历中当时是否有任何事件发生。它运行得很好,
我正在尝试安排使用 AlarmManager 调用的方法,但它似乎不起作用。我看过其他例子,但他们的例子不适合我。所以我认为这是我的代码中的一些内容。这是 AlarmManager 代码: Alarm
我将 AlarmManager 设置如下: Intent startIntent = new Intent(context, MyService.class); PendingIntent pend
我正在尝试使用警报管理器发出警报。应用程序运行时没有任何错误消息,但没有任何反应。 我尝试了来自developer.android.com的解决方案和来自stackoverflow的建议。我也尝试复制
public class background_alarm extends BroadcastReceiver { @Override public void onReceive(Context co
我编写了一个每天凌晨 5:22 运行的闹钟代码。该代码在第一个时间间隔内运行良好,但在第二个时间间隔内会在 24 小时之前触发。 我在 MainActivity 的 onCreate() 方法中添加了
我在这些类(class)中有两个类(class),A,B。他们两个我都有一个像下一个这样的警报管理器; class A{ PendingIntent sender; AlarmManager am
我在 9:00 和 21:00 有两个 AlarmManager,但只运行第二个闹钟。 ////////9:00am///////// Intent aviso = new Inten
我是一名优秀的程序员,十分优秀!