2016-09-20 4 views
0

내 앱이 BOOT_COMPLETED을 (를) 수신 대기합니다.BOOT_COMPLETED을 (를) 듣는 앱을 다시 시작하는 방법

<receiver android:name=".BootReceiver"> 
    <intent-filter> 
    <action android:name="android.intent.action.BOOT_COMPLETED" />   
</receiver> 

하지만 내 응용 프로그램 충돌하는 경우 는, 어떻게 그것을 자동를 다시 시작 얻을 수 있을까?

BOOT_COMPLETED은 끈적한 의도가 아닙니다.

+1

경우의 uncaughtException()에 들어갈 것이다? – androidXP

+0

@androidXP 예. 있습니다. – newathens

답변

0

질문의 답변을 얻으려면 매우 간단합니다. 이 경우 Thread.setDefaultUncaughtExceptionHandler()를 사용해야합니다. 항상 응용 프로그램 crashed.For Check Full Tutorial Here

public class YourApplication extends Application { 

    private static Context mContext; 

    public static YourApplication instace; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     mContext = getApplicationContext(); 
     instace = this; 
    } 

    @Override 
    public Context getApplicationContext() { 
     return super.getApplicationContext(); 
    } 

    public static YourApplication getIntance() { 
     return instace; 
    } 
} 

DefaultExceptionHandler.java 당신이 자동으로 추락 한 후 응용 프로그램을 다시 시작해야

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.OutputStreamWriter; 
import java.lang.Thread.UncaughtExceptionHandler; 
import java.text.SimpleDateFormat; 
import java.util.Date; 

import android.app.Activity; 
import android.app.AlarmManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Environment; 
import android.util.Log; 

/** 
* This custom class is used to handle exception. 
* 
* @author Chintan Rathod (http://www.chintanrathod.com) 
*/ 
public class DefaultExceptionHandler implements UncaughtExceptionHandler { 

    private UncaughtExceptionHandler defaultUEH; 
    Activity activity; 

    public DefaultExceptionHandler(Activity activity) { 
     this.activity = activity; 
    } 

    @Override 
    public void uncaughtException(Thread thread, Throwable ex) { 

     try { 

      Intent intent = new Intent(activity, RelaunchActivity.class); 

      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
        | Intent.FLAG_ACTIVITY_CLEAR_TASK 
        | Intent.FLAG_ACTIVITY_NEW_TASK); 

      PendingIntent pendingIntent = PendingIntent.getActivity(
        YourApplication.getInstance().getBaseContext(), 0, intent, intent.getFlags()); 

         //Following code will restart your application after 2 seconds 
      AlarmManager mgr = (AlarmManager) YourApplication.getInstance().getBaseContext() 
        .getSystemService(Context.ALARM_SERVICE); 
      mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, 
        pendingIntent); 

         //This will finish your activity manually 
      activity.finish(); 

         //This will stop your application and take out from it. 
      System.exit(2); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
}