如何按日期,月份,小时和分钟在android计划上发出通知?

时间:2012-08-16 18:06:56

标签: android date notifications

我有一个带通知的应用程序,我想每年安排一次。在我的代码下面:

private void checkAlarm() {
    Calendar c = Calendar.getInstance();
    int date   = c.get(Calendar.DATE);
    int month  = c.get(Calendar.MONTH);
    int year   = c.get(Calendar.YEAR);
    int hour   = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);


          if (date==17 && month==8 && hour==6 && minute==0){
              showNotification(R.drawable.sphere_notification, "hari ini 17 Agustus");
          }
}


private void showNotification(int icon, String text) {
    Notification notification = new Notification(icon, "Hari ini memperingati Hari Kemerdekaan RI", System.currentTimeMillis());
    notification.flags  = Notification.FLAG_AUTO_CANCEL;
    notification.sound  = Uri.parse("android.resource://app.vmh.main/"+R.raw.agustus); 

    PendingIntent facebook = PendingIntent.getActivity(this, 0,
            new Intent(this, ShareOnFacebook.class),0);

    PendingIntent twitter = PendingIntent.getActivity(this, 0,
            new Intent(this, ShareOnTwitter.class),0);

    Intent exit = new Intent (Intent.ACTION_MAIN);  
           exit.addCategory(Intent.CATEGORY_HOME);  

    PendingIntent mainIntent = PendingIntent.getActivity(this, 0,
            exit,0);

    if (Pengaturan.getSetFacebook(this)){
        notification.setLatestEventInfo(this, "Kemerdekaan - Remainder",
        text, facebook);

    }else if((Pengaturan.getSetFacebook(this)) && (Pengaturan.getSetTwitter(this))){
        notification.setLatestEventInfo(this, "Kemerdekaan - Remainder", 
                text, facebook);

    }else if(Pengaturan.getSetTwitter(this)){
            notification.setLatestEventInfo(this, "Kemerdekaan - Remainder", 
                    text, twitter);

    }else
        notification.setLatestEventInfo(this, "Kemerdekaan - Remainder",
                text, mainIntent);

    // Send the notification.
    mNM.notify(1, notification); 
}

我实施日期和月份时遇到问题,通知无效。如果我使用小时和分钟我的应用程序正在运行。 你可以帮帮我吗?

1 个答案:

答案 0 :(得分:2)

您可以使用AlarmManager to schedule任务。