Skip to content

Commit 259fbfd

Browse files
author
chunle.wei
committed
新增: 构建了TestDemo的基本启动流程.
1 parent 9b1dbba commit 259fbfd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1296
-0
lines changed

.idea/.gitignore

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

.idea/compiler.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.

.idea/misc.xml

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

app/.gitignore

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

app/build.gradle

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
plugins {
2+
id 'com.android.application'
3+
id 'com.jakewharton.butterknife'
4+
5+
id 'kotlin-android'
6+
id 'kotlin-android-extensions'
7+
}
8+
9+
android {
10+
compileSdk 32
11+
12+
defaultConfig {
13+
applicationId "com.wcl.testdemo"
14+
minSdk 21
15+
targetSdk 32
16+
versionName rootProject.ext.version_name
17+
versionCode rootProject.ext.version_code
18+
19+
// testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
20+
}
21+
22+
buildTypes {
23+
release {
24+
minifyEnabled false
25+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
26+
}
27+
debug {
28+
minifyEnabled false
29+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
30+
}
31+
}
32+
compileOptions {
33+
sourceCompatibility JavaVersion.VERSION_1_8
34+
targetCompatibility JavaVersion.VERSION_1_8
35+
}
36+
}
37+
38+
dependencies {
39+
40+
implementation 'androidx.appcompat:appcompat:1.3.0'
41+
implementation 'com.google.android.material:material:1.4.0'
42+
// testImplementation 'junit:junit:4.13.2'
43+
// androidTestImplementation 'androidx.test.ext:junit:1.1.3'
44+
// androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
45+
46+
implementation 'com.jakewharton:butterknife:10.2.3'
47+
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'//黄油刀(8.4.0以上版本,该库会使用androidx.如果我的其他库使用support,会冲突使得项目无法build)
48+
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'//黄油刀
49+
50+
implementation 'com.blankj:utilcodex:1.31.0'//TODO: github上的工具类.(如果用了AndroidX,引入utilcodex否则引入utilcode)
51+
}

app/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

