Android的AlarmManager [英] Android AlarmManager

查看:104
本文介绍了Android的AlarmManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人可以告诉我如何使用 AlarmManager 在Android的一些样本code。

Can someone please show me some sample code on how to use an AlarmManager in ِAndroid.

我一直在玩弄一些code几天,它只是将无法正常工作。

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

我需要在20分钟后触发的code块中的 AlarmManager 被设置。

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

推荐答案

一些示例code是不是那么容易当谈到 AlarmManager

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

下面是一个片段展示的设置 AlarmManager

Here is a snippet showing the setup of 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()。如果你想一杆报警,你只需要使用设置()。一定要给予时间报警启动的同时基地为您在初始参数使用设置()。在我上面的例子中,我使用 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().

下面是显示这种技术更大的样本项目。

Here is a larger sample project showing this technique.

这篇关于Android的AlarmManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