android 之APP崩溃自动重启

/**
 * Project Name:appPrj_ylwis
 * File Name:MapData.java
 * Package Name:com.neomap.ylwis.view.Data.map
 * Date:2014-3-6下午02:30:47
 * Copyright (c) 2014, [email protected] All Rights Reserved.
 *
*/

package com.neomap.qst.view.tabitem.mapdata;

import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.neomap.qst.dao.controller.AddvNcd_Controller;
import com.neomap.qst.entity.AddvNm_Cd;
import com.neomap.qst.entity.SmSlmcr;
import com.neomap.qst.entity.StpptnrRain;
import com.neomap.qst.entity.Striverr;

public class MapData extends Application {
	//雨情地图数据
	 private List<StpptnrRain> stpptnrRainList = new ArrayList();
	 private List<SmSlmcr> smSlmcrList= new ArrayList();
	 private List<Striverr>  striverrList= new ArrayList();
	 private List<AddvNm_Cd>addvnmList=new ArrayList();
	 private PendingIntent restartIntent;  
	 
	@Override
	public void onCreate() {
		super.onCreate();
	          // 以下用来捕获程序崩溃异常  
		       Intent intent = new Intent();  
		      // 参数1:包名,参数2:程序入口的activity  
		       intent.setClassName("com.neomap.qst", "com.neomap.qst.view.splash.WelcomeAct");  
		        restartIntent = PendingIntent.getActivity(getApplicationContext(), 0,  
		        intent, Intent.FLAG_ACTIVITY_NEW_TASK);  
		         Thread.setDefaultUncaughtExceptionHandler(restartHandler); // 程序崩溃时触发线程  
		
	}
	
	  public UncaughtExceptionHandler restartHandler = new UncaughtExceptionHandler() {  
		           @Override  
		           public void uncaughtException(Thread thread, Throwable ex) {  
		               AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);  
		               mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000,  
		                      restartIntent); // 1秒钟后重启应用  
		               new ActivityContrl().finishProgram(); // 自定义方法,关闭当前打开的所有avtivity  
		          }  
		       };
	
	
       public class ActivityContrl {  
	           private List<Activity> activityList = new ArrayList<Activity>();  
	          
	           public void remove(Activity activity) {  
	               activityList.remove(activity);  
	           }  
	          
	            public void add(Activity activity) {  
	                activityList.add(activity);  
	            }  
	         
	           public void finishProgram() {  
	               for (Activity activity : activityList) {  
	                   if (null != activity) {  
	                        activity.finish();  
	                   }  
	               }  
	               android.os.Process.killProcess(android.os.Process.myPid());  
	            }  
	        }	       
		       
		       
		       
	
	public void clear(){
		
		if(stpptnrRainList != null)stpptnrRainList.clear();
		if(smSlmcrList != null)smSlmcrList.clear();
		if(striverrList != null)striverrList.clear();
	}
	public List<StpptnrRain> getStpptnrRainList() {
		return stpptnrRainList;
	}

	public void setStpptnrRainList(List<StpptnrRain> stpptnrRainList) {
		this.stpptnrRainList = stpptnrRainList;
	}

	public List<SmSlmcr> getSmSlmcrList() {
		return smSlmcrList;
	}

	public void setSmSlmcrList(List<SmSlmcr> smSlmcrList) {
		this.smSlmcrList = smSlmcrList;
	}

	public List<Striverr> getStriverrList() {
		return striverrList;
	}

	public void setStriverrList(List<Striverr> striverrList) {
		this.striverrList = striverrList;
	}
	public List<AddvNm_Cd> getAddvnmList() {
		return addvnmList;
	}
	public void setAddvnmList(List<AddvNm_Cd> addvnmList) {
		this.addvnmList = addvnmList;
	}
}

将HKApplication设置为程序的入口,并在manifest配置文件中的application节点加上android:name="com.neomap.qst.view.tabitem.mapdata"属性即可~~~

相关推荐