【发布时间】:2016-06-21 04:57:00
【问题描述】:
我正在尝试使用警报管理器在特定时间向用户发送通知。基本上,什么都没有发生,对我来说代码看起来很好。我的警报管理器代码如下:
/** 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, 0, 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, 0, 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 也没有出现。我不知道我是否真的很愚蠢,但任何帮助将不胜感激! :)
【问题讨论】:
-
你在哪里调用你的服务/
-
嗯.. 我应该在哪里?
-
你可以随心所欲地调用任何地方,但请记住,android会为了省电而杀死后台服务
-
您的服务是否在清单中声明?
-
代码看起来不错。不过,您实际上并不需要服务,广播接收器就可以了。
标签: android notifications alarmmanager