Android 通知使 phone 滞后

Android notification makes phone lag

我正在开发一个应用程序,用户可以在其中创建例程并为这些例程设置提醒。

我使用由 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);

在某些设备上,虽然此通知存在于通知抽屉中,但 phone 表现得很迟钝。激活 phone 屏幕、查看通知抽屉、解锁 phone 需要很长时间。在某些 phone 上它工作得很好。

滞后的phone上面有Android30。如果关闭通知,phone 将再次开始正常工作。

有什么想法吗?

您应该检查延迟设备的 规格 并监控 RAMCPU 在您的应用程序运行之前、之后和期间使用设备,特别是 RAM 使用情况,也许 Android 30 对设备施加了太大压力,这就是导致问题的原因。

一般来说,在某些设备上出现而不在其他设备上出现的问题是由 Android 版本 之间的差异及其资源处理方法或瓶颈引起的硬件软件在不同设备上的相同OS版本。

您可以通过AndroidStudio自带的资源监控工具"Android Profiler"或第三方Resource Monitoring Apps[监控设备资源使用情况ether] =27=] 在 Google 上找到

如果您的应用程序中存在导致资源泄漏的进程或功能,您可以通过 android 探查器检测它来轻松修复它,但如果问题是由 OS 资源处理或您应该跳过该设备的硬件和软件瓶颈。

您的应用通知代码中可能存在数据泄漏问题。您可以使用 android 探查器。您还可以围绕通知的 UI 代码进行一些重构。有时当通知栏有太多组件无法加载时,它通常会 differently.Try 检查设备屏幕尺寸和 UI re-sizer