在Android的每一半的n小时更新的通知 [英] updating notification in android every half n hour

查看:151
本文介绍了在Android的每一半的n小时更新的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code为显示在状态栏上的通知。我知道的更新可以通过调用来完成 setLatestEventInfo()了,但我想更新到每半小时来完成。
如何保持轨道的时间呢? 是否有人知道有什么功能呢?
我甚至想到了用计数器,它不断得到每经过一个半小时,但再次检索时间是一个问题。

 字符串NS = Context.NOTIFICATION_SERVICE;

NotificationManager mNotificationManager =(NotificationManager)getSystemService(NS);

INT图标= R.drawable.index1;
CharSequence的tickerText =你好;

时长= System.currentTimeMillis的();
通知通知=新的通知(图标,tickerText时);
上下文的背景下= getApplicationContext();
CharSequence的notificationTitle =我的通知;
CharSequence的notificationText =的Hello World!;

意图notificationIntent =新的意图(这一点,NotificationAppActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(此,0,notificationIntent,0);
notification.setLatestEventInfo(背景下,notificationTitle,notificationText,contentIntent);

最终诠释HELLO_ID = 1;

mNotificationManager.notify(HELLO_ID,通知);
notification.defaults | = Notification.DEFAULT_SOUND;
notification.defaults | = Notification.DEFAULT_VIBRATE;
notification.defaults | = Notification.DEFAULT_LIGHTS;
}
 

解决方案

您必须使用的 AlarmManager 并安排定期事件每隔30分钟。然后,你需要处理此事件,并在广播接收器的的onReceive()更新您的通知,但通常发送意图您服务做的工作。

例如code:

 意向意图=新的意图(这一点,MyAlarmBroadcastReceiver.class);
PendingIntent发送= PendingIntent.getBroadcast(此,0,意向,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager AM =(AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
长期经常性=(30 * 60000); //以毫秒为单位
am.setRepeating(AlarmManager.RTC,Calendar.getInstance()getTimeInMillis(),反复出现,发送者);
 

和您的 MyAlarmBroadcastReceiver 是正规的BroadcastReceiver 与code。在的onReceive() 。我preFER使用一个广播接收器,所以我也有一些额外的数据添加到意图,所以我的广播接收器知道它应该做的,但你可以把它分离出来,如果​​你喜欢的。

 公共类MyBroadcastReceiver扩展的BroadcastReceiver {
   @覆盖
   公共无效的onReceive(上下文的背景下,意图意图){

     // ...做你需要做的...

   }
}
 

I have the code for showing the notification in the status bar. I know the updating can be done by calling setLatestEventInfo() again, but I want the updating to be done in every half an hour.
How do I keep track of the time? Does somebody know any function for that?
I even thought of using counter which keeps getting incremented every half an hour but again retrieving the time is a problem.

String ns = Context.NOTIFICATION_SERVICE;

NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

int icon = R.drawable.index1;
CharSequence tickerText = "Hello";

long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence notificationTitle = "My notification";
CharSequence notificationText = "Hello World!";

Intent notificationIntent = new Intent(this, NotificationAppActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, notificationTitle, notificationText, contentIntent);

final int HELLO_ID = 1;

mNotificationManager.notify(HELLO_ID, notification);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
}

解决方案

You have to use AlarmManager and schedule recurring event every 30 minutes. Then you need to handle this event and in broadcast receiver's onReceive() update your notification, but usually sending Intent to your service to do the job.

Example code:

Intent intent = new Intent(this, MyAlarmBroadcastReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
long recurring = (30 * 60000);  // in milliseconds
am.setRepeating(AlarmManager.RTC, Calendar.getInstance().getTimeInMillis(), recurring, sender);

and your MyAlarmBroadcastReceiver is regular BroadcastReceiver with code in onReceive(). I prefer to use one broadcast receiver so I also add some additional data to the intent so my broadcast receiver knows what it should do, but you can have it separated if you like.

public class MyBroadcastReceiver extends BroadcastReceiver {
   @Override
   public void onReceive( Context context, Intent intent ) {

     // ... do what you need to do here...

   }
}

这篇关于在Android的每一半的n小时更新的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