性能优化一-app启动速度及splash设计
App启动方式:
1冷启动:启动应用时后台没有该应用的进程,系统会重新创建一个该应用的进程分配与该应用,这种启动方式叫做冷启动
特点:因为系统会重新创建一个进程分配给应用,所以会创建并初始化application类,再创建和初始化mainactivity类(测量,布局,绘制等),最后再显示
2:热启动:按home键,后退键等方式退出应用后,其实后台有该应用的进程,这种启动方式叫做热启动。
特点:因为新进程的创建到进程的销毁,application只会初始化一次。热启动因为后台已经有该应用的进程所以不会再初始化application类,只会初始化mainactivity类。所以这种启动实际上是从已有的列表里恢复应用。
App启动流程:
启动虚拟机—>>ams-->>applicationprocess—>>application类构造器-->>attachBaseContext—>>oncreate—>>activity构造器-->>activity主题背景等属性—>>oncreate—>> onstart—>>onresume -->>测量布局绘制,显示在界面
测试启动用时
执行成功后将返回三个测量到的时间:
(1)ThisTime:当前指定的activity启动用的时间
(2)TotalTime: 整个应用启动用的时间,application+activity
(3)WaitTime: 一般比totaltime大,包括系统影响用时
Adb命令:
adb shell am start –W [packagename]/[activityname]
ps: W是大写
优化前
优化后
思路:
1、 application不要初始化太多东西,放在新启动的intentService去初始化
2、 activity oncreate 、onstart、onresume都不要有太多耗时的操作
3、 splash 与mainactivity 结合做优化
4、 合理延迟加载。展示完了再做耗时操作
代码展示及讲解
/** * An {@link IntentService} subclass for handling asynchronous task requests in * a service on a separate handler thread. * <p> * TODO: Customize class - update intent actions, extra parameters and static * helper methods. */ @Route(path = RouteManager.ROUTER_MAIN_INTIAKUZESERVICE_URL) public class InitializeService extends IntentService { // TODO: Rename actions, choose action names that describe tasks that this // IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS private static final String ACTION_FOO = "com.liveaa.education.action.FOO"; private static final String ACTION_BAZ = "com.liveaa.education.action.BAZ"; // TODO: Rename parameters private static final String EXTRA_PARAM1 = "com.liveaa.education.extra.PARAM1"; private static final String EXTRA_PARAM2 = "com.liveaa.education.extra.PARAM2"; public static final String ACTION_INIT_WHEN_APP_CREATE = "com.liveaa.education.ui.splash.SplashActivity"; public static final String EXTRA_PARAM = "com.liveaa.education.extra.PARAM"; public InitializeService(String name) { super(name); } public InitializeService() { super("InitializeService2"); } /** *启动调用 * @param context */ public static void start(Context context){ // Intent intent = new Intent(context,InitializeService.class); // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // intent.setAction(ACTION_INIT_WHEN_APP_CREATE); // context.startActivity(intent); RouteManager.getInstance().toInitializeService(context); } @Override protected void onHandleIntent(@Nullable Intent intent) { if (intent != null){ perforInit(EXTRA_PARAM); // String action = intent.getAction(); // if (ACTION_INIT_WHEN_APP_CREATE.equals(action)){ // perforInit(EXTRA_PARAM); // } } } /** * 启动初始化操作 * @param extraParam */ private void perforInit(String extraParam) { // 各种初始化,公司项目所以不能展示 } }
1、Application里初始化的东西太多,项目中有stetho,imageloader,glide,bugtags,腾讯的x5浏览器,易观,友盟等
于是新启动一个intentservice去初始化这些东西,具体写法以代码体现,该继承的继承,
这里我们项目里有做过模块化,所以直接从splash到mainactivity中得在Arouter中写路径和跳转方法
InitializeService.start(this);//启动服务执行耗时操作
2、splash里就一张展示的图片,索性将它作为mainactivity里的一个fragment先展示,然后加载mainactivity的布局。
剩下的以代码展示。
viewStub = (ViewStub) findViewById(R.id.content_viewstub); splahFragment = new SplahFragment(); FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.frame,splahFragment); fragmentTransaction.commit(); getWindow().getDecorView().post(new Runnable() { @Override public void run() { viewStub.inflate(); // } }); getWindow().getDecorView().post(new Runnable() { @Override public void run() { handler.postDelayed(new DelayRunnable(MainTabActivity.this,splahFragment),2000); } });
static class DelayRunnable implements Runnable { private WeakReference<Context> contextRef; private WeakReference<SplahFragment> splashFragmentRef; public DelayRunnable(Context context, SplahFragment splashFragment) { contextRef = new WeakReference<Context>(context); splashFragmentRef = new WeakReference<SplahFragment>(splashFragment); } @Override public void run() { if (contextRef != null){ SplahFragment splahFragment = splashFragmentRef.get(); if (splahFragment == null){ return; } FragmentActivity activity = (FragmentActivity) contextRef.get(); FragmentTransaction fragmentTransaction = activity.getSupportFragmentManager().beginTransaction(); fragmentTransaction.remove(splahFragment); fragmentTransaction.commit(); } } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ViewStub android:id="@+id/content_viewstub" android:layout="@layout/ac_main_stub" android:layout_width="match_parent" android:layout_height="match_parent" /> <FrameLayout android:id="@+id/frame" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout> </RelativeLayout>
3、有很多app都在启动的一瞬间会白屏,解决办法是在入口activity中theme里加入backgroud,因为测量布局绘制需要时间,而主题背景很快就展示了,所以一个logo或图片可以让用户看着舒服一些。
<activity android:name=".Splash" android:theme="@style/splashTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="splashTheme" parent="AppTheme"> <item name="android:windowBackground">@drawable/bg_splash</item> </style> </resources>
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/white"></item> <item> <bitmap android:src="@drawable/ic_splash_main" android:gravity="center"></bitmap> </item> </layer-list>