यदि हम अपवादों को ठीक से नहीं संभालते हैं, तो एंड्रॉइड एप्लिकेशन में, हमें आमतौर पर "फोर्स बंद" त्रुटि मिलती है।
बल बंद होने पर मैं अपने आवेदन को स्वचालित रूप से कैसे पुनः आरंभ कर सकता हूं?
क्या इसके लिए किसी विशिष्ट अनुमति का उपयोग किया गया है?
यदि हम अपवादों को ठीक से नहीं संभालते हैं, तो एंड्रॉइड एप्लिकेशन में, हमें आमतौर पर "फोर्स बंद" त्रुटि मिलती है।
बल बंद होने पर मैं अपने आवेदन को स्वचालित रूप से कैसे पुनः आरंभ कर सकता हूं?
क्या इसके लिए किसी विशिष्ट अनुमति का उपयोग किया गया है?
जवाबों:
इसे पूरा करने के लिए आपको दो काम करने होंगे:
नीचे देखें कि ये कैसे करना है:
Thread.setDefaultUncaughtExceptionHandler()सभी अनकैप्ड अपवाद को पकड़ने के लिए कॉल करें , जिसमें केस uncaughtException()विधि कहा जाएगा। "फोर्स क्लोज़" दिखाई नहीं देगा और एप्लिकेशन अनुत्तरदायी होगा, जो कि बहुत अच्छी बात नहीं है। अपने एप्लिकेशन को पुनरारंभ करने के लिए जब यह दुर्घटनाग्रस्त हो जाए तो आपको निम्न कार्य करना चाहिए:
में onCreateविधि, अपने मुख्य गतिविधि में एक प्रारंभ PendingIntentसदस्य:
Intent intent = PendingIntent.getActivity(
YourApplication.getInstance().getBaseContext(),
0,
new Intent(getIntent()),
getIntent().getFlags());
फिर अपनी uncaughtException()विधि में निम्नलिखित डालें :
AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, intent);
System.exit(2);
आपको भी बुलाना System.exit()होगा, नहीं तो काम नहीं होगा। इस तरह आपका आवेदन 2 सेकंड के बाद पुनः आरंभ होगा।
आखिरकार आप अपने इरादे में कुछ झंडा लगा सकते हैं कि आवेदन दुर्घटनाग्रस्त हो गया और आपकी onCreate()विधि में आप "मैं माफी चाहता हूं, आवेदन दुर्घटनाग्रस्त हो गया, आशा है कि फिर कभी नहीं दिखा सकता है :)"।
चाल सुनिश्चित करें कि यह पहली जगह में फोर्स क्लोज नहीं करता है।
यदि आप उस Thread.setDefaultUncaughtExceptionHandler()विधि का उपयोग करते हैं जो आप अपवादों को पकड़ सकते हैं जो आपके एप्लिकेशन को फोर्स क्लोज कर रहे हैं।
किसी एप्लिकेशन द्वारा उठाए गए अपवादों को लॉग इन करने के लिए उपयोग करने के उदाहरण के लिए इस प्रश्न पर एक नज़र डालें UncaughtExceptionHandler।
यदि आप Crittercism या किसी अन्य त्रुटि रिपोर्ट सेवा का उपयोग करते हैं, तो स्वीकृत उत्तर लगभग सही है।
final UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
public void uncaughtException(Thread thread, Throwable ex) {
Intent launchIntent = new Intent(activity().getIntent());
PendingIntent pending = PendingIntent.getActivity(CSApplication.getContext(), 0,
launchIntent, activity().getIntent().getFlags());
getAlarmManager().set(AlarmManager.RTC, System.currentTimeMillis() + 2000, pending);
defaultHandler.uncaughtException(thread, ex);
}
});
public class ForceCloseExceptionHandalingActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setContentView(MyLayout());
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
myHandaling(paramThread, paramThrowable);
}
});
}
private ViewGroup MyLayout(){
LinearLayout mainLayout = new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.VERTICAL);
Button btnHello =new Button(this);
btnHello.setText("Show all button");
btnHello.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setContentView(MyLayout2());
}
});
mainLayout.addView(btnHello);
return mainLayout;
}
private ViewGroup MyLayout2(){
LinearLayout mainLayout = new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.VERTICAL);
Button btnHello =new Button(this);
btnHello.setText("I am a EEROR uncaughtException");
btnHello.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Alert","btn uncaughtException::");
Toast.makeText(ForceCloseExceptionHandalingActivity.this, "Alert uncaughtException222",Toast.LENGTH_LONG).show();
View buttone = null;
setContentView(buttone);
}
});
Button btnHello2 =new Button(this);
btnHello2.setText("I am a EEROR Try n catch");
btnHello2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try{
View buttone = null;
setContentView(buttone);
}
catch (Exception e) {
Log.e("Alert","Try n catch:::");
Toast.makeText(ForceCloseExceptionHandalingActivity.this, "Alert Try n catch",Toast.LENGTH_LONG).show();
setContentView(MyLayout());
}
}
});
mainLayout.addView(btnHello);
mainLayout.addView(btnHello2);
return mainLayout;
}
public void myHandaling(Thread paramThread, Throwable paramThrowable){
Log.e("Alert","Lets See if it Works !!!" +"paramThread:::" +paramThread +"paramThrowable:::" +paramThrowable);
Toast.makeText(ForceCloseExceptionHandalingActivity.this, "Alert uncaughtException111",Toast.LENGTH_LONG).show();
Intent in =new Intent(ForceCloseExceptionHandalingActivity.this,com.satya.ForceCloseExceptionHandaling.ForceCloseExceptionHandalingActivity.class);
startActivity(in);
finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
@Override
protected void onDestroy() {
Log.e("Alert","onDestroy:::");
Toast.makeText(ForceCloseExceptionHandalingActivity.this, "Alert onDestroy",Toast.LENGTH_LONG).show();
super.onDestroy();
}
इस वर्ग को अपने पैकेज में जोड़ें
public class MyExceptionHandler implements
java.lang.Thread.UncaughtExceptionHandler {
private final Context myContext;
private final Class<?> myActivityClass;
public MyExceptionHandler(Context context, Class<?> c) {
myContext = context;
myActivityClass = c;
}
public void uncaughtException(Thread thread, Throwable exception) {
StringWriter stackTrace = new StringWriter();
exception.printStackTrace(new PrintWriter(stackTrace));
System.err.println(stackTrace);// You can use LogCat too
Intent intent = new Intent(myContext, myActivityClass);
String s = stackTrace.toString();
//you can use this String to know what caused the exception and in which Activity
intent.putExtra("uncaughtException", "Exception is: " + stackTrace.toString());
intent.putExtra("stacktrace", s);
myContext.startActivity(intent);
//for restarting the Activity
Process.killProcess(Process.myPid());
System.exit(0);
}}
अपने आवेदन में या हर गतिविधि वर्ग में, ऑनक्रीट () विधि के अंदर फिर कॉल करें:
Thread.setDefaultUncaughtExceptionHandler(new MyExceptionHandler(this,
SplashScreenActivity.class));