我知道有很多问题需要重新考虑,但没有一个对我有用。
我遇到警报管理员的问题。我希望报警管理员在上午10点开始服务并停止在晚上8点如果用户从应用服务中退出停止,如果再次登录服务,如果时间在上午10点到晚上8点之间,应该使用报警管理器重新开始服务。
在4.3和5.0(棒棒糖)中工作正常,但我在4.4(kit Kat)面临问题
所以请告诉我如何在4.4中实现这一点。
这是我的代码
Calendar calendar2,calendar,cal,cal2;
calendar = new GregorianCalendar();
calendar2 = new GregorianCalendar();
calendar2.setTimeInMillis(System.currentTimeMillis());
calendar.setTimeInMillis(System.currentTimeMillis());
cal = new GregorianCalendar();
cal2 = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, 10);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, calendar.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, calendar.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, calendar.get(Calendar.DATE));
cal.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent it = new Intent(MainActivity.this,Start_service_alarm.class);
it.putExtra(Start_service_alarm.ACTION, Start_service_alarm.ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 987654321, it, 0);
if(android.os.Build.VERSION.SDK_INT<19)
{
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}else
{
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
}
cal2.add(Calendar.DAY_OF_YEAR, calendar2.get(Calendar.DAY_OF_YEAR));
cal2.set(Calendar.HOUR_OF_DAY, 20);
cal2.set(Calendar.MINUTE, 0);
cal2.set(Calendar.SECOND, calendar2.get(Calendar.SECOND));
cal2.set(Calendar.MILLISECOND, calendar2.get(Calendar.MILLISECOND));
cal2.set(Calendar.DATE, calendar2.get(Calendar.DATE));
cal2.set(Calendar.MONTH, calendar2.get(Calendar.MONTH));
AlarmManager alarmManager2 = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent itIntent = new Intent(MainActivity.this,Stop_service_alarm.class);
itIntent.putExtra("ACTION_STOP", Stop_service_alarm.ACTION_STOP);
PendingIntent pendingIntent2 = PendingIntent.getBroadcast(MainActivity.this, 123456789, itIntent, 0);
if(android.os.Build.VERSION.SDK_INT<19)
{
alarmManager2.setRepeating(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent2);
}else
{
alarmManager2.setExact(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), pendingIntent2);
}