目标:如果满足特定条件,通知会在每天下午 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/
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
有人知道在发布新版本的Ruby和Rails时收到电子邮件的方法吗?他们有邮件列表,RubyonRails有一个推特,但我不想听到那些随之而来的喧嚣,我只想知道什么时候发布新版本,尤其是那些有安全修复的版本。 最佳答案 从therailsblog获取提要.http://weblog.rubyonrails.org/feed/atom.xml 关于ruby-on-rails-如何在发布新的Ruby或Rails版本时收到通知?,我们在StackOverflow上找到一个类似的问题:
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路
这就是我做的a="%span.rockets#diamonds.ribbons.forever"a=a.match(/(^\%\w+)([\.|\#]\w+)+/)putsa.inspect这是我得到的#这就是我想要的#帮助?我尝试过但失败了:( 最佳答案 通常,您不能获得任意数量的捕获组,但如果您使用扫描,您可以为您想要捕获的每个标记获得一个匹配:a="%span.rockets#diamonds.ribbons.forever"a=a.scan(/^%\w+|\G[.|#]\w+/)putsa.inspect["%span","
我无法使用传统的Ruby方法从下面的数组user_list中删除所有重复对象,从而获得预期的结果。有解决这个问题的聪明方法吗?users=[]user_list.eachdo|u|user=User.find_by_id(u.user_id)users 最佳答案 这个怎么样?users=User.find(user_list.map(&:user_id).uniq)这具有作为一个数据库调用而不是user_list.size数据库调用的额外好处。 关于Ruby从数组中删除重复的对象,我们在
ruby中有没有一个很好的方法来删除可枚举列表中的重复项(即拒绝等) 最佳答案 对于数组你可以使用uniq()方法a=["a","a","b","b","c"]a.uniq#=>["a","b","c"]所以如果你只是(1..10).to_a.uniq或%w{antbatcatant}.to_a.uniq因为无论如何,几乎所有您实现的方法都将作为Array类返回。 关于Ruby删除可枚举列表中的重复项,我们在StackOverflow上找到一个类似的问题: h
我知道如何创建值数组的排列。例如:[*1..3].permutation(2)这导致以下六种排列:[1,2][1,3][2,1][2,3][3,1][3,2]但这个结果缺少三个排列,它们是相同值的组合,即:[1,1][2,2][3,3]如何获得所有排列,包括上面重复的排列? 最佳答案 尝试#repeated_permutation:[*1..3].repeated_permutation(3).to_a>pp[*1..3].repeated_permutation(3).to_a[[1,1,1],[1,1,2],[1,1,3],[1
像这样转换数组的最快/单行方法是什么:[1,1,1,1,2,2,3,5,5,5,8,13,21,21,21]...进入像这样的对象数组:[{1=>4},{2=>2},{3=>1},{5=>3},{8=>1},{13=>1},{21=>3}] 最佳答案 要获得所需的格式,您可以附加一个调用以映射到您的解决方案:array.inject({}){|h,v|h[v]||=0;h[v]+=1;h}.map{|k,v|{k=>v}}虽然它仍然是单行的,但它开始变得凌乱了。 关于ruby-在Ruby
我想输出一个散列数组,其中name对所有散列都是唯一的。我将如何使用ruby执行此操作?这是我的输入:input=[{:name=>"Kutty",:score=>2,:some_key=>'value',...},{:name=>"Kutty",:score=>4,:some_key=>'value',...},{:name=>"Baba",:score=>5,:some_key=>'value',...}]我希望输出看起来像这样:output=[{:name=>"Kutty",:score=>4,:some_key=>'value',...},{:name=>"Baba",:s
我正在使用Rails4应用程序,它需要创建大量对象以响应来自另一个系统的事件。当我调用create!时,主键列上出现非常频繁的ActiveRecord::RecordNotUnique错误(由PG::UniqueViolation引起)我的模型之一。我在SO上找到了其他答案,建议挽救异常并调用retry:beginTableName.create!(data:'here')rescueActiveRecord::RecordNotUnique=>eife.message.include?'_pkey'#Onlyretryprimarykeyviolationslog.warn"Retr