我正在尝试使用报警管理器在特定时间向用户发送通知.基本上,什么也没有发生,对我来说代码看起来不错.我的Alarm Manager的代码如下:
/** Notify the user when they have a task */ public void notifyAtTime() { Intent myIntent = new Intent(PlanActivity.this,Notification.class); AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); PendingIntent pendingIntent = PendingIntent.getService(PlanActivity.this,myIntent,0); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY,12); calendar.set(Calendar.MINUTE,17); calendar.set(Calendar.SECOND,00); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),24*60*60*1000,pendingIntent); }
public class Notification extends Service { @Override public void onCreate() { Toast.makeText(this,"Notification",Toast.LENGTH_LONG).show(); Intent intent = new Intent(this,PlanActivity.class); PendingIntent pending = PendingIntent.getActivity(this,intent,0); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( this).setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Football") .setContentText("Don't forget that you have Football planned!") .setContentIntent(pending); NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.notify(0,mBuilder.build()); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } }
在Notification类中设置的Toast也不会出现.我不知道这是真的很傻,我失踪了,但任何帮助将不胜感激! 原文链接:https://www.f2er.com/android/313039.html