Skip to content

Commit 2d698c0

Browse files
committed
添加App启动优化代码
1 parent a2d87d5 commit 2d698c0

36 files changed

+1418
-61
lines changed

.idea/codeStyles/Project.xml

Lines changed: 122 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LaunchStarter/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

LaunchStarter/build.gradle

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
apply plugin: 'com.android.library'
2+
3+
android {
4+
compileSdkVersion 29
5+
buildToolsVersion "29.0.2"
6+
7+
8+
defaultConfig {
9+
minSdkVersion 16
10+
targetSdkVersion 29
11+
versionCode 1
12+
versionName "1.0"
13+
14+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
15+
consumerProguardFiles 'consumer-rules.pro'
16+
}
17+
18+
buildTypes {
19+
release {
20+
minifyEnabled false
21+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
22+
}
23+
}
24+
25+
}
26+
27+
dependencies {
28+
implementation fileTree(dir: 'libs', include: ['*.jar'])
29+
30+
implementation 'androidx.appcompat:appcompat:1.0.2'
31+
testImplementation 'junit:junit:4.12'
32+
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
33+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
34+
}

LaunchStarter/consumer-rules.pro

Whitespace-only changes.

LaunchStarter/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fmt.launch.starter;
2+
3+
import android.content.Context;
4+
5+
import androidx.test.platform.app.InstrumentationRegistry;
6+
import androidx.test.ext.junit.runners.AndroidJUnit4;
7+
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
11+
import static org.junit.Assert.*;
12+
13+
/**
14+
* Instrumented test, which will execute on an Android device.
15+
*
16+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
17+
*/
18+
@RunWith(AndroidJUnit4.class)
19+
public class ExampleInstrumentedTest {
20+
@Test
21+
public void useAppContext() {
22+
// Context of the app under test.
23+
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24+
25+
assertEquals("com.fmt.launch.starter.test", appContext.getPackageName());
26+
}
27+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.fmt.launch.starter" >
3+
4+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
5+
</manifest>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fmt.launch.starter;
2+
3+
import android.os.Looper;
4+
import android.os.MessageQueue;
5+
import com.fmt.launch.starter.task.DispatchRunnable;
6+
import com.fmt.launch.starter.task.Task;
7+
import java.util.LinkedList;
8+
import java.util.Queue;
9+
10+
/**
11+
* 延迟初始化:使用MessageQueue中的IdleHandler在(系统空闲的时候)进行延迟初始化
12+
*/
13+
public class DelayInitDispatcher {
14+
15+
private Queue<Task> mDelayTasks = new LinkedList<>();//队列保存延迟初始化任务
16+
17+
//核心点IdleHandler
18+
private MessageQueue.IdleHandler mIdleHandler = new MessageQueue.IdleHandler() {
19+
@Override
20+
public boolean queueIdle() {
21+
//空闲时,每次只执行一次任务
22+
if(mDelayTasks.size()>0){
23+
Task task = mDelayTasks.poll();
24+
new DispatchRunnable(task).run();
25+
}
26+
return !mDelayTasks.isEmpty();
27+
}
28+
};
29+
30+
//添加延迟初始化任务
31+
public DelayInitDispatcher addTask(Task task){
32+
mDelayTasks.add(task);
33+
return this;
34+
}
35+
36+
//开启延迟初始化
37+
public void start(){
38+
Looper.myQueue().addIdleHandler(mIdleHandler);
39+
}
40+
41+
}

0 commit comments

Comments
 (0)