我已经定义了一个接收器,它应该在调用时触发通知。这只有在我立即呼叫接收器时才有效。 我无法安排特定日期。
Calendar c = Calendar.getInstance();
c.set(2014, 4, 18, 2, 20, 0);
AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(getBaseContext().ALARM_SERVICE);
int id = (int) System.currentTimeMillis();
Intent intent = new Intent(this, TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
这是我的Receiver类:
public class TimeAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent paramIntent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent("android.intent.action.VIEW");
intent.setData(Uri.parse("http://www.sdfasdf.ch"));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Create the notification
Notification notification = new Notification(R.drawable.android_anydont_logo, "sdfs", System.currentTimeMillis());
notification.setLatestEventInfo(context, "something something", "darkside",pendingIntent);
// Fire the notification
notificationManager.notify(1, notification);
}
}
这是清单中的定义:
receiver
android:name="com.hack.doneit.utils.TimeAlarm"
android:process=":remote" />
我的问题是我无法设置闹钟以在将来的特定时间触发接收器。特定的一天就足够了。
答案 0 :(得分:0)
问题在于您设置的Month
值。我相信您认为月份April
的值为4.月份starts from 0
的值,所以April will be 3
。
将月份代码更改为Calendar.APRIL
或将值设置为3
c.set(2014, Calendar.APRIL, 17, 20, 43, 0);
这应该有效。除此之外,您的代码也很完美。
注意:您使用Notification
已弃用的API' s。使用Notification Builder