app/src/main/AndroidManifest.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
package="com.wcl.testdemo">
5+
6+
<application
7+
android:allowBackup="true"
8+
android:dataExtractionRules="@xml/data_extraction_rules"
9+
android:fullBackupContent="@xml/backup_rules"
10+
android:icon="@mipmap/ic_launcher"
11+
android:label="@string/app_name"
12+
android:roundIcon="@mipmap/ic_launcher_round"
13+
android:supportsRtl="true"
14+
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
15+
tools:targetApi="31">
16+
17+
<!-- 测试入口 -->
18+
<activity
19+
android:name=".init.TestActivity"
20+
android:exported="true">
21+
<intent-filter>
22+
<action android:name="android.intent.action.MAIN" />
23+
24+
<category android:name="android.intent.category.LAUNCHER" />
25+
</intent-filter>
26+
</activity>
27+
28+
</application>
29+
30+
</manifest>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.wcl.testdemo.init;
2+
3+
import android.app.Activity;
4+
import android.app.Application;
5+
import android.os.Build;
6+
7+
import com.blankj.utilcode.util.AppUtils;
8+
import com.blankj.utilcode.util.CrashUtils;
9+
import com.blankj.utilcode.util.DeviceUtils;
10+
import com.blankj.utilcode.util.LogUtils;
11+
import com.blankj.utilcode.util.ProcessUtils;
12+
import com.blankj.utilcode.util.ScreenUtils;
13+
import com.blankj.utilcode.util.Utils;
14+
15+
16+
/**
17+
* @Author WCL
18+
* @Date 2020/4/27 16:10
19+
* @Version 1.0
20+
* @Description
21+
*/
22+
public class AppApplication extends Application {
23+
24+
@Override
25+
public void onCreate() {
26+
super.onCreate();
27+
if (ProcessUtils.isMainProcess()) { //是主进程
28+
initLog(); //初始化日志工具.
29+
initCrash(); //初始化崩溃收集器.
30+
initAppStatusListener(); //注册App前后台切换监听器.
31+
}
32+
}
33+
34+
//初始化日志.
35+
private void initLog() {
36+
//通用配置
37+
LogUtils.getConfig().setGlobalTag("WCL");//TAG不会做落地保存,是记录在运行内存中的.
38+
// LogUtils.getConfig().setFilePrefix("log");
39+
LogUtils.getConfig().setLog2FileSwitch(true);//默认开启日志落地.
40+
LogUtils.getConfig().setFileFilter(LogUtils.I);//控制日志落地等级.
41+
LogUtils.getConfig().setSaveDays(7);//设置log可保留天数.
42+
LogUtils.getConfig().setLogHeadSwitch(AppUtils.isAppDebug());//是否展示日志中可跳转到代码的头部信息.
43+
LogUtils.getConfig().setBorderSwitch(AppUtils.isAppDebug());//是否展示每条日志的边框图形.
44+
LogUtils.getConfig().setConsoleFilter(AppUtils.isAppDebug() ? LogUtils.V : LogUtils.I);//设置AS控制台过滤器.
45+
//设备信息打印.
46+
LogUtils.v("====================>>Application initApp<<====================",
47+
"device_model: " + DeviceUtils.getModel(),//设备型号.
48+
"device_id: " + DeviceUtils.getUniqueDeviceId(),//设备ID.
49+
"android_version: " + Build.VERSION.RELEASE,//Android系统版本.
50+
"android_sdk_version: " + Build.VERSION.SDK_INT,//Android API版本.
51+
"screenW:" + ScreenUtils.getScreenWidth() + " screenH:" + ScreenUtils.getScreenHeight() + " appW:" + ScreenUtils.getAppScreenWidth() + " appH:" + ScreenUtils.getAppScreenHeight(),//屏幕宽高和应用宽高.
52+
AppUtils.getAppInfo()//APP信息.
53+
);
54+
}
55+
56+
//初始化崩溃收集器.
57+
private void initCrash() {
58+
//方式一:(手写)
59+
// Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {//崩溃收集器1.
60+
// @Override
61+
// public void uncaughtException(@NonNull Thread thread, @NonNull Throwable throwable) {
62+
// // 得到详细报错信息:
63+
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
64+
// PrintWriter pw = new PrintWriter(baos);
65+
// throwable.printStackTrace(pw);
66+
// Throwable cause = throwable.getCause();
67+
// while (cause != null) {
68+
// cause.printStackTrace(pw);
69+
// cause = cause.getCause();
70+
// }
71+
// pw.close();
72+
// String errMsg = new String(baos.toByteArray()); //得到详细报错信息,可落地处理.
73+
// // 崩溃处理: (弹出"APP崩溃提示"界面,友好的提示用户)
74+
// if (AppUtils.isAppForeground()) { //APP在前台:启动"崩溃提示界面".
75+
// Intent intent = new Intent(AppApplication.this, CrashActivity.class);
76+
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
77+
// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
78+
// AppApplication.this.startActivity(intent);
79+
// Process.killProcess(Process.myPid()); //杀死崩溃进程.
80+
// } else { //APP在后台:退出APP.
81+
// AppUtils.exitApp();
82+
// }
83+
// }
84+
// });
85+
//方式二: (用库)
86+
// 正常情况下setDefaultUncaughtExceptionHandler会覆盖之前的崩溃收集器.
87+
// 该方式中会先保存收集器1,再覆盖,当收集器2被调用时再主动调用一次收集器1,故收集器2应放在后面初始化.
88+
CrashUtils.init();//崩溃收集器2.
89+
}
90+
91+
//注册App前后台切换监听器.
92+
private void initAppStatusListener() {
93+
AppUtils.registerAppStatusChangedListener(new Utils.OnAppStatusChangedListener() {
94+
@Override
95+
public void onForeground(Activity activity) {
96+
// LogUtils.i("APP前台:" + activity);
97+
}
98+
99+
@Override
100+
public void onBackground(Activity activity) {
101+
// LogUtils.i("APP后台:" + activity);
102+
}
103+
});
104+
}
105+
106+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.wcl.testdemo.init;
2+
3+
import android.content.Intent;
4+
import android.os.Bundle;
5+
import android.view.View;
6+
import android.view.ViewGroup;
7+
8+
import com.blankj.utilcode.util.LogUtils;
9+
import com.wcl.testdemo.R;
10+
11+
import java.util.ArrayList;
12+
13+
import androidx.appcompat.app.AppCompatActivity;
14+
import androidx.recyclerview.widget.LinearLayoutManager;
15+
import androidx.recyclerview.widget.RecyclerView;
16+
17+
/**
18+
* @Author WCL
19+
* @Date 2020/4/27 15:22
20+
* @Version 1.0
21+
* @Description 测试Demo的主入口.
22+
*/
23+
public class TestActivity extends AppCompatActivity implements TestAdapter.OnItemClickListener {
24+
25+
/**
26+
* Comment:列表即将展示的数据.
27+
*/
28+
private final ArrayList<TestBean> mList = new ArrayList<>();
29+
30+
{
31+
mList.add(new TestBean("(0) Java基础测试."));
32+
mList.add(new TestBean("(1) Android基础测试."));
33+
mList.add(new TestBean("(2) 四大组件测试."));
34+
mList.add(new TestBean("(3) 视图测试. (View; Dialog)"));
35+
mList.add(new TestBean("(4) 设备功能测试. (传感器; 蓝牙; WIFI)"));
36+
mList.add(new TestBean("(5) 三方库测试. (插件化; 热更)"));
37+
}
38+
39+
@Override
40+
public void onItemClick(int position, View rootView) {
41+
// LogUtils.d("点击条目: " + position);
42+
Intent intent = null;
43+
switch (position) {
44+
case 0:
45+
// intent = new Intent(TestActivity.this, ThemeActivity.class);
46+
// intent.putExtra(ThemeActivity.THEME_TYPE, 4);
47+
break;
48+
case 1:
49+
break;
50+
case 2:
51+
break;
52+
case 3:
53+
break;
54+
case 4:
55+
break;
56+
case 5:
57+
break;
58+
default://其他情况
59+
intent = null;
60+
break;
61+
}
62+
}
63+
64+
@Override
65+
protected void onCreate(Bundle savedInstanceState) {
66+
super.onCreate(savedInstanceState);
67+
setContentView(R.layout.activity_test);
68+
initRcv();//初始化RecyclerView.
69+
}
70+
71+
//初始化RecyclerView.
72+
private void initRcv() {
73+
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv);//[一].通过id找到布局中的控件.
74+
TestAdapter adapter = new TestAdapter(TestActivity.this, mList);
75+
adapter.setOnItemClickListener(this);//条目点击事件监听.
76+
recyclerView.setAdapter(adapter);//[二].设置适配器(类似于ListView)
77+
//注意: rcv和lv不一样,需要根据需求设置设置不同种类的LayoutManager.
78+
//(Ⅰ)LinearLayoutManager(线性管理器):支持[横向][纵向][反转].
79+
//LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false); //参数2-orientation:方向; 参数3-reverseLayout:翻转布局.
80+
//(Ⅱ)GridLayoutManager(网格布局管理器):支持反转.
81+
//GridLayoutManager layoutManager = new GridLayoutManager(this, 3, GridLayoutManager.HORIZONTAL, false); //参数2-spanCount:显示为几列或者几行; 参数3-orientation:方向; 参数4-reverseLayout:翻转布局.
82+
//(Ⅲ)StaggeredGridLayoutManager(瀑布式布局管理器).
83+
//StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); //参数1-spanCount:显示为几列或者几行; 参数2-orientation:方向;
84+
LinearLayoutManager layoutManager = new LinearLayoutManager(TestActivity.this) {
85+
@Override
86+
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
87+
// return super.generateDefaultLayoutParams();
88+
//RecyclerView的字条目想要沾满全屏需要在此处重新设置.
89+
return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
90+
}
91+
};
92+
recyclerView.setLayoutManager(layoutManager);//[三].设置LayoutManager.
93+
}
94+
95+
}

0 commit comments

Comments
 (0)