package com.moral.yunfushao; import android.annotation.TargetApi; import android.app.Application; import android.content.Context; import android.content.Intent; import android.os.Build; import android.text.TextUtils; import android.widget.Toast; import com.tencent.bugly.Bugly; import com.tencent.bugly.beta.Beta; import com.tencent.bugly.beta.interfaces.BetaPatchListener; import com.tencent.bugly.crashreport.CrashReport; import com.tencent.tinker.entry.DefaultApplicationLike; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.Locale; /** * @author : chenqi. * @e_mail : 1650699704@163.com. * @create_time : 2018/10/26. * @Package_name: Bugly2 */ public class SampleApplicationLike extends DefaultApplicationLike { public static final String TAG = "Tinker.SampleApplicationLike"; public SampleApplicationLike(Application application, int tinkerFlags, boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime, long applicationStartMillisTime, Intent tinkerResultIntent) { super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime, applicationStartMillisTime, tinkerResultIntent); } @Override public void onCreate() { super.onCreate(); // 设置是否开启热更新能力,默认为true Beta.enableHotfix = true; // 设置是否自动下载补丁,默认为true Beta.canAutoDownloadPatch = true; // 设置是否自动合成补丁,默认为true Beta.canAutoPatch = true; // 设置是否提示用户重启,默认为false Beta.canNotifyUserRestart = true; // 设置升级检查周期为60s(默认检查周期为0s),60s内SDK不重复向后台请求策略); Beta.upgradeCheckPeriod = 10 * 1000; // 设置启动延时为1s(默认延时3s),APP启动1s后初始化SDK,避免影响APP启动速度; Beta.initDelay = 1 * 1000; // 设置更新弹窗默认展示的banner Beta.defaultBannerId = R.mipmap.icon; //设置自定义升级对话框UI布局 // Beta.upgradeDialogLayoutId = R.layout.upgrade_dialog; // 补丁回调接口 Beta.betaPatchListener = new BetaPatchListener() { @Override public void onPatchReceived(String patchFile) { // Toast.makeText(getApplication(), "补丁下载地址" + patchFile, Toast.LENGTH_SHORT).show(); } @Override public void onDownloadReceived(long savedLength, long totalLength) { Toast.makeText(getApplication(), String.format(Locale.getDefault(), "%s %d%%", Beta.strNotificationDownloading, (int) (totalLength == 0 ? 0 : savedLength * 100 / totalLength)), Toast.LENGTH_SHORT).show(); } @Override public void onDownloadSuccess(String msg) { Toast.makeText(getApplication(), "补丁下载成功", Toast.LENGTH_SHORT).show(); } @Override public void onDownloadFailure(String msg) { Toast.makeText(getApplication(), "补丁下载失败", Toast.LENGTH_SHORT).show(); } @Override public void onApplySuccess(String msg) { Toast.makeText(getApplication(), "补丁应用成功", Toast.LENGTH_SHORT).show(); } @Override public void onApplyFailure(String msg) { Toast.makeText(getApplication(), "补丁应用失败", Toast.LENGTH_SHORT).show(); } @Override public void onPatchRollback() { } }; Context context = getApplication().getApplicationContext(); // 获取当前包名 String packageName = context.getPackageName(); // 获取当前进程名 String processName = getProcessName(android.os.Process.myPid()); // 设置是否为上报进程 CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context); strategy.setUploadProcess(processName == null || processName.equals(packageName)); // 设置开发设备,默认为false,上传补丁如果下发范围指定为“开发设备”,需要调用此接口来标识开发设备 Bugly.setIsDevelopmentDevice(context, false); // 初始化Bugly 调试时候设置为true Bugly.init(getApplication(), "6c3f03f59c", 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; } @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) @Override public void onBaseContextAttached(Context base) { super.onBaseContextAttached(base); // 安装tinker // TinkerManager.installTinker(this); 替换成下面Bugly提供的方法 Beta.installTinker(this); } @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) public void registerActivityLifecycleCallback(Application.ActivityLifecycleCallbacks callbacks) { getApplication().registerActivityLifecycleCallbacks(callbacks); } }