首页 > 解决方案 > Android 缓慢的冷启动时间

问题描述

我有一个非常奇怪的问题,需要帮助解决。它与较长的应用程序冷启动时间有关。这意味着,我的应用程序需要很长时间才能启动并首次显示。鉴于此处给出的材料,我已经研究了故障排除:

https://developer.android.com/topic/performance/vitals/launch-time

都非常有帮助,但我尝试了一些不同的东西。当我们的系统处于重负载状态时,就是我看到启动时间问题的时候。我尝试将整个活动剥离为仅启动,而不是像以往那样绘制/夸大视图。从字面上看,只是启动基本活动。我看到平均冷启动时间为 1.45 秒。您可能认为这没问题,但我们还有其他应用程序具有完整的实现和视图/小部件、数据等的实际膨胀。启动只需要大约 1.17 秒。这让我很困惑,因为我实际上只有一个活动(相同的 android 主题)。我还应该寻找哪些其他东西?为什么启动需要这么长时间?

请注意,我与之比较的活动是在我使用 Maven 时使用 Gradle 构建的。我们的 Android 清单在属性使用方面是相同的。

我还尝试在启动活动之前已经启动该过程(在启动时创建了一个虚拟服务),但启动时间并没有真正的改善。我在 Android 4.3.1 上,通过查看 logcat 中的 ActivityManager 条目来确认启动时间。也许是资源加载问题?它必须与构建有关,因为没有理由说明为什么一个简单的活动需要更长的时间才能启动,而不是一个更复杂的活动。

编辑:添加代码

这是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="11" android:versionName="debug-11" package="com.my.app">

    <uses-permission android:name="com.cnh.android.permission.ACCESS_SETTINGS"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.VIBRATE"/>

    <!-- uses-sdk is updated automatically by pom.xml -->
    <uses-sdk android:minSdkVersion="18" android:targetSdkVersion="18"/>


    <application android:allowBackup="true" android:debuggable="false" android:icon="@drawable/ic_tab" android:label="@string/app_name" android:name=".App" android:theme="@style/AppTheme">
        <activity android:label="@string/app_name" android:launchMode="singleInstance" android:name=".MyActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

申请代码:

package com.my.app;

import android.app.Application;
import android.content.Context;

/**
 * base App class used for application
 */
public class App extends Application {

   private static Context context;

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

   private static void setContext(Context ctx) {
      context = ctx;
   }

   public static Context getContext() {
      return context;
   }
}

活动代码:

package com.my.app;

import android.app.Activity;
import android.os.Bundle;

public class MyActivity extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
   }

   @Override
   protected void onStart() {
      super.onStart();
   }

   @Override
   protected void onResume() {
      super.onResume();
   }

   @Override
   protected void onPause() {
      super.onPause();
   }

   @Override
   protected void onStop() {
      super.onStop();
   }

   @Override
   protected void onDestroy() {
      super.onDestroy();
   }
}

就像我说的,超级基本/骨架。

标签: androidandroid-activitytimeandroid-inflatecold-start

解决方案


推荐阅读