在特定时间发送给用户的通知Android

前端之家收集整理的这篇文章主要介绍了在特定时间发送给用户的通知Android前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用报警管理器在特定时间向用户发送通知.基本上,什么也没有发生,对我来说代码看起来不错.我的Alarm Manager的代码如下:
  1. /** Notify the user when they have a task */
  2. public void notifyAtTime() {
  3. Intent myIntent = new Intent(PlanActivity.this,Notification.class);
  4. AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
  5. PendingIntent pendingIntent = PendingIntent.getService(PlanActivity.this,myIntent,0);
  6.  
  7. Calendar calendar = Calendar.getInstance();
  8. calendar.set(Calendar.HOUR_OF_DAY,12);
  9. calendar.set(Calendar.MINUTE,17);
  10. calendar.set(Calendar.SECOND,00);
  11.  
  12. alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),24*60*60*1000,pendingIntent);
  13. }

通知”类中的通知代码如下:

  1. public class Notification extends Service {
  2.  
  3.  
  4. @Override
  5. public void onCreate() {
  6.  
  7. Toast.makeText(this,"Notification",Toast.LENGTH_LONG).show();
  8.  
  9. Intent intent = new Intent(this,PlanActivity.class);
  10. PendingIntent pending = PendingIntent.getActivity(this,intent,0);
  11. NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
  12. this).setSmallIcon(R.drawable.ic_launcher)
  13. .setContentTitle("Football")
  14. .setContentText("Don't forget that you have Football planned!")
  15. .setContentIntent(pending);
  16.  
  17. NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  18. manager.notify(0,mBuilder.build());
  19. }
  20.  
  21. @Override
  22. public IBinder onBind(Intent intent) {
  23. // TODO Auto-generated method stub
  24. return null;
  25. }
  26. }

在Notification类中设置的Toast也不会出现.我不知道这是真的很傻,我失踪了,但任何帮助将不胜感激!

猜你在找的Android相关文章