Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 显示通知时出现极端滞后 原始问题:_Java_Android_Performance_Android Service_Android Notifications - Fatal编程技术网

Java 显示通知时出现极端滞后 原始问题:

Java 显示通知时出现极端滞后 原始问题:,java,android,performance,android-service,android-notifications,Java,Android,Performance,Android Service,Android Notifications,这是我第一次尝试实现一个服务和一个通知,所以如果我的实现概念是完全错误的,请容忍我 问题: 我实现了一个在给定时间间隔内重复显示通知的服务无论何时显示通知,通知都会正确显示,但我的手机开始严重滞后。滞后的不仅仅是我的应用程序,而是整个手机,运行Android 6.0.1的Nexus 5X。它几乎无法使用 实施: main启动服务的活动: public class MainActivity extends AppCompatActivity { @Override protected void

这是我第一次尝试实现一个服务和一个通知,所以如果我的实现概念是完全错误的,请容忍我


问题: 我实现了一个在给定时间间隔内重复显示通知的服务无论何时显示通知,通知都会正确显示,但我的手机开始严重滞后。滞后的不仅仅是我的应用程序,而是整个手机,运行Android 6.0.1的Nexus 5X。它几乎无法使用


实施: main启动服务的活动

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    Intent intent = new Intent(this, MyService.class);
    startService(intent);
}
显示通知的MyService

public class MyService extends Service {
private static long UPDATE_INTERVAL = 1 * 10 * 1000;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

@Override
public void onCreate() {
    super.onCreate();
    startService();
}

private void startService() {
    mTimer.scheduleAtFixedRate(

            new TimerTask() {

                public void run() {
                    refreshData();
                }
            }, 1000, UPDATE_INTERVAL);
}

private void refreshData() {

    Intent intent = new Intent(this, MainActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);

    Notification noti = new Notification.Builder(this)
            .setContentTitle("New notification")
            .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.").setSmallIcon(R.drawable.home)
            .setContentIntent(pIntent).build();
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    noti.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(1337, noti);
}

private void stopService() {
    if (mTimer != null) mTimer.cancel();
}

@Override
public void onDestroy() {
    super.onDestroy();
    stopService();
}
我真的无法确定是哪个部分导致了延迟,因此非常感谢您的帮助

先谢谢你


更新:
这个错误是由我的一个愚蠢的错误引起的,因为为通知加载的图像文件太大了(请参阅接受的答案)。我仍然会在线回答这个问题,因为它几乎演示了一个关于如何实现显示通知的服务的完整示例。(只要图像文件不是太大:-)

好的,因此在问题注释中,我们发现用于通知的图像是2000x2000像素。将其更改为尺寸更合理的图像解决了此问题


我的假设是CPU使用量来自SystemUI(显示通知),每次更新通知时都会重新加载该映像。解码一个2000x2000像素的png图像实际上需要几百毫秒。

它是在开始显示通知时立即发生的,还是随着时间的推移变得越来越糟糕?如果更新频率降低(比如说,每10秒更新一次),它会变得更好吗?它一出现在任务栏上就开始滞后。间隔似乎不相关,因为当我从任务栏中删除通知时,延迟会停止,并在显示下一个通知时再次开始。请尝试更长的间隔(或者根本不更新它)。它将为我们提供一个线索,即时间是连续花费的(例如:绘图)还是用于某种初始化(例如:图像加载)。说到图像加载,你的R.drawable.home位图是什么样子的?它有多大?哦,好吧,。。。我刚刚发现home.png文件是一个2000x2000文件。将其更改为适当的较小文件,它就像一个符咒。谢谢你的帮助!是的,那就够了。:)