Fragment Runtime Exception Debugging - java

I'm a bit new to Android development, but am working on an application with a relatively large code base that loads in a library that does JNI calls. When calling a library method, in my fragment, a Runtime Exception is thrown due to an error in interfacing with JNI, which causes a crash (the pop-up window "Unfortunately, APP_NAME has stopped.") but doesn't actually stop the application; enabling it to be used in a broken mode in which assets and other parts aren't loaded properly in my ViewPager.
I'm wondering if this is common behavior for Runtime Exceptions in Fragments?
I'd prefer for the whole application to just crash and not enable this broken mode; but haven't had enough experience with Android to know if this is intended behavior or not.
I've tried using the Android Studio debugger after the crash occurs but it appears the debugger stops which is understandable behavior because the application should have terminated.

sounds like your app is handling such exceptions, you might be interested to look into something like this in your work space:
private Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread thread, Throwable ex) {
Log.e("TestApplication", "Uncaught exception is: ", ex);
// log it & phone home.
androidDefaultUEH.uncaughtException(thread, ex);
}
};

Related

ProviderInstaller doesn't work on emulators and caught error is not being read

In my application, I am connecting to an SSL server and in order to make it possible for API's less than 21 I need to install ProviderInstaller. Below is a preview of my code:
if (Build.VERSION.SDK_INT < 21) {
try {
ProviderInstaller.installIfNeeded(this);
} catch (Exception e) {
//Crashes the app when accessing the getMessage
Log.i("error", e.getMessage());
}
}
This works well on device with API 16, but it doesnt work on emulators with API 16. And when I try to access the getMessage() error it crashes the whole application.
Why doesn't the ProviderInstaller.installIfNeeded(this) work on emulators, and also why does the application crashes when accessing the e.getMessage() function?
installIfNeeded can throw two exceptions GooglePlayServicesRepairableException and GooglePlayServicesNotAvailableException, They are thrown when Google Play Services is not up-to-date or enabled and when Google Play services are not available, respectively. Possibly there is a problem with Google Play services in the emulator.
And app crashes when you access getmessage() is because the exception that you are catching doesn't have a message. You need to log the entire exception, not just the exception's message. You can verify this from the documentation of getMessage which says
the detail message string of this Throwable instance (which may be null).
From documentation of GooglePlayServicesRepairableException
In these cases, client code can use getConnectionStatusCode() in
conjunction with getErrorDialog(android.app.Activity, int, int) to
provide users with a localized Dialog that will allow users to
install, update, or otherwise enable Google Play services.

closing muliple process with different affinity android [duplicate]

I'd like to force stop my Android application when I click closeButton. This is my code.
protected void onCreate(Bundle savedInstanceState) {
this.setContentView(R.layout.layoutxml);
this.closeButton = (Button)this.findViewById(R.id.close);
this.closeButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
This finishes my application. If I go to Settings -> Applications -> Manage applications -> <my application name>, I can see the 'Force Stop' button is enabled. Does this mean my application was not stopped completely?
How can I finish my Android application completely and disable the 'Force Stop' button inthe 'Settings'? From my limited experience, when an 'Exception' (ex. NullPointerException) occurs in the application, it stops abnormally, looks like it finished completely, and the 'Force Stop' button looks disabled.
Another way is
android.os.Process.killProcess(android.os.Process.myPid());
I don't think it's all that bad to do this, provided you put those calls in onDestroy(). (If you kill your process in the middle of event handling, all kinds of bad things—like the touch focus going into the ether—can happen.)
Nevertheless, you need a compelling reason to deviate from best practice, which is to just call finish() and let the OS take care of killing off your process when/if it needs to.
Note: This does not kill the entire app, but if what you want to do is to finish all the app activities, this is the best option.
Android ≥ 16
finishAffinity();
Android < 16
ActivityCompat.finishAffinity(Activity activity)
Hope this helps
A bad way to kill the application would be System.exit(0)
Edit:
I believe I owe some explanation. Android handles the application lifecycle on its own, and you are not supposed to 'ForceClose' it, and I don't know any good way to do it. Generally its ok if your application is still alive in the background, this way if user launches it again it will pop up quickly.
I know it is a late reply , hope this helps some one.
You can try finishAndRemoveTasks(); instead of finish(); in your snippet.
This would kill your application's all activities and all process and even remove for recent apps from task manager.
Note: If you have use any kind of handler or thread in your code make sure you remove its functionalities and then use the above suggested code , if not NullPointer Exception or ResourceNotFound Exception would occur.
Short and simple
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory( Intent.CATEGORY_HOME );
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
Why not to make a Shell-Call to ActivityManager?
try {
Runtime.getRuntime().exec("am force-stop com.me.myapp");
} catch (IOException e) {
e.printStackTrace();
}
The link below has the solution.
Its worked for me.
finishAffinity()
How to force stop my android application programmatically?

how to close an android application after exception caught

I am designing an Android Application where i want to check details if crash happens. So i used Thread.UncaughtExceptionHandler() Class. It generate detail of Crashed Thread but Application Stuck once crash occur. I want to close my application immediately after crash but also want to detail of Thread. I include following code in the Activity class. Any suggestion?
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
#Override
public void uncaughtException(Thread thread, Throwable ex) {
Log.e("UserApp", "uncaught_exception_handler: uncaught exception in thread " + thread.getName(), ex);
if(ex instanceof RuntimeException)
throw (RuntimeException)ex;
if(ex instanceof Error)
throw (Error)ex;
Log.e("UserApp", "uncaught_exception handler: unable to rethrow checked exception");
}
you can try finish(); in your exception block
Try this...
You can use,
YourActivityname.this.finish(); or
android.os.Process.killProcess(android.os.myPid());
You should use first one because it is a best practice, which is to just call finish() and let the OS take care of killing off your process when/if it needs to.
In spite of if you use second one, kill your process in the middle of event handling, all kinds of bad things -it is a bad practice.

GWT client side unchecked exceptions

I've been working on a project which uses GWT (and SmartGWT), which is (are) new to me.
Whenever runtime exceptions are thrown in the client (no RPCs involved) nothing happens. By that I mean the method does not continue executing, there are no alerts, there is nothing in the Javascript error console or the SmartGWT error console.
I'm assuming this isn't normal practice in GWT applications. Where are these errors (e.g. null pointers) normally logged/handled?
Thanks in advance.
You can do catch them in the next way
GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() {
#Override
public void onUncaughtException(Throwable e) {
GWT.log(e.getMessage(), e);
}
});
You can replace the GWT.log by any other logging method.

