BroadcastEvent在启动android设备时

时间:2012-05-10 09:31:53

标签: java android broadcastreceiver reboot

我和我的小组正在尝试创建一个具有闹钟功能(或通知提醒)的应用,我们希望在特定时间关闭。问题是,当我们启动应用程序时,我们有2个场景

对于当前版本,事件在设备完成启动的同一时间关闭,而不是在它假设关闭时。

或者它永远不会消失。发布此函数的代码,我还要指出,我们对此完全陌生,并且不知道我们在做什么。

private void startAlarm() {

    am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, NotificationEvent.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
            intent, PendingIntent.FLAG_CANCEL_CURRENT );
    am.cancel(pendingIntent);

    Calendar calendar = Calendar.getInstance();     

    calendar.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), 11, 9, 0);

    am.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
    Toast toast = Toast.makeText(getApplicationContext(), "Reminder in " + maleOrFemale + " days.", 4);
    toast.show();
    fm.setAlarmBool(true);
    fm.setAlarmTimeLong(calendar.getTimeInMillis());
}

setAlarmBool和setAlarmTimeLong是文件的2个作者,因此我们可以在应用程序关闭或设备重新启动时存储信息。

这是我们的广播课

public void onReceive(Context context, Intent intent) {

    if("Intent.ACTION_BOOT_COMPLETED".equals(intent.getAction())){

        if(fm.getAlarmBool()){
            time = fm.getAlarmTimeLong();
            reminder.startAlarm(time);
        }
    }
    else{
        nm = (NotificationManager)        context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.blod_launcher, "Nu kan ni l�mna blod igen", System.currentTimeMillis());
        // This is intent, we want to launch when user clicks on the notification.
        Intent intentTL = new Intent(context, Reminder.class);
        notification.setLatestEventInfo(context, "Nu kan ni l�mna blod igen", "Bes�k appen f�r mer info",
                PendingIntent.getActivity(context, 0, intentTL,
                        PendingIntent.FLAG_CANCEL_CURRENT));
        notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
        nm.notify(NOTIF_ID, notification);
        fm.setAlarmBool(false);
    }

}

0 个答案:

没有答案