[英]Repeating notifications on Android 4
目標:如果滿足某些條件,通知每天下午2點出現一次。
示例:為簡單起見,我們考慮每天都滿足使用Internet連接檢查的條件。 如果今天已經在下午2點之后,我們將從明天開始通知。 例如,用戶在星期一下午4點啟動應用程序,並在周二下午2點,周三下午2點,周四下午2點獲得通知,依此類推。
問題:下午2點有第一個通知,但隨后我會一遍又一遍地收到相同的通知。
問題似乎只在Android> = 4.0上。 它在早期的機器人上運行良好。
這是我發送通知的方式:
public class NotifyService extends Service
{
static final int NOTIFICATION_ID = 1;
// ...
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
try
{
Symbol biggest = getBiggestMover();
if (biggest != null)
{
String title = getString(R.string.app_name);
String text = getNotificationText(biggest.symbol, biggest.change);
sendNotification(title, text);
}
}
catch (Exception e)
{
// If there is Internet problem we do nothing, don't want to disturb the user.
e.printStackTrace();
}
return super.onStartCommand(intent, flags, startId);
}
/** @return Symbol which is the biggest mover today. If there is no big mover - null is returned.
* @throws Exception If there is Internet problem. */
private Symbol getBiggestMover() throws Exception
{
Symbol biggest = null;
Symbol[] equities = Network.getTraded(SymbolType.EQUITY);
for (Symbol equity : equities)
{
if (Utilities.isToday(equity.lastTraded) && isBigMove(equity.change) && isBigger(equity, biggest))
{
biggest = equity;
}
}
return biggest;
}
private void sendNotification(String title, String text)
{
Notification notification = new Notification(R.drawable.ic_launcher, text, System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
Intent clickIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, clickIntent, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(this, title, text, pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, notification);
}
// ...
}
因為AlarmManager
,在下午2點調用sendNotification()
:
public class ServiceStarter extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
setNotificationAlarm(context);
}
/** Set repeating notifications every 24 hours. */
public static void setNotificationAlarm(Context context)
{
Intent intent = new Intent(context, NotifyService.class);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
final int oneDay = 24 * 60 * 60 * 1000;
alarmManager.setRepeating(AlarmManager.RTC, getTriggerTime(), oneDay, pendingIntent);
}
private static long getTriggerTime()
{
GregorianCalendar calendar = new GregorianCalendar();
calendar.set(GregorianCalendar.HOUR_OF_DAY, 14);
calendar.set(GregorianCalendar.MINUTE, 0);
calendar.set(GregorianCalendar.SECOND, 0);
calendar.set(GregorianCalendar.MILLISECOND, 0);
if (calendar.before(new GregorianCalendar()))
{
calendar.add(GregorianCalendar.DAY_OF_MONTH, 1);
}
return calendar.getTimeInMillis();
}
}
setNotificationAlarm()
從2個地方調用。 首先,在應用程序的開頭。 其次,從上面的代碼中,當手機重新啟動時( onReceive()
收到BOOT_COMPLETED
)。 我這樣做,因為當用戶關閉手機時, AlarmManager
清除其警報。
所以都應該工作,因為alarmManager.setRepeating()
會覆蓋以前的警報。
我發現有人有同樣的問題,但也沒有答案:
https://groups.google.com/forum/?fromgroups=#!topic/android-developers/t_tDU4PwR3g
在這里我也發現了類似的問題: http : //comments.gmane.org/gmane.comp.handhelds.android.devel/171471
前段時間我問過如何創建這樣的通知,所以這是相關的:
特定時間的日常通知
使用AlarmManager.RTC中的AlarmManager.RTC_WAKEUP
在AlarmManager.RTC中
System.currentTimeMillis()中的鬧鍾時間(以UTC為單位的掛鍾時間)。 此警報不會喚醒設備; 如果在設備處於睡眠狀態時熄滅,則在下次設備喚醒時才會發送。
在AlarmManager.RTC_WAKEUP中的位置
System.currentTimeMillis()中的鬧鍾時間(以UTC為單位的掛鍾時間),它將在設備關閉時喚醒設備。
在ICS +設備上遇到同樣的問題。 我的解決方法非常簡單 - >在顯示通知時將當前時間放在共享首選項中。 在此之前總是檢查間隔是否真的通過,如果不是中止。
long lastnotification = sharedPrefs.getLong("lnnd", -1);
Calendar now = Calendar.getInstance();
if (!namedayalarmEnabled) {
return;
}
if (lastnotification > 1) {
Calendar last = Calendar.getInstance();
last.setTimeInMillis(lastnotification);
long distance = (now.getTimeInMillis() - last
.getTimeInMillis());
if (distance < YOURINTERVAL) {
return;
} else {
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putLong("lnnd", now.getTimeInMillis());
editor.commit();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.