意图服务时间间隔未维护
Intent Service Time Interval Not maintained
我已经创建了一个意图服务,它应该以用户定义的时间间隔将数据发送到服务器,但在我的例子中,警报管理器没有正确维护时间间隔。你能指导我在哪里做错了吗?我也想在设备处于睡眠或空闲模式时 运行 服务。这是我的代码。
public void sosalarmson() {
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(this, AlarmReceiver.receivercode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), Integer.parseInt(userdefinedinterval) *60* 1000, pendingIntent);
Toast.makeText(FinalSettings.this, "alarm On", Toast.LENGTH_SHORT).show();
}
理想情况下,如果我给用户输入 15,那么警报应该在 15 分钟发送意图,而它在 10 分钟发送。
我正在使用这段代码,它运行良好:
定义:
final int SDK_INT = Build.VERSION.SDK_INT;
AlarmManager al;
PendingIntent fintent;
Intent notif;
我直接用闹钟启动服务manager._alarm是你预定义的时间间隔
long _alarm;
Calendar now = Calendar.getInstance();
Calendar wakeupcall = Calendar.getInstance();
wakeupcall.setTimeInMillis(System.currentTimeMillis());
wakeupcall.set(Calendar.HOUR_OF_DAY, 18);
wakeupcall.set(Calendar.MINUTE, 30);
if (wakeupcall.getTimeInMillis() <= now.getTimeInMillis())
_alarm=wakeupcall.getTimeInMillis() + (AlarmManager.INTERVAL_DAY+1);
else
_alarm=wakeupcall.getTimeInMillis();
al = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
notif= new Intent(this,TestNotifyService.class);
fintent = PendingIntent.getService(this,0,notif,0);
if (SDK_INT < Build.VERSION_CODES.KITKAT) {
al.set(AlarmManager.RTC_WAKEUP,_alarm, fintent);
}
else if (Build.VERSION_CODES.KITKAT <= SDK_INT && SDK_INT < Build.VERSION_CODES.M) {
al.setExact(AlarmManager.RTC_WAKEUP,_alarm,fintent);
}
else if (SDK_INT >= Build.VERSION_CODES.M) {
al.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,_alarm,fintent);
}
我已经创建了一个意图服务,它应该以用户定义的时间间隔将数据发送到服务器,但在我的例子中,警报管理器没有正确维护时间间隔。你能指导我在哪里做错了吗?我也想在设备处于睡眠或空闲模式时 运行 服务。这是我的代码。
public void sosalarmson() {
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(this, AlarmReceiver.receivercode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), Integer.parseInt(userdefinedinterval) *60* 1000, pendingIntent);
Toast.makeText(FinalSettings.this, "alarm On", Toast.LENGTH_SHORT).show();
}
理想情况下,如果我给用户输入 15,那么警报应该在 15 分钟发送意图,而它在 10 分钟发送。
我正在使用这段代码,它运行良好:
定义:
final int SDK_INT = Build.VERSION.SDK_INT;
AlarmManager al;
PendingIntent fintent;
Intent notif;
我直接用闹钟启动服务manager._alarm是你预定义的时间间隔
long _alarm;
Calendar now = Calendar.getInstance();
Calendar wakeupcall = Calendar.getInstance();
wakeupcall.setTimeInMillis(System.currentTimeMillis());
wakeupcall.set(Calendar.HOUR_OF_DAY, 18);
wakeupcall.set(Calendar.MINUTE, 30);
if (wakeupcall.getTimeInMillis() <= now.getTimeInMillis())
_alarm=wakeupcall.getTimeInMillis() + (AlarmManager.INTERVAL_DAY+1);
else
_alarm=wakeupcall.getTimeInMillis();
al = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
notif= new Intent(this,TestNotifyService.class);
fintent = PendingIntent.getService(this,0,notif,0);
if (SDK_INT < Build.VERSION_CODES.KITKAT) {
al.set(AlarmManager.RTC_WAKEUP,_alarm, fintent);
}
else if (Build.VERSION_CODES.KITKAT <= SDK_INT && SDK_INT < Build.VERSION_CODES.M) {
al.setExact(AlarmManager.RTC_WAKEUP,_alarm,fintent);
}
else if (SDK_INT >= Build.VERSION_CODES.M) {
al.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,_alarm,fintent);
}