美文网首页
android 冷启动性能优化

android 冷启动性能优化

作者: 我弟是个程序员 | 来源:发表于2017-07-30 21:25 被阅读0次

什么是冷启动?

之前从未打开过应用,后台中不存在该应用进程时。这时候点击应用图标icon启动应用程序叫做冷启动。

什么是热启动?

已经启动,点击返回键或是HOME键返回到桌面,然后后台还存在该应用的进程。返回桌面进行一些列操作后,再次点击图标的启动,成为热启动。这里面冷、热启动的主要划分,是通过看后台是否还存在该应用进程来划分的。

冷启动时:肯定会走application,我们的优化主要也是针对他来讲。

  1. 将一系列的初始化操作放在Thread里面,比如各种第三方的sdk的启动啊。
      new Thread(new Runnable() {
            @Override
            public void run() {
                //doInitAction
            }
        }
        ).start();
  1. 将一系列的初始化操作,放在IntentService中,放在后台来执行。IntentService启动的时候,内部开了一个线程,当任务执行完后,IntentService会自动停止,而不需要我们去手动控制。原理和第一条差不多。
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;

public class InitializeService extends IntentService{
    private static final String ACTION_INIT_WHEN_APP_CREATE = "com.sanxin.ttreader";

    public InitializeService() {
        super("InitializeService");
    }

    public static void init(Context context) {
        Intent intent = new Intent(context, InitializeService.class);
        intent.setAction(ACTION_INIT_WHEN_APP_CREATE);
        context.startService(intent);

    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            if (ACTION_INIT_WHEN_APP_CREATE.equals(action)) {
                performInit();
            }
        }
    }

    /**.
     * do you init action here
     */
    private void performInit() {
     //do somthing youself here

    }
}

然后再application 的onCreate方法中使用这个service:

 InitializeService.init(getApplicationContext());
  1. 界面预加载。指的是在启动的Activity启动前,给一个启动图片或是什么的,这样可以增强用户的体验。

在drawable目录下,新建start_window.xml,用来承载一个图片

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:opacity="translucent">


    <item android:drawable="@color/colorPrimary"/>
    <item >
        <bitmap android:src="@drawable/flax.png" android:gravity="center"/>
    </item>
</layer-list>

在style文件中建一个style:

    <!-- 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>
    <!-- 新建一个Theme -->
    <style name="coldLoadTheme" parent="AppTheme">
        <item name="android:windowBackground">@drawable/start_window</item>
    </style>
    

然后在AndroidManifest.xml文件中配置:

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity" android:theme="@style/coldLoadTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

最后在MainActivity中:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        InitializeService.init(getApplicationContext());
    }
}

ok ,这样就好了。

相关文章

  • 【性能优化】Android冷启动优化

    前段时间做冷启动优化,刚好也很久没写博文了,觉得还是很有必要记录下。 一.常规操作 大部分开发者在遇到页面冷启动耗...

  • Android优化文章精选

    Android性能优化典范 Android性能优化典范 - 第1季Android性能优化之渲染篇Android性能...

  • Android性能优化(下)

    Android性能优化 内存泄漏和性能优化方式Android性能优化(上)数据库优化和网络优化Android性能优...

  • android 冷启动性能优化

    什么是冷启动? 之前从未打开过应用,后台中不存在该应用进程时。这时候点击应用图标icon启动应用程序叫做冷启动。 ...

  • Android 性能——冷启动优化

    一、冷热启动概念: 1、冷启动:冷启动因为系统会重新创建一个新的进程分配给它,所以会先创建和初始化Applicat...

  • iOS 性能优化三

    主要讲解APP冷启动的优化 iOS 性能优化一iOS 性能优化二iOS 性能优化三 1. APP 启动的分类 冷...

  • [笔记]Android性能优化 中

    [笔记]Android性能优化 上[笔记]Android性能优化 中[笔记]Android性能优化 下 7.And...

  • [笔记]Android性能优化 下

    [笔记]Android性能优化 上[笔记]Android性能优化 中[笔记]Android性能优化 下 8.And...

  • [笔记]Android性能优化 上

    [笔记]Android性能优化 上[笔记]Android性能优化 中[笔记]Android性能优化 下 说明 这篇...

  • Android 性能优化系列视频(三)

    Android 性能优化系列视频如下 Android 性能优化系列视频(四)Android 性能优化系列视频(五...

网友评论

      本文标题:android 冷启动性能优化

      本文链接:https://www.haomeiwen.com/subject/ozdklxtx.html