Android通知使手机滞后

标签 android notifications broadcastreceiver alarm foreground-service

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

我使用由 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。如果通知被取消,手机将再次开始正常工作。

有什么想法吗?

最佳答案

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

一般来说,在某些设备上而不是在其他设备上看到的问题是由 Android 版本 及其资源处理方法之间的差异或硬件软件在不同设备上的相同操作系统版本。

可以通过Android Studio自带的资源监控工具监控设备资源使用情况 "Android Profiler" 或在 Google Play 上找到的第三方资源监控应用

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

关于Android通知使手机滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71562326/

相关文章:

java - 在android中迭代它时从堆栈中删除一个项目

ios - 应用程序终止时无法处理本地通知

android - 广播接收器双卡

android - BroadcastReceiver.onReceive 不为 PHONE_STATE 调用

android - 广播接收器未收到 ACTION_BOOT_COMPLETED

android - 无法读取 android 调试 key

android - dexguard 是否可以防止简单的 LVL 裂缝?

python - Django修改pip安装的应用程序的模板

Android Canvas 硬件加速

Azure 角色失败通知