Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

YMSHXH/AndroidAllException

Repository files navigation

week3

//全局捕获异常Application
public class MyApp extends Application {
@Override
public void onCreate() {
 super.onCreate();
 //初始化了
 KqwException handler = KqwException.getInstance(this);
 Thread.setDefaultUncaughtExceptionHandler(handler);
 
}
/ *AndroidManifest.xml 
 * 配置name
 *
 */
<application
 android:name=".MyApp"
 android:allowBackup="true"
 android:icon="@mipmap/ic_launcher"
 android:label="@string/app_name"
 android:roundIcon="@mipmap/ic_launcher_round"
 android:supportsRtl="true"
 android:theme="@style/AppTheme">
</application>
/ * 自定义KqwException类
 * 实现Thread.UncaughtExceptionHandler接口
 *
 */
public class KqwException implements Thread.UncaughtExceptionHandler {
 private static KqwException myCrashHandler;
 private Context mContext;
 private KqwException(Context context) {
 mContext = context;
 }
 public static synchronized KqwException getInstance(Context context) {
 if (null == myCrashHandler) {
 myCrashHandler = new KqwException(context);
 }
 return myCrashHandler;
 }
 @Override
 public void uncaughtException(Thread t, Throwable e) {
 long threadId = t.getId();
 String message = e.getMessage();
 String localizedMessage = e.getLocalizedMessage();
 Log.i("KqwException", "------------------------------------------------------");
 Log.i("KqwException", "threadId = " + threadId);
 Log.i("KqwException", "message = " + message);
 Log.i("KqwException", "localizedMessage = " + localizedMessage);
 Log.i("KqwException", "------------------------------------------------------");
 e.printStackTrace();
 Log.i("KqwException", "------------------------------------------------------");
 // TODO 下面捕获到异常以后要做的事情,可以重启应用,获取手机信息上传到服务器等
 Log.i("KqwException", "------------------应用被重启----------------");
 // 重启应用
 mContext.startActivity(mContext.getPackageManager().getLaunchIntentForPackage(mContext.getPackageName()));
 //干掉当前的程序
 android.os.Process.killProcess(android.os.Process.myPid());
 }
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

AltStyle によって変換されたページ (->オリジナル) /