Android:如何使用AlarmManager

编程入门 行业动态 更新时间:2024-10-23 07:25:18
本文介绍了Android:如何使用AlarmManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要在设置 AlarmManager 20分钟后触发一段代码。

I need to trigger a block of code after 20 minutes from the AlarmManager being set.

有人可以给我看一下如何在Android中使用 AlarmManager 的示例代码?

Can someone show me sample code on how to use an AlarmManager in ِAndroid?

我一直在玩一些代码几天,它将无法正常工作。

I have been playing around with some code for a few days and it just won't work.

推荐答案

一些示例代码并非如此简单code> AlarmManager 。

"Some sample code" is not that easy when it comes to AlarmManager.

这是一个显示 AlarmManager :

AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); Intent i=new Intent(context, OnAlarmReceiver.class); PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0); mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), PERIOD, pi);

在这个例子中,我使用 setRepeating()。如果你想要一次性警报,你只需要使用 set()。请确保在与 set()的初始参数中使用的时间相同的时间内启动警报的时间。在上面的例子中,我使用 AlarmManager.ELAPSED_REALTIME_WAKEUP ,所以我的时基是 SystemClock.elapsedRealtime()。

In this example, I am using setRepeating(). If you want a one-shot alarm, you would just use set(). Be sure to give the time for the alarm to start in the same time base as you use in the initial parameter to set(). In my example above, I am using AlarmManager.ELAPSED_REALTIME_WAKEUP, so my time base is SystemClock.elapsedRealtime().

这是一个更大的样本项目展示了这种技术。

更多推荐

Android:如何使用AlarmManager

本文发布于:2023-11-27 09:25:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637499.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   Android   AlarmManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!