在我的应用程序中,我想实现一个始终更新的持续通知,就像在通知中心运行的闹钟一样。
我尝试制作一个每分钟运行一次的服务来更新通知中心的时间,但遗憾的是我发现它不够高效,因为它耗尽了很多电池!
什么是最有效的解决方案,使总是更新通知不会耗尽电池?
谢谢!
代码:
服务:
public class MyService extends IntentService {
MyClass MyClass;
public PrayerTimeAlarmService() {
super("PrayerTimeAlarmService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent in) {
// TODO Auto-generated method stub
if(MyClass == null){
MyClass = new NextSalah(this);
}
MyClass.updatenotification();
}
}
更新无效:
public updatenotification(){
title = GetTitle();/*the title is dynamically generated based on complex codes*/
timediff = GetTimeDiff(title);/*joda time period and stuff calculations*/
NB.setContentTitle(title);
NB.setContentText(timedif);
NB.setSmallIcon(R.drawable.ic_launcher);
Notification not = NB.build();
not.flags = Notification.FLAG_ONGOING_EVENT;
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(071527/*Just a random integer*/ , not);
}