android报错后,如果友好提示

本文介绍了一种自定义异常处理器CndicExceptionHandler的实现方式,该处理器用于捕获应用程序中未被捕获的异常,并将异常信息记录到指定的日志文件中。通过这种方式可以有效地收集应用崩溃时的错误信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.nhn.android.exception;

import java.io.File;
import java.io.PrintWriter;
import java.lang.Thread.UncaughtExceptionHandler;

import android.content.Context;

import com.nhn.android.util.Global;

public class CndicExceptionHandler implements UncaughtExceptionHandler {

	
	private static CndicExceptionHandler handler;
	private static Context mContext;
	
	public static CndicExceptionHandler getInstence(Context cont){
		mContext=cont;
		if(handler==null){
			handler=new CndicExceptionHandler();
		}
		Thread.setDefaultUncaughtExceptionHandler(handler);
		
		return handler;
	}
	
	@Override
	public void uncaughtException(Thread thread, Throwable ex) {
//		Intent intent = new Intent(mContext, WelcomeActivity.class);
//		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//		mContext.startActivity(intent);
//		
//		android.os.Process.killProcess(android.os.Process.myPid());
		try{
			File file=new File(Global.SD_ROOT+Global.DOWN_PATH+File.separator+Global.ERROR_LOG);
			if(!file.exists()){
				file.createNewFile();
			}
			PrintWriter pw=new PrintWriter(file);
			ex.printStackTrace(pw);
			pw.close();
		}catch(Exception exception){
			
		}
		ex.printStackTrace();
	}

}
activity里调用getInstence方法注册
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值