0

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

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); 
}

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

4

1 回答 1

2

您可以使用AlarmManager 来安排您的任务。

于 2012-08-16T18:15:22.740 回答