java - 警报管理器立即运行

标签 java android

            Date dat = new Date();
            Calendar cal_alarm = Calendar.getInstance();
            Calendar cal_now = Calendar.getInstance();
            cal_alarm.setTime(dat);
            cal_alarm.set(Calendar.HOUR_OF_DAY, hrs);// set the alarm time
            cal_alarm.set(Calendar.MINUTE, min);
            cal_alarm.set(Calendar.SECOND, 0);
            cal_alarm.set(Calendar.MILLISECOND, 0);
            if (cal_alarm.before(cal_now)) {// if its in the past increment
                cal_alarm.add(Calendar.DATE, 1);
            }

            Intent intent = new Intent(ctx, AlarmReceiver.class);
            // intent.putExtra("Reminder to Take Photo", "Pixitch!");
            PendingIntent sender = PendingIntent.getBroadcast(ctx, 0010000,
                    intent, 0);
            // Get the AlarmManager service
            long tmemills = cal_alarm.getTimeInMillis()
                    - cal_now.getTimeInMillis();
            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            am.setRepeating(AlarmManager.RTC_WAKEUP, tmemills,
                    AlarmManager.INTERVAL_DAY, sender);

报警接收器类

public class AlarmReceiver extends BroadcastReceiver {

private static final int MY_NOTIFICATION_ID = 1;
private NotificationManager notificationManager;
private Notification myNotification;

// Context ctx = this;

@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
    try {
        NotificationManager mNM;
        mNM = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(
                R.drawable.ic_launcher, "Pixitch Notification !",
                System.currentTimeMillis());
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                new Intent(context, AlarmManage.class), 0);
        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(context, "Pixitch Notification!",
                "Reminder For TakePhoto", contentIntent);
        mNM.notify(0, notification);
    } catch (Exception e) {
        Toast.makeText(
                context,
                "There was an error somewhere, but we still received an alarm",
                Toast.LENGTH_SHORT).show();
        e.printStackTrace();

    }
}

}

tmemills 的值(value)是 278,088

tmemills 大约需要 4.5 分钟,但是

警报管理器正在立即运行

我无法找到问题出在哪里,因为我是 Android 初学者。请帮助我

最佳答案

试试这个:

am.setRepeating(AlarmManager.RTC_WAKEUP, tmemills + System.currentTimeMillis(), 
     AlarmManager.INTERVAL_DAY, sender);

关于java - 警报管理器立即运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18510983/

相关文章:

Java DataInputStream 长度

android - 我无法在 phonegap 构建中加载我的 Polymer 项目

java - 为什么在我删除了 Log.foo(outputs) 后 LogCat 仍在报告?

android - 了解 Android list 中的提供程序属性 "android:multiprocess"

android - 用作图像按钮的切换按钮

java - 带有 $in 运算符的 mongodb find() 是顺序的还是并行的?

Java - 如何使用循环点播放 mp3

java - SWT - 获取图像的实际屏幕位置

java - 自定义身份验证过滤器 Spring Security 3.2

android - 使用 retrofit2 和 rxjava2 对 android 应用程序进行单元测试