AlarmManager为Android [英] AlarmManager for Android

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

问题描述

在跌破code,它做的主要事情是我想要的,到目前为止: - 同时重复每小时

有人可以验证是否或什么,我需要做的,以确保下列项目?

(1)的警报最终将基于每月的天。只要熄灭,当他们醒悟自己的手机最多(以节省电池)。它不是小时或分钟特有的。只有一天。如何才能实现这一目标?

(2)如果活动被破坏,或手机重新启动,我不知道如果我的AlarmManager保持清醒?

(3)最后,这code是重复每次应用程序启动(从而覆盖任何现有AlarmManagers;这是一个做事的正确方法,或者我应该检查是否报警存在

 的(INT I:AlarmDays){

        如果(将String.valueOf(一)== NULL){
            继续;
        }

        日历CAL = Calendar.getInstance();
        如果(cal.get(Calendar.MINUTE)> =ⅰ)
            cal.add(Calendar.HOUR,1);
        cal.set(Calendar.MINUTE,我);

        意向意图=新的意图(这一点,TimeAlarm.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(这个,我,
                意图,PendingIntent.FLAG_CANCEL_CURRENT);
        am.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),
                60 * 60 * 1000,pendingIntent);
    }

// TimeAlarm.class
公共无效的onReceive(上下文的背景下,意图意图){


    字符串DebtName = NULL;

    纳米=(NotificationManager)上下文
            .getSystemService(Context.NOTIFICATION_SERVICE);
    从CharSequence的=到期还款;
    CharSequence的消息=打开应用程序和更新您的平衡;
    意图notificationIntent =新的意图(背景下,ManageDebts.class);
    notificationIntent.getExtras();
    PendingIntent contentIntent = PendingIntent.getActivity(上下文,0,
            notificationIntent,0);
    注意notif =新的通知(R.drawable.icon,买单
            + DebtName +的今天!,System.currentTimeMillis的());
    notif.setLatestEventInfo(上下文,从,消息,contentIntent);
    notif.defaults = Notification.DEFAULT_SOUND
            | Notification.DEFAULT_LIGHTS;
    nm.notify(1,notif);
}
 

和在我的清单我的应用程序标签:

编辑:

 <使用-权限的Andr​​oid:名称=android.permission.RECEIVE_BOOT_COMPLETED/>
 <应用>
  <接收器
        机器人:名称=。TimeAlarm
        机器人:启用=真
        机器人:出口=真正的>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.BOOT_COMPLETED/>
        &所述; /意图滤光器>
    < /接收器>
 < /用途>
 

解决方案

在AlarmManager会的没有的持续跨越手机被重新启动。然而,它的将会的坚持在应用程序被杀害了Android调度。正因为如此,你基本上需要:

  • 存储你的日程安排的地方,并拿出一个调度算法所需的下一个时间 AlarmManager 决定火灾。
  • 在每次你得到一个报警时间,安排一个新的。
  • 启动 AlarmManager 开机,由捕 BOOT_COMPLETED 播出。

In the below code, it is doing the main thing what I want so far: - Repeats every hour at the same time

Can someone verify if or what I need to do to make sure of the following items?

(1) The alarm will eventually be based on days of a month. As long as it goes off when they wake their phone up (to save battery). It is not hour or minute specific. Only day. How can this be achieved?

(2) If the Activity is destroyed or the phone is rebooted, I am not sure if my AlarmManager stays awake?

(3) Lastly, This code is repeated every time the app starts (thus overwriting any existing AlarmManagers; is this a proper way of doing things, or should I be checking to see if an Alarm exists?

 for (int i : AlarmDays) {

        if (String.valueOf(i) == null ) {
            continue;
        }

        Calendar cal = Calendar.getInstance();
        if (cal.get(Calendar.MINUTE) >= i)
            cal.add(Calendar.HOUR, 1);
        cal.set(Calendar.MINUTE, i);

        Intent intent = new Intent(this, TimeAlarm.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, i,
                intent, PendingIntent.FLAG_CANCEL_CURRENT);
        am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                60 * 60 * 1000, pendingIntent);
    }

//  TimeAlarm.class
public void onReceive(Context context, Intent intent) {


    String DebtName = null;

    nm = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    CharSequence from = "Payment Due";
    CharSequence message = "Open App and Update your Balance";
    Intent notificationIntent = new Intent(context, ManageDebts.class);
    notificationIntent.getExtras();
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);
    Notification notif = new Notification(R.drawable.icon, "Pay "
            + DebtName + " today!", System.currentTimeMillis());
    notif.setLatestEventInfo(context, from, message, contentIntent);
    notif.defaults = Notification.DEFAULT_SOUND
            | Notification.DEFAULT_LIGHTS;
    nm.notify(1, notif);
}

And in my application tag in my manifest:

EDIT:

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
 <Application>        
  <receiver
        android:name=".TimeAlarm"
        android:enabled="true"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
 </Application>

解决方案

The AlarmManager will not persist across the phone being rebooted. However, it will persist across the application being killed by the Android scheduler. Because of this, you basically need to:

  • Store your schedule somewhere, and come up with a scheduling algorithm for deciding the next time you want an AlarmManager to fire.
  • Each time you get an alarm, schedule a new one.
  • Start the AlarmManager on boot, by catching the BOOT_COMPLETED broadcast.

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

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