Skip to content

Commit

Permalink
优化BaseApplication初始化
Browse files Browse the repository at this point in the history
  • Loading branch information
LoveBabyForeve committed Jun 11, 2021
1 parent d71f9d5 commit de15ed3
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 104 deletions.
96 changes: 96 additions & 0 deletions app/src/main/java/com/xiayule/xiayucommom/App.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package com.xiayule.xiayucommom

import android.os.Process
import android.text.TextUtils
import com.kongzue.dialog.util.BaseDialog
import com.kongzue.dialog.util.DialogSettings
import com.tencent.bugly.crashreport.CrashReport
import com.tencent.bugly.crashreport.CrashReport.UserStrategy
import com.xiayule.commonlibrary.base.BaseApplication
import com.xiayule.commonlibrary.utlis.KLog
import java.io.BufferedReader
import java.io.FileReader
import java.io.IOException

/**
* @Description: java类作用描述
Expand All @@ -16,6 +25,93 @@ class App : BaseApplication() {

override fun onCreate() {
super.onCreate()
// log初始化
KLog.init(BuildConfig.DEBUG)

// 初始化 DiaLogV3
// 初始化 DiaLogV3
initDialogV3()

// 初始化 内存泄漏 腾讯bugly

// 初始化 内存泄漏 腾讯bugly
initBugly()
}

/**
* 初始化 DialogV3
*/
private fun initDialogV3() {
DialogSettings.style = DialogSettings.STYLE.STYLE_IOS //全局主题风格,提供三种可选风格,STYLE_MATERIAL, STYLE_KONGZUE, STYLE_IOS
DialogSettings.theme = DialogSettings.THEME.LIGHT //全局对话框明暗风格,提供两种可选主题,LIGHT, DARK
DialogSettings.tipTheme = DialogSettings.THEME.LIGHT //全局提示框明暗风格,提供两种可选主题,LIGHT, DARK
DialogSettings.init() //初始化清空 BaseDialog 队列
}

/**
* LeakCanary 内存泄漏
* CrashReport 初始化Bugly
*/
private fun initBugly() {
if (!com.xiayule.commonlibrary.BuildConfig.DEBUG) {
// LeakCanary 内存泄漏
// if (LeakCanary.isInAnalyzerProcess(application)) {
// // This process is dedicated to LeakCanary for heap analysis.
// // You should not init your app in this process.
// return;
// }
// LeakCanary.install(application);
// Normal app init code...


// =================Bugly==================
// 获取当前包名
val packageName = baseContext.packageName
// 获取当前进程名
val processName = getProcessName(Process.myPid())
// 设置是否为上报进程
val strategy = UserStrategy(baseContext)
strategy.isUploadProcess = processName == null || processName == packageName
/*
初始化Bugly
第三个参数为SDK调试模式开关,调试模式的行为特性如下:
输出详细的Bugly SDK的Log;
每一条Crash都会被立即上报;
自定义日志将会在Logcat中输出。
建议在测试阶段建议设置成true,发布时设置为false。
*/CrashReport.initCrashReport(baseContext, "a7e554980e", false, strategy)
}
}

/**
* 获取进程号对应的进程名
*
* @param pid 进程号
* @return 进程名
*/
private fun getProcessName(pid: Int): String? {
var reader: BufferedReader? = null
try {
reader = BufferedReader(FileReader("/proc/$pid/cmdline"))
var processName = reader.readLine()
if (!TextUtils.isEmpty(processName)) {
processName = processName.trim { it <= ' ' }
}
return processName
} catch (throwable: Throwable) {
throwable.printStackTrace()
} finally {
try {
reader?.close()
} catch (exception: IOException) {
exception.printStackTrace()
}
}
return null
}

override fun onTerminate() {
BaseDialog.unload()
super.onTerminate()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,6 @@ public <T extends ViewModel> T createViewModel(Class<T> cls) {
}
//----------------------------------------------------------------------------------------------------------------

/**
* findViewById
*
* @param viewId 控件ID
* @param <T> 泛指
* @return 控件
*/
public <T extends View> T getView(int viewId) {
View view = findViewById(viewId);
return (T) view;
}

/**
* 通过Class跳转界面
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,14 @@
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.kongzue.dialog.util.DialogSettings;
import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.bugly.crashreport.CrashReport.UserStrategy;
import com.xiayule.commonlibrary.BuildConfig;
import com.xiayule.commonlibrary.http.networkLoader.HttpRequestProxy;
import com.xiayule.commonlibrary.http.networkLoader.NetRequestType;
import com.xiayule.commonlibrary.imageLoader.ImageLoaderProxy;
import com.xiayule.commonlibrary.utlis.Utils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
* Created by goldze on 2017/6/15.
*/
Expand All @@ -46,11 +37,6 @@ public static synchronized void setApplication(@NonNull Application application)
ImageLoaderProxy.getInstance().init(application);
// 初始化网络加载框架
HttpRequestProxy.getInstance().init(NetRequestType.OKHTTP);
// 初始化 DiaLogV3
initDialogV3();

// 初始化 内存泄漏 腾讯bugly
initBugly();

//注册监听每个activity的生命周期,便于堆栈式管理
application.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
Expand Down Expand Up @@ -97,81 +83,4 @@ public static Application getInstance() {
}
return sInstance;
}

/**
* 初始化 DialogV3
*/
private static void initDialogV3() {
DialogSettings.init();

DialogSettings.style = (DialogSettings.STYLE.STYLE_IOS); //全局主题风格,提供三种可选风格,STYLE_MATERIAL, STYLE_KONGZUE, STYLE_IOS
DialogSettings.theme = (DialogSettings.THEME.LIGHT); //全局对话框明暗风格,提供两种可选主题,LIGHT, DARK
DialogSettings.tipTheme = (DialogSettings.THEME.LIGHT); //全局提示框明暗风格,提供两种可选主题,LIGHT, DARK

}

/**
* LeakCanary 内存泄漏
* CrashReport 初始化Bugly
*/
private static void initBugly() {
if (!BuildConfig.DEBUG) {
// LeakCanary 内存泄漏
// if (LeakCanary.isInAnalyzerProcess(application)) {
// // This process is dedicated to LeakCanary for heap analysis.
// // You should not init your app in this process.
// return;
// }
// LeakCanary.install(application);
// Normal app init code...


// =================Bugly==================
// 获取当前包名
String packageName = sInstance.getPackageName();
// 获取当前进程名
String processName = getProcessName(android.os.Process.myPid());
// 设置是否为上报进程
UserStrategy strategy = new UserStrategy(sInstance);
strategy.setUploadProcess(processName == null || processName.equals(packageName));
/*
初始化Bugly
第三个参数为SDK调试模式开关,调试模式的行为特性如下:
输出详细的Bugly SDK的Log;
每一条Crash都会被立即上报;
自定义日志将会在Logcat中输出。
建议在测试阶段建议设置成true,发布时设置为false。
*/
CrashReport.initCrashReport(sInstance, "2b7d15a3c8", false, strategy);
}
}

/**
* 获取进程号对应的进程名
*
* @param pid 进程号
* @return 进程名
*/
private static String getProcessName(int pid) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
String processName = reader.readLine();
if (!TextUtils.isEmpty(processName)) {
processName = processName.trim();
}
return processName;
} catch (Throwable throwable) {
throwable.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException exception) {
exception.printStackTrace();
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @UpdateRemark: 更新说明
* @Version: 1.0
*/
public abstract class BaseFragment extends Fragment implements IBaseView{
public abstract class BaseFragment extends Fragment implements IBaseView {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down

0 comments on commit de15ed3

Please sign in to comment.