如何通过后台服务在Android的具体时间每天重复通知

嗨我正在应用程序,我已经通过后台服务设置用户inputdate和时间的通知。 现在我想每天下午6点设置通知/警报,要求用户是否要添加另一个条目? 我怎样才能做到这一点? 我应该使用相同的后台服务或广播接收器吗? 请给我更好的解决scheme,教程将是一个好主意。 提前致谢。

Solutions Collecting From Web of "如何通过后台服务在Android的具体时间每天重复通知"

首先设置报警pipe理器如下

Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 18); calendar.set(Calendar.MINUTE, 30); calendar.set(Calendar.SECOND, 0); Intent intent1 = new Intent(MainActivity.this, AlarmReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,intent1, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE); am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); 

创build一个广播接收器类“AlarmReceiver”,在onReceive时引发通知

 public class AlarmReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub long when = System.currentTimeMillis(); NotificationManager notificationManager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); Intent notificationIntent = new Intent(context, EVentsPerform.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder( context).setSmallIcon(R.drawable.applogo) .setContentTitle("Alarm Fired") .setContentText("Events to be Performed").setSound(alarmSound) .setAutoCancel(true).setWhen(when) .setContentIntent(pendingIntent) .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}); notificationManager.notify(MID, mNotifyBuilder.build()); MID++; } } 

并在清单文件中为AlarmReceiver类注册接收者:

 <receiver android:name=".AlarmReceiver"/> 

通过报警pipe理器不需要特别的权限来引发事件。

NVRao的答案是正确的,但是不要忘记把receiver标签放在AndroidManifest.xml文件中的application标签中:

 <receiver android:name=".alarm.AlarmReceiver" />