0

我正在尝试设置警报管理器,以便我的应用程序每周日发送通知,但我似乎无法得到它。这是我的代码:

此代码位于名为“StatusOfChild.java”的类的 onCreate 方法中

Intent myIntent = new Intent(StatusOfChild.this , myService.class);     
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getService(StatusOfChild.this, 0, myIntent, 0);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.DAY_OF_WEEK, 1);
    calendar.set(Calendar.HOUR_OF_DAY, 12);
    calendar.set(Calendar.MINUTE, 00);
    calendar.set(Calendar.SECOND, 00);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent);  //set repeating every 24 hours

此代码位于名为“myService.java”的类中

public class myService extends IntentService{

public myService(){
    super("myService");
}


@Override
protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub
    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notify = new Notification(R.drawable.main1,"Its Time to Eat",1000);

    Context context = myService.this;
    CharSequence title = "Its Time to Eat";
    CharSequence details = "Click Here to Search for Restaurants";
    Intent myIntent = new Intent(context,StatusOfChild.class);
    PendingIntent pending = PendingIntent.getActivity(context, 0, myIntent, 0);
    notify.setLatestEventInfo(context, title, details, pending);
    nm.notify(0,notify);
}

}

4

1 回答 1

0

在 Calender(calendar) 中,您需要设置 MONTH 和 YEAR。请以适当的格式(数据和时间格式)打印日历的值;以便您知道您为alarmManager 设置的时间。

于 2013-01-04T21:26:44.827 回答