Alarmmanager 不适用于大秒数
Alarmmanager not working with large seconds
我已经通过警报管理器发出了简单的警报,问题是当插入很多秒时它不能及时工作,我在这里声明
这是代码
mo = ((Integer.parseInt(mons.getText().toString())) * (30 * 24 * 60 * 60));
we = ((Integer.parseInt(weeks.getText().toString())) * (7 * 24 * 60 * 60));
da = ((Integer.parseInt(days.getText().toString())) * (24 * 60 * 60));
ho = ((Integer.parseInt(hours.getText().toString())) * (60 * 60));
mi = ((Integer.parseInt(mins.getText().toString())) * (60));
int all = mo+we+da+ho+mi;
Intent i = new Intent(Messages.this, Alarm.class);
PendingIntent pd = PendingIntent.getBroadcast(getApplicationContext(), 5484, i, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (all*1000), pd);
这是接收器
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"ALarm....",Toast.LENGTH_LONG).show();
showNotification( context);
}
private void showNotification(Context context) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context,"M_CH_ID")
.setSmallIcon(android.R.drawable.ic_dialog_alert)
.setContentTitle("فكرنى")
.setContentText("I think you need to do something")
.setAutoCancel(true);
mBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
NotificationManagerCompat mNotificationManager =
NotificationManagerCompat.from(context);
mNotificationManager.notify(1, mBuilder.build());
}
如果它得到 "wrong" 大约 828 秒(± 2 秒,我懒得做 MWE),那么在计算 mo
时你有一个简单的整数溢出,参见。 https://dzone.com/articles/overflow-and-underflow-data
使用 long
s 或以其他方式进行计算(基于某些日期对象?)。
编辑:我需要更多咖啡,当然你不会在 828 秒左右得到它,因为秒数不会传递给月份评估。不过,我仍然押注整数 ovewflow,只是不完全符合这个值和变量。
我已经通过警报管理器发出了简单的警报,问题是当插入很多秒时它不能及时工作,我在这里声明
这是代码
mo = ((Integer.parseInt(mons.getText().toString())) * (30 * 24 * 60 * 60));
we = ((Integer.parseInt(weeks.getText().toString())) * (7 * 24 * 60 * 60));
da = ((Integer.parseInt(days.getText().toString())) * (24 * 60 * 60));
ho = ((Integer.parseInt(hours.getText().toString())) * (60 * 60));
mi = ((Integer.parseInt(mins.getText().toString())) * (60));
int all = mo+we+da+ho+mi;
Intent i = new Intent(Messages.this, Alarm.class);
PendingIntent pd = PendingIntent.getBroadcast(getApplicationContext(), 5484, i, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (all*1000), pd);
这是接收器
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"ALarm....",Toast.LENGTH_LONG).show();
showNotification( context);
}
private void showNotification(Context context) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context,"M_CH_ID")
.setSmallIcon(android.R.drawable.ic_dialog_alert)
.setContentTitle("فكرنى")
.setContentText("I think you need to do something")
.setAutoCancel(true);
mBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
NotificationManagerCompat mNotificationManager =
NotificationManagerCompat.from(context);
mNotificationManager.notify(1, mBuilder.build());
}
如果它得到 "wrong" 大约 828 秒(± 2 秒,我懒得做 MWE),那么在计算 mo
时你有一个简单的整数溢出,参见。 https://dzone.com/articles/overflow-and-underflow-data
使用 long
s 或以其他方式进行计算(基于某些日期对象?)。
编辑:我需要更多咖啡,当然你不会在 828 秒左右得到它,因为秒数不会传递给月份评估。不过,我仍然押注整数 ovewflow,只是不完全符合这个值和变量。