gpt4 book ai didi

android - 在 Android 4 上重复通知

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:06:35 24 4
gpt4 key购买 nike

目标:如果满足特定条件,通知会在每天下午 2 点出现一次。

示例:为简单起见,让我们假设每天都满足通过 Internet 连接检查的条件。如果今天已经是下午 2 点之后,我们将从明天开始通知。例如,用户在星期一下午 4 点启动应用程序,然后他在星期二下午 2 点、星期三下午 2 点、星期四下午 2 点等收到通知。

问题:下午 2 点收到第一条通知,但随后我在随机时间一遍又一遍地收到相同的通知。

问题似乎只出现在 Android >= 4.0 上。它在早期的 Android 上运行良好。

这是我发送通知的方式:

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);
}
// ...
}

sendNotification() 在下午 2 点被调用,因为 AlarmManager:

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

前段时间我问过如何创建这样的通知,所以这是相关的:
Everyday notifications at certain time

最佳答案

使用 AlarmManager.RTC_WAKEUP 代替 AlarmManager.RTC

在AlarmManager.RTC

System.currentTimeMillis() 中的闹钟时间(UTC 中的挂钟时间)。此警报不会唤醒设备;如果它在设备休眠时熄灭,则直到下次设备唤醒时才会发送。

在 AlarmManager.RTC_WAKEUP 中的位置

System.currentTimeMillis() 中的闹钟时间(UTC 中的挂钟时间),它将在设备关闭时唤醒它。

关于android - 在 Android 4 上重复通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12865337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com