警报管理器通知设置Cookie

时间:2014-10-01 11:56:31

标签: java android cookies notifications alarmmanager

尝试根据Android中的Alarm Manager设置的时间提示通知时遇到问题。

所以我要做的是预算设置应用程序。如果每月费用超过预算,则会提示通知。为了使问题简短,我将发布与Alarm Manager交互的部分:

AlarmManager mgr = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        Intent notificationIntent = new Intent(context, BudgetAlarm.class);
        PendingIntent pi = null;
        if (bm.getReminderNotify().equals("Y")
                && percentage >= 90) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 1);
            notificationCount = notificationCount + 1;

            // Set request flag to 1 so that the same pending intent in broadcastReceiver
            notificationIntent.putExtra("NotifyCount", notificationCount);
            pi = PendingIntent.getBroadcast(context, 1,
                    notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                    calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);

我在这里要做的是检查费用是否超过预算。在BudgetAlarm类中,我提示通知:

private NotificationManager mNotificationManager;
private Notification notification;

@Override
public void onReceive(Context context, Intent intent) {
    mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 1,
            new Intent(), 0);
    notification = new Notification(R.drawable.ic_launcher, "Notification",
            System.currentTimeMillis());
    notification.setLatestEventInfo(context, "Budget Reminder",
            "Your monthly expenses nearly exceed the budget!",
            contentIntent);
    mNotificationManager.notify(
            Integer.parseInt(intent.getExtras().get("NotifyCount")
                    .toString()), notification);
}

我现在的问题是它每天都设置并触发警报管理器。但是,让我们说今天我没有运行应用程序,它确实提示我通知我。但过了一段时间,我启动了应用程序,然后再次提示我。

我想知道有没有办法将通知设置为每天只通知一次,无论我启动应用程序多少次。我实际上在想像饼干或共享偏好。

提前致谢。

修改

public class BudgetAlarm extends BroadcastReceiver {
private NotificationManager mNotificationManager;
private Notification notification;

@Override
public void onReceive(Context context, Intent intent) {
    saveDay(context);
    if (sameDay(context) == false) {
        mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 1,
                new Intent(), 0);
        notification = new Notification(R.drawable.ic_launcher,
                "Notification", System.currentTimeMillis());
        notification.setLatestEventInfo(context, "Budget Reminder",
                "Your monthly expenses nearly exceed the budget!",
                contentIntent);
        mNotificationManager.notify(
                Integer.parseInt(intent.getExtras().get("NotifyCount")
                        .toString()), notification);
        saveDay(context);
    }
}

private boolean sameDay(Context context) {
    boolean isSameDay = false;
    SharedPreferences pref = context.getSharedPreferences("PrefKey",
            Context.MODE_PRIVATE);

    Calendar cal = Calendar.getInstance();
    SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
    Date today = cal.getTime();
    String day = format.format(today);

    String savedDate = pref.getString("SaveDateKey", "NONE");

    if (savedDate.equals(day)) {
        isSameDay = true;
    } else {
        isSameDay = false;
    }
    return isSameDay;
}

private void saveDay(Context context) {
    SharedPreferences pref = context.getSharedPreferences("PrefKey",
            Context.MODE_PRIVATE);
    Editor editor = pref.edit();
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
    Date today = cal.getTime();
    String day = format.format(today);
    editor.putString("SaveDateKey", day);
    editor.commit();
}

}

1 个答案:

答案 0 :(得分:0)

我认为你的共享偏好是正确的。这可能是您的应用中的一个场景:制作方法以了解同一天是否相同,如果没有,请保存启动应用时的日期:

  private boolean sameDay(){

  boolean isSameDay = false;
 SharedPreferences pref = getApplicationContext().getSharedPreferences(YOUR_PREF_KEY, Context.MODE_PRIVAT);

 Calendar cal = Calendar.getInstance();
 SimpleDateFormat format =  new SimpleDateFormat("dd.MM.yyyy");
 Date today = cal.getTime();
 String day = format.format(today);

 String savedDate = pref.getString(YOUR_SAVED_DATE_KEY,"NONE");

 if(savedDate.equals(day){
    isSameDay=true;
 }else{
    isSameDay=false;
  } 
    return isSameDay;
}

并保存当天:

private void saveDay(){

  SharedPreferences pref = getApplicationContext().getSharedPreferences(YOUR_PREF_KEY, Context.MODE_PRIVAT);


 Editor editor = pref.edit();

 Calendar cal = Calendar.getInstance();
 SimpleDateFormat format =  new SimpleDateFormat("dd.MM.yyyy");
 Date today = cal.getTime();
 String day = format.format(today);

 editor.putString(YOUR_SAVED_DATE_KEY,day);
 editor.commit();
 }

然后你只需在你的onReceive()中处理这个:

 if(sameDay==true){
    //don´t notify

 }else{
  //notify and save the day
  saveDay();
 }