繁体   English   中英

警报管理器和通知Android

[英]Alarm Manager and Notifications Android

我正在尝试设置一个警报管理器,以便我的应用程序将在每个星期日发送通知,但我似乎听不到。 这是我的代码:

该代码在名为“ 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);
}

}

在日历(日历)中,您需要设置月和年。 请以正确的格式(数据和时间格式)打印日历的值; 使您知道为alarmManager设置的时间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM