我需要让警报管理器每天重复两次,为此我在两个不同的时间使用了2个日历对象,但是它第一次只能工作一次,而不能第二次
MainActivity
public void startEvent(View view) {
Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.HOUR_OF_DAY, 14);
cal1.set(Calendar.MINUTE, 00);
cal1.set(Calendar.SECOND, 00);
Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.HOUR_OF_DAY, 14);
cal2.set(Calendar.MINUTE, 30);
cal2.set(Calendar.SECOND, 00);
Intent intent = new Intent(getApplicationContext(), YourBroadCastReceiver.class);
PendingIntent pIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(),cal2.getTimeInMillis(), pIntent);
Toast.makeText(this, "Alarm is set now", Toast.LENGTH_SHORT).show();
}
BroadCastReciver
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Send Location to server", Toast.LENGTH_LONG).show();
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("test");
myRef.push().setValue("New Location Received");
}
答案 0 :(得分:0)
Intent intent;
PendingIntent pIntent;
AlarmManager alarmManager;
Date date = new Date();
Calendar cal_now = Calendar.getInstance();
cal_now.setTimeInMillis(System.currentTimeMillis());
cal_now.setTime(date);
private void alarmOne(){
Calendar cal1 = Calendar.getInstance();
cal1.setTimeInMillis(System.currentTimeMillis());
cal_alarm.setTime(date);
cal1.set(Calendar.HOUR_OF_DAY, 14);
cal1.set(Calendar.MINUTE, 00);
cal1.set(Calendar.SECOND, 00);
cal1.set(Calendar.AM_PM, 1);
if(cal1.before(cal_now)){
// Move to tomorrow for first alarm
cal1.add(Calendar.DATE, 1);
}
intent = new Intent(getApplicationContext(), YourBroadCastReceiver.class);
pIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
cal1.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pIntent);
}
private void alarmTwo(){
Calendar cal2 = Calendar.getInstance();
cal2.setTimeInMillis(System.currentTimeMillis());
cal2.setTime(date);
cal2.set(Calendar.HOUR_OF_DAY, 14);
cal2.set(Calendar.MINUTE, 30);
cal2.set(Calendar.SECOND, 00);
cal2.set(Calendar.AM_PM, 1);
if(cal2.before(cal_now)){
// Move to tomorrow for second alarm
cal2.add(Calendar.DATE, 1);
}
pIntent = PendingIntent.getBroadcast(getApplicationContext(), 1, intent, 0);
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
cal2.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pIntent);
}
答案 1 :(得分:0)
尝试发送小时和分钟
public static void setAlarmforOverlay(Context context, int hour, int minute, int AlarmId) {
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, YourBroadCastReceiver.class);
intent.putExtra("hour", hour);
intent.putExtra("minute", minute);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, AlarmId, intent, 0);
//1 PM and 0 AM
int AM_PM;
if (hour < 12)
AM_PM = 0;
else
AM_PM = 1;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 1);
calendar.set(Calendar.AM_PM, AM_PM);
if (Build.VERSION.SDK_INT >= 23) {
manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingIntent);
} else if (Build.VERSION.SDK_INT >= 21) {
manager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
} else {
manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
}
用法
setAlarmforOverlay(mContext,14,00,1);
setAlarmforOverlay(mContext,14,30,2);
还要验证清单
<receiver
android:name=".YourBroadCastReceiver"
android:process=":remote">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>