在设定的日期和时间发出通知

时间:2012-11-09 12:58:02

标签: android

我想创建一个在设定的日期和时间触发的通知,我创建了以下功能:

public void notify(View v){
    Context context = AllClasses.this;
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.DAY_OF_WEEK, 4);
    calendar.set(Calendar.HOUR_OF_DAY, 17);
    calendar.set(Calendar.MINUTE, 31);
    calendar.set(Calendar.SECOND, 0);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context, Notify.class), PendingIntent.FLAG_UPDATE_CURRENT);
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
}

和相应的课程:

public class Notify extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
    NotificationManager nm;
    nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);        
    CharSequence from = "VIPUL";
    CharSequence message = "Crazy About Android...";
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
      new Intent(), 0);
    Notification notif = new Notification(R.drawable.android_logo,
      "Crazy About Android...", System.currentTimeMillis());
    notif.setLatestEventInfo(context, from, message, contentIntent);
    nm.notify(1, notif);

}
}

我无法弄清楚缺少什么。我需要某些权限吗?任何帮助非常感谢!

1 个答案:

答案 0 :(得分:1)

我猜您需要使用PendingIntent.getBroadcast(context, requestCode, intent, flags)代替getService方法。