我正在处理现有项目。我已将目标sdk版本升级到最新版本,因为较早的目标sdk是19,并在未由我开发的现有项目中做了一些UI更改。我的应用程序具有必须在用户选择的时间通过通知来通知的列表。我已将alaram管理器与广播接收器一起使用来触发通知。该代码可在仿真器中正常运行,但在实际设备上的行为却有所不同。
Redmi-版本6-工作正常
三星-版本9-有时将通知延迟3分钟,有时在晚上3或4或5,延迟3分钟,选择在每小时的第10分钟显示通知,但将在上午3.30、4.35时显示,5.45am。此行为是随机的,不是特定的。有时会在第10分钟完美触发。
Moto G 8.1-有时通知两次,有时没有触发通知。
Moto G版本7-完美运行,有时会同时发出两次通知。
在模拟器中可以正常工作。不幸的是,我只有很少的设备可以测试。
此代码是否可以触发?iI如何检查测试设备中安装了APK的应用的日志?我使用测试团队的测试设备在不同的地方工作。是否可以将用户的日志保存在已安装apk的设备中的用户SD卡中?如何配置设备中的日志?
这是保存数据的代码:
Intent intentsOpenOnce = new Intent(context, AlarmReceiver.class);
intentsOpenOnce.setAction("xx.xxx.xxx.ACTION");
Utils.pendingIntentOnce = PendingIntent.getBroadcast(context,111, intentsOpenOnce, PendingIntent.FLAG_CANCEL_CURRENT);
Utils.alarmManagerOnce = (AlarmManager) context.getSystemService(ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= 19){
Utils.alarmManagerOnce.setExact(AlarmManager.RTC_WAKEUP, calendaronce.getTimeInMillis(), Utils.pendingIntentOnce);
}else{
Utils.alarmManagerOnce.setRepeating(AlarmManager.RTC_WAKEUP, calendaronce.getTimeInMillis(), AlarmManager.INTERVAL_HOUR, Utils.pendingIntentOnce);
}
这是我在广播接收器中使用的代码:
if (SOMEACTION.equals(action)) {
if(isOnceAlarmExpired(Utils.pendingIntentOnce, Utils.alarmManagerOnce)) {
return;
}
DateFormat df = new SimpleDateFormat("HH:mm", Locale.getDefault());
String date = df.format(Calendar.getInstance().getTime());
int currentHours;
int currentMinute;
String[] oneSeparated = date.split(":");
currentHours = Integer.valueOf(oneSeparated[0]);
currentMinute = Integer.valueOf(oneSeparated[1]);
Utils.globalOnceTimerOneEndHour = Utils.sharedpreferences.getInt(Utils.endAlarmHour, 0);
Utils.globalOnceTimerOneEndMinute = Utils.sharedpreferences.getInt(Utils.endAlarmMinute, 0);
String endDateStr = Utils.sharedpreferences.getString(Utils.endAlarmDate, null);
if((endDateStr != null) && (!endDateStr.trim().equalsIgnoreCase(""))) {
Calendar endDateCal = Calendar.getInstance();
try {
endDateCal.setTime(Utils.SDF_YYYY1MM1DD1HH1MM1SS1SSS.parse(endDateStr));
} catch (java.text.ParseException e) {
Log.e(TAG, "[line#056]:onReceive():endDateCal.setTime()",e);
}
if(Calendar.getInstance().after(endDateCal)) {
boolean needToCancel = false;
if(Utils.globalOnceTimerOneEndHour <= currentHours){
if(Utils.globalOnceTimerOneEndMinute <= currentMinute){
needToCancel = true;
}
}
if(needToCancel){
if(Utils.alarmManagerOnce != null) {
Utils.alarmManagerOnce.cancel(Utils.pendingIntentOnce);
if(Utils.pendingIntentOnce != null){
Utils.pendingIntentOnce.cancel();
}
}
return;
}
}
}
if (Build.VERSION.SDK_INT >= 19) {
Calendar hourlyCalendar = Calendar.getInstance();
hourlyCalendar.add(Calendar.HOUR_OF_DAY, 1);
hourlyCalendar.set(Calendar.MINUTE, Utils.sharedpreferences.getInt(Utils.globalOnceTimerMainMinute, 0));
hourlyCalendar.set(Calendar.SECOND, 0);
hourlyCalendar.set(Calendar.MILLISECOND, 0);
Intent intentsOpenOnce = new Intent(context, AlarmReceiver.class);
intentsOpenOnce.setAction("xx.xxx.xxx.ACTION");
Utils.pendingIntentOnce = PendingIntent.getBroadcast(context, 111, intentsOpenOnce, PendingIntent.FLAG_CANCEL_CURRENT);
Utils.alarmManagerOnce = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Utils.alarmManagerOnce.setExact(AlarmManager.RTC_WAKEUP, hourlyCalendar.getTimeInMillis(), Utils.pendingIntentOnce);
}
fireNotification(context);