2
我需要在特定时间每天显示通知(例如:4.25PM)。我正在使用AlarmManager
和Notification
。我在应用程序的着陆页上设置了警报。我也为它执行了BroadcastReceiver
。使用AlarmManager在特定时间显示通知
代码设置报警:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 16);
calendar.set(Calendar.MINUTE, 25);
calendar.set(Calendar.SECOND, 0);
Intent notificationmassage = new Intent(getApplicationContext(),Notificationmassage.class);
//This is alarm manager
PendingIntent pi = PendingIntent.getService(this, 0 , notificationmassage, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);
这是广播接收器
public class Notificationmassage extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
showNotification(context);
}
private void showNotification(Context context) {
Log.i("notification", "visible");
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, Notificationmassage.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("xyz")
.setContentText("It will contain dummy content");
mBuilder.setContentIntent(contentIntent);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
有什么不对的这种做法?
嗨。什么是问题...对不起滚动下来。那么我能看到的唯一可能会导致问题的是如果这个人没有打开应用程序会发生什么。 – Pintac 2014-09-25 11:22:21
嘿,我需要在特定的时间发送通知扔android应用程序,我已经完成上面的代码,但它不工作.. – 2014-09-25 11:24:53
嗨。定义不工作。它是没有得到广播或它不显示通知 – Pintac 2014-09-25 11:26:34