Alarmmanager无法长时间运行

时间:2018-09-07 09:38:27

标签: java android alarmmanager

我已通过警报管理器进行了简单的警报,问题是当插入许多秒时它在我声明的时间上不起作用

这是代码

 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());
}

1 个答案:

答案 0 :(得分:1)

如果在828秒(±2秒,我懒于做一个MWE)上出现“错误”,那么在计算mo时,您将得到一个简单的整数溢出,参见https://dzone.com/articles/overflow-and-underflow-data

使用long,否则进行计算(基于某些日期对象?)。

编辑:我需要更多的咖啡,当然您不会在828秒左右拿到咖啡,因为秒数没有传递到月份评估中。我仍然押注于整数卵流,只是不完全与此值和变量有关。