即使设置警报的应用程序被终止,如何使警报生效?

发布于 2024-11-27 05:09:07 字数 1213 浏览 1 评论 0原文

我对设置闹钟有点困惑。如果有人可以提供反馈或一些提示,我们将不胜感激。总之,这是我的情况:

在我的应用程序中,我设置了一个闹钟:

////////////////////////
// Set an alarm

// Actually I set a unique id here. The hardcoded value is for simplicity only.
int id = 123454321;

Intent intent = new Intent(context, MyAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(context, id, intent, 0);

long firstTime = SystemClock.elapsedRealtime();
firstTime += 10*1000;

AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 15*1000, sender);
////////////////////////

////////////////////////
// MyAlarm implementation
public class MyAlarm extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        // Do some periodic work here
    }
}
////////////////////////

当我设置闹钟时,一切都很好。当我使用该命令时,我可以看到我的警报:

    adb shell dumpsys alarm

即使我的应用程序进入后台,我也可以看到警报。

接下来,我通过

设置 - 应用程序 - 管理应用程序 - 强制停止手动

终止我的应用程序当我重新发出命令“adb shell dumpsys Alarm”时,我看到我的警报消失了。这让我觉得当我的应用程序进入后台并且框架杀死它以释放资源时,我的警报也会被杀死。但是,我希望有一个警报,该警报将独立于应用程序是否运行而存在。有人有提示如何做到这一点吗?

I have a little confusion with setting an alarm. If anyone could give feedback or some hints it'll be greatly appreciated. In summary here is my situation:

Inside my application I set an alarm:

////////////////////////
// Set an alarm

// Actually I set a unique id here. The hardcoded value is for simplicity only.
int id = 123454321;

Intent intent = new Intent(context, MyAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(context, id, intent, 0);

long firstTime = SystemClock.elapsedRealtime();
firstTime += 10*1000;

AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 15*1000, sender);
////////////////////////

////////////////////////
// MyAlarm implementation
public class MyAlarm extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        // Do some periodic work here
    }
}
////////////////////////

When I set the alarm everything is fine and good. I can see my alarm when I use the command:

    adb shell dumpsys alarm

I can see the alarm even when my application goes to the background.

Next I manually kill my application through

Settings - Applications - Manage applications - - Force stop

When I re-issue the command "adb shell dumpsys alarm" I see that my alarm is gone. This makes me think that when my application goes into the background and the framework kills it to free resources, my alarm will also be killed. However, I would like to have an alarm which will live independent of whether the application is running or not. Does anybody have hints how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

得不到的就毁灭 2024-12-04 05:09:07

我有一个应用程序可以设置闹钟,然后就消失。

我使用的代码是:

清单:

    <receiver android:name=".AlarmReceiver" android:process=":remote"></receiver>

PendingIntent 代码:

    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.putExtra("ALARM_MESSAGE", alarmMessage);
    PendingIntent receiverIntent =
        PendingIntent.getBroadcast(context,
        MyConstants.UPDATE_ALARM, intent,
        pendingIntent.FLAG_UPDATE_CURRENT);

设置(实际上取消旧的并重置):

    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.cancel(receiverIntent); // get rid of previous if unfired.
    am.set(AlarmManager.RTC_WAKEUP, calcTime.afterMidnight(), receiverIntent);

pendingIntent 触发接收器,接收器执行一些快速工作,发送通知,然后消失。接收者不必发布通知。它可以有意图地启动一项活动,发送一条祝酒消息等。

我有一个接收器,可以在启动后恢复警报。 (断电时警报会消失。)

I have an app that sets an alarm and then goes away.

The code I use is:

Manifest:

    <receiver android:name=".AlarmReceiver" android:process=":remote"></receiver>

PendingIntent Code:

    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.putExtra("ALARM_MESSAGE", alarmMessage);
    PendingIntent receiverIntent =
        PendingIntent.getBroadcast(context,
        MyConstants.UPDATE_ALARM, intent,
        pendingIntent.FLAG_UPDATE_CURRENT);

Set (actually cancel old and reset):

    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.cancel(receiverIntent); // get rid of previous if unfired.
    am.set(AlarmManager.RTC_WAKEUP, calcTime.afterMidnight(), receiverIntent);

The pendingIntent fires the receiver, the receiver does some quick work , sends a notification, and goes away. The receiver doesn't have to post a notification. It can start an activity with an intent, do a toast message, etc.

I have a receiver that restores the alarm after a boot. (The alarm does go away at power down.)

不语却知心 2024-12-04 05:09:07

我猜你的接收者对象尚未在清单中注册。如果您在清单文件中注册了接收器对象,那么即使您的应用程序没有运行,系统也会找到它并调用。

I guess your receiver object has not been registered in the manifest. If you registered the receiver object in the manifest file then the system will find it and call even if your application's not running.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文