How to close another app in Android?

I have been developing an app, and I need to close another app in my code. Does anyone know any api to call to close an app?
BTW: my app will be pre-installed.
thanks
Since Android 2.2 (i.e. going forward), you can only close the background processes of other apps, you are no longer able to close their main activities.
If your app is targeting Android <2.2, look atandroid.permission.RESTART_PACKAGE.
If you want it to work properly on 2.2 and above (which you should :-)), look at android.permission.KILL_BACKGROUND_PROCESSES, but again, this only closes background services and such and might "mess up" the other app rather than doing any good.
With the right permissions, you can then do the following:
private ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
am.restartPackage("com.jimmy.appToBeClosed");
Try This
ActivityManager am = (ActivityManager) getApplicationContext().getSystemService("activity");
Method forceStopPackage;
forceStopPackage =am.getClass().getDeclaredMethod("forceStopPackage",String.class);
forceStopPackage.setAccessible(true);
forceStopPackage.invoke(am, pkg);
In manifest file add this
<uses-permission android:name="android.permission.FORCE_STOP_PACKAGES"></uses-permission>
If both applications are yours, you can use AIDL for inter-process communication to send a message telling the other application to close. See http://developer.android.com/guide/developing/tools/aidl.html.
I have been able to close another app on Android 12 successfully. Here is how:
Basically, I am closing another app from a service although you should be able to do it from an app too.
My service is a privileged system app that gets installed in system/priv-app/ (It has LOCAL_PRIVILEGED_MODULE := true in its Android.mk)
I added <uses-permission android:name="android.permission.FORCE_STOP_PACKAGES" /> in AndroidManifest.xml
I added in privapp-permissions.xml
<privapp-permissions package="<my service package name>">
<permission name="android.permission.FORCE_STOP_PACKAGES"/>
</privapp-permissions>
I called in my service this method with the package name of the application I want to close:
private void closePackageApp(String namePackage) {
ActivityManager activityManager = (ActivityManager)
context.getSystemService(Context.ACTIVITY_SERVICE);
try {
Method forceStopPackage = activityManager.getClass().
getDeclaredMethod("forceStopPackage", String.class);
forceStopPackage.setAccessible(true);
forceStopPackage.invoke(activityManager, namePackage);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
}
I tested this and in the logs, I can see the app is being closed. However the app is not removed from the recent screen (logs suggested the app was disposed without first being removed with the input manager!).
However, I am sure the app was really being closed when it was in the background by comparing its lifecycle on opening again. Normally, it is onPause->onResume but now it is onPause->onCreate.
You don't ever really want to close another application, due to Android activity lifecycle.
There's no benefit, and always detriment to closing another app if it's not yours, and very little benefit to closing your own.
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
If you know for certain that you'll never, ever need a root activity and its children (an "app"), you can stop it to free memory (it doesn't free that much), but if you do the user may restart it while it's still in cache, stopped, which can cause problems if the stopped state is restored. So this is a bad practice.

Categories