Android 通知使手机延迟

发布于 2025-01-16 00:09:50 字数 3315 浏览 2 评论 0原文

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

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

有什么想法吗?

I'm developing an app where users can create routines and set reminders for these routines.

I use alarms scheduled by AlarmManager and a BroadcastReceiver to call a foregroud service that sends a notification by using the notification manager. Here's some code:

    // 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);

On some devices while this notification exists in the notification drawer the phone is acting very laggy. It takes a very long time to activate the phone screen, to view the notifaction drawer, to unlock the phone. On some phones it works perfectly fine.

The phone that lags has Android 30 on it. If the notification is dismissed the phone starts working normally again.

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光沙漏 2025-01-23 00:09:50

您应该检查延迟设备的规格,并在应用运行之前、之后和期间监控设备的RAMCPU使用情况特别是 RAM 使用情况,也许 Android 30 给设备带来了太大的压力,从而导致了问题。

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

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

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

You should check the specifications of the device that is laggy and monitor RAM and CPU usage of the device, before, after and during your app runtime, speatially the RAM usage, maybe Android 30 is putting too much pressure on device and that is causing the issue.

In General the issue that are seen on some devices and not on another are caused ether by the difference between Android Versions and their resource handling methods or bottleneck between Hardware and Software on same OS version on different devices.

You can monitor device resource usage ether by Android Studio's own resource monitor tool "Android Profiler" or third party Resource Monitoring Apps found on Google Play.

If there is a process or function in your App that causing the resource leak you can fix it easily by detecting it from android profiler but if the issue is caused by OS Resource handling or Hardware and Software bottleneck you should skip that device.

活雷疯 2025-01-23 00:09:50

您的应用程序通知代码中可能存在数据泄漏问题。您可以使用 android profiler 。您还可以对通知的 UI 代码进行一些重构。有时,当通知栏的组件太多而无法加载时,它的行为通常会有所不同。尝试检查设备屏幕尺寸和 UI 调整器

There can be a issue with the data leak within your app notification code .You can use android profiler . And you can also do some refactoring around your UI code for the notification . Some time when the notification bar is too many components to load it usually behave differently.Try to check with device screen size and UI re-sizer

怪异←思 2025-01-23 00:09:50

我也有同样的问题。我能够通过降低 android:width & 来修复它。 android:height 用于我的通知的小图标的矢量资源。

我的矢量资源的原始大小是 800dp x 800dp。 Android Studio 建议我将其更改为 200dp x 200dp,所以我尝试了,锁屏延迟消失了。

进行此尺寸更改后,我的通知的小图标没有明显的差异。

I had the same problem. I was able to fix it by lowering the android:width & android:height for my notification's small-icon's vector asset.

The original size for my vector asset was 800dp x 800dp. Android Studio recommended me to change it to 200dp x 200dp so I tried that, and the lock screen lag disappeared.

There was no visible difference in my notification's small-icon after making this size change.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文