android9自动重启,Android 如何捕获崩溃异常并重启应用

第一种方法:通过AppUncaughtExceptionHandler来捕获异常

异常重启操作

因为捕获异常后我要马上关闭掉app即上面的byebye方法,是将app整个进程杀死,如果接着要显示提示对话框,则需要在新的任务栈中打开activity:

public static Intent newIntent(Context context, String title, String ultimateMessage) {

Intent intent = new Intent();

intent.setClass(context, PatchDialogActivity.class);

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

intent.putExtra(EXTRA_TITLE, title);

intent.putExtra(EXTRA_ULTIMATE_MESSAGE, ultimateMessage);

return intent;

}

对话框中给出了重启操作的选项,重启过程的实现:

private void restart() {

Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(intent);

super.onDestroy();

}

第二种方法:通过第三方捕获(腾讯bugly)

集成SDK

继承SDK需要下载官方的so文件

b0721268b4b4

同时集成SDK和NDK

在Module的build.gradle文件中添加依赖和属性配置:

android {

defaultConfig {

ndk {

// 设置支持的SO库架构

abiFilters 'armeabi' //, 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'

}

}

}

dependencies {

compile 'com.tencent.bugly:crashreport:latest.release' //其中latest.release指代最新Bugly SDK版本号,也可以指定明确的版本号,例如2.1.9

compile 'com.tencent.bugly:nativecrashreport:latest.release' //其中latest.release指代最新Bugly NDK版本号,也可以指定明确的版本号,例如3.0

}

同时集成Bugly SDK和NDK的配置如下图所示,后续更新Bugly SDK和NDK时,只需变更配置脚本中的版本号即可。

b0721268b4b4

Alt text

注意:自动集成时会自动包含Bugly SO库,建议在Module的build.gradle文件中使用NDK的“abiFilter”配置,设置支持的SO库架构。

如果在添加“abiFilter”之后Android Studio出现以下提示:

NDK integration is deprecated in the current plugin. Consider trying the new experimental plugin.

则在项目根目录的gradle.properties文件中添加:

android.useDeprecatedNdk=true

参数配置

在AndroidManifest.xml中添加权限:

请避免混淆Bugly,在Proguard混淆文件中增加以下配置:

-dontwarn com.tencent.bugly.**

-keep public class com.tencent.bugly.**{*;}

初始化

CrashReport.initCrashReport(getApplicationContext(), "注册时申请的APPID", false);

在“AndroidManifest.xml”的“Application”中增加“meta-data”配置项:

android:name="BUGLY_APPID"

android:value="" />

android:name="BUGLY_APP_VERSION"

android:value="" />

android:name="BUGLY_APP_CHANNEL"

android:value="" />

android:name="BUGLY_ENABLE_DEBUG"

android:value="" />

不同于“android:versionName”,“BUGLY_APP_VERSION”配置的是Bugly平台的APP版本号。

通过“AndroidManifest.xml”配置后的初始化方法如下:

CrashReport.initCrashReport(getApplicationContext());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值