Android AlarmManager AM和PM差异

时间:2015-09-22 13:33:17

标签: android timezone alarmmanager

我正在创建闹钟应用。我正在使用以下代码。它运行良好但它在错误的时间启动警报警报。它不区分上午和下午之间的警报。如果我在设备上午10点和当前晚上10点设置闹钟,那么我的警报活动也会开始。我怎么能解决这个问题,谢谢。

Calendar timeCalendar = Calendar.getInstance();
timeCalendar.setTimeInMillis(System.currentTimeMillis());


@Override
public void onTimeSet(RadialPickerLayout radialPickerLayout, int i, int i2) {
    timeCalendar.set(Calendar.MINUTE, i2);
    timeCalendar.set(Calendar.HOUR_OF_DAY, i);

    if (i == 0) {
        timeCalendar.set(Calendar.AM_PM,0);
    } else if (i < 12) {
        timeCalendar.set(Calendar.AM_PM,0);
    } else if (i == 12) {
        timeCalendar.set(Calendar.AM_PM,1);
    } else {
        timeCalendar.set(Calendar.AM_PM,1);
    }
    updateLabel();
}

功能:

   private void setAlarm() {
    Intent myIntent = new Intent(this, AlarmReceiver.class);
    Bundle b = new Bundle();
    b.putString("type", "Alarm");
    DBHelper db = new DBHelper(this);
    int sonid = db.sonId("alarm");
    b.putInt("notify_id", sonid);
    db.close();
    myIntent.putExtras(b);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, sonid, myIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, timeCalendar.getTimeInMillis(), pendingIntent);
}

1 个答案:

答案 0 :(得分:0)

享受完整代码

在按钮中点击 openTimePickerDialog()或在任何想要设置闹钟的位置;

$.ajax({
    headers: {
        'X-CSRF-TOKEN' : '{{ csrf_token() }}'
    },
    // ...
});

这是计时器监听器;

OnTimeSetListener onTimeSetListener = new OnTimeSetListener(){

private void openTimePickerDialog(boolean is24r) {
        Calendar calendar = Calendar.getInstance();

        timePickerDialog = new TimePickerDialog(MainActivity.this,
                onTimeSetListener, calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE), is24r);
        timePickerDialog.setTitle("Set Alarm Time");

        timePickerDialog.show();

    }

};

setAlarm();

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

    Calendar calNow = Calendar.getInstance();
    Calendar calSet = (Calendar) calNow.clone();

    calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
    calSet.set(Calendar.MINUTE, minute);
    calSet.set(Calendar.SECOND, 0);
    calSet.set(Calendar.MILLISECOND, 0);

    if (calSet.compareTo(calNow) <= 0) {
        // Today Set time passed, count to tomorrow
        calSet.add(Calendar.DATE, 1);
    }

    setAlarm(calSet);
}

<强> Reciver.java

 private void setAlarm(Calendar targetCal) {

        textAlarmPrompt.setText("\n\n***\n" + "Alarm is set "
                + targetCal.getTime() + "\n" + "***\n");

        Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                getBaseContext(), RQS_1, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),
                pendingIntent);

    }  

在清单中:

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context k1, Intent k2) {
        // TODO Auto-generated method stub
         Toast.makeText(k1, "Alarm received!", Toast.LENGTH_LONG).show();

    }

}