0
            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 的价值是 278,088

tmemills 大约需要 4.5 分钟,但

警报管理器正在立即运行

我无法找到问题所在,因为我是 Android 的初学者。请帮我

4

2 回答 2

2

尝试这个:

am.setRepeating(AlarmManager.RTC_WAKEUP, tmemills + System.currentTimeMillis(), 
     AlarmManager.INTERVAL_DAY, sender);
于 2013-08-29T12:31:31.760 回答
1

摆脱tmemills. 用作调用 oncal_alarm.getTimeInMillis()的第二个参数,因为这是自您希望事件发生的 Unix 纪元以来的毫秒数。set()AlarmManager

于 2013-08-29T12:39:34.007 回答