作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个应用程序,用户可以在其中创建例程并为这些例程设置提醒。
我使用由 AlarmManager 和 BroadcastReceiver 安排的警报来调用使用通知管理器发送通知的前台服务。这是一些代码:
// Mark as completed
Intent markAsCompletedIntent = new Intent(this, RoutincoBroadcastReceiver.class);
markAsCompletedIntent.putExtra(RoutincoBroadcastReceiver.ROUTINE_ID, routineModelId);
markAsCompletedIntent.putExtra(RoutincoBroadcastReceiver.ROUTINE_REMINDER_ID, routineReminderModelId);
markAsCompletedIntent.setAction(RoutincoBroadcastReceiver.ACTION_BROADCAST_MARK_AS_COMPLETED);
PendingIntent markAsCompletedPendingIntent = PendingIntent.getBroadcast(this, routineReminderModelId, markAsCompletedIntent, PendingIntent.FLAG_CANCEL_CURRENT);
// Dismiss the notification
Intent closeIntent = new Intent(this, RoutincoBroadcastReceiver.class);
closeIntent.putExtra(RoutincoBroadcastReceiver.ROUTINE_ID, routineModelId);
closeIntent.putExtra(RoutincoBroadcastReceiver.ROUTINE_REMINDER_ID, routineReminderModelId);
closeIntent.setAction(RoutincoBroadcastReceiver.ACTION_BROADCAST_CLOSE);
PendingIntent closePendingIntent = PendingIntent.getBroadcast(this, routineReminderModelId, closeIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder;
if (!routineModel.Description.equals("")) {
builder = new NotificationCompat.Builder(this, ChannelID)
.setSmallIcon(R.drawable.ic_appiconnotifications)
.setContentTitle(routineModel.Caption)
.setContentText(routineModel.Description)
.addAction(0, "MARK AS COMPLETED", markAsCompletedPendingIntent)
.addAction(0, "DISMISS", closePendingIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_REMINDER)
.setAutoCancel(true);
} else {
builder = new NotificationCompat.Builder(this, ChannelID)
.setSmallIcon(R.drawable.ic_appiconnotifications)
.setContentTitle(routineModel.Caption)
.addAction(0, "MARK AS COMPLETED", markAsCompletedPendingIntent)
.addAction(0, "DISMISS", closePendingIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_REMINDER)
.setAutoCancel(true);
}
Intent openIntent = new Intent(this, RoutineReminderActivity.class);
openIntent.putExtra(RoutineReminderActivity.RRA_ROUTINE_ID, routineModelId);
openIntent.putExtra(RoutineReminderActivity.RRA_ROUTINE_REMINDER_ID, routineReminderModelId);
openIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
PendingIntent openActivityIntent = PendingIntent.getActivity(this, routineReminderModelId, openIntent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(openActivityIntent);
Notification notification = builder.build();
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(routineReminderModelId, notification);
在某些设备上,虽然此通知存在于通知抽屉中,但手机的运行速度非常慢。激活手机屏幕、查看通知抽屉、解锁手机都需要很长时间。在某些手机上它工作得很好。
滞后的手机上装有 Android 30。如果通知被取消,手机将再次开始正常工作。
有什么想法吗?
最佳答案
您应该检查延迟设备的规范,并在之前、之后和期间监控设备的RAM 和CPU 使用情况您的应用程序运行时,特别是 RAM 使用情况,也许 Android 30 对设备施加了太大压力,这就是导致问题的原因。
一般来说,在某些设备上而不是在其他设备上看到的问题是由 Android 版本 及其资源处理方法之间的差异或硬件 和软件在不同设备上的相同操作系统版本。
可以通过Android Studio自带的资源监控工具监控设备资源使用情况 "Android Profiler" 或在 Google Play 上找到的第三方资源监控应用。
如果您的应用程序中存在导致资源泄漏的进程或功能,您可以通过 android profiler 检测它轻松修复它,但如果问题是由操作系统资源处理或硬件和软件瓶颈引起的,您应该跳过该设备。
关于Android通知使手机滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71562326/
我是一名优秀的程序员,十分优秀!