Catching and handling global exceptions in Android xamarin - android

We are trying to prevent the app to restart on exception log the error and kill the app gracefully.
We use the AndroidEnvironment.UnhandledExceptionRaiser += HandleAndroidException; in the mainLauncher / activity to catch unhandled exceptions.
Problems are:
In the HandleAndroidException method, we wish to display a notification to the user letting know the application will close.
The activity object is not available so we are unable to show a HUD / dialog.
After showing the dialog we wish to kill the app and prevent it to restart in a unusable state. ( have tested this.FinishAffinity(); ,Finish(); System.Diagnostics.Process.GetCurrentProcess().Kill(); etc.)
The app still restarts.
In the same method we try to track the error in Visual Studio App Center
(Crashes.TrackError(e.Exception);) The exception is not logged in the appcenter.
(Writing the error to a local logfile works fine.)

Related

Close Android App programatically in Xamarin Forms project

I am using the following code to close the android app in a Xamarin Form project.
var activity = (Activity)Forms.Context;
activity.FinishAndRemoveTask();
It is closing the app but if again I tap on the app it is not opening the new instance of the app as the I can see debugger is still active .
Can anyone help ?
it is not opening the new instance of the app as the I can see debugger is still active
When you use activity.FinishAndRemoveTask() method, this method cant kill your Android app, it just means :
Finishes all activities in this task and removes it from the recent tasks list.
You could use the following code to implement this feature :
FinishAndRemoveTask();
Java.Lang.JavaSystem.Exit(0);// Terminate JVM

Is it possible to hide the error message when an Android service is crashed

I have a bound service in separate process. It crashes occasionally due to the oem native libs on certain phones. I am trying to make the app recover from it.
I would like to silently restart the service and continue the workflow. But it seems the error message is always displayed after the service is crashed. Is there a way to suppress the error? We can handle the unhanded java exception, but not sure how to handle the native crash.
Figured out. Handle the sig works for me.

How can I force a Worklight Android hybrid app to crash?

I know it sounds strange to WANT an app to crash, but I am testing an application that captures app crash data. I would like to create a sample hybrid app that crashes on demand. Typical errors such as divide by zero and null pointer exceptions in the Javascript are handled by the system but the app does not crash. I also tried causing a crash in a Cordova plug-in in Java code but of course that only crashes the plug-in, not the parent app.
Ideas?
I suppose you want to crash the thread in which Worklight runs. In that case, you can go to your AppName.java and throw a runtime exception wherever you want to. It could be onCreate or any other of the Android lifecycle events. (This is in src/com.projectName in your Android project).
invoke requestFeature() after setting content view
try to open activity not declared in androidmanifest.xml
do an action requiring specific permission but do not request this permission in androidmanifest.xml

How to prevent Android App from crashing abruptly

I know this is a very broad question on how to prevent an Android app from crashing. I understand there could be many reason behind the app crashing.
Primarly my app crashed because of 2 main reasons :-
1) Out of memory while taking pictures and storing byte array in memory. I also uses bitmap to redraw the image captured.
2) Camera issues. App has a feature of autofocusing on touch events and while actually taking the pictures. These autofocus often crashes into each other. I have handled it using cancelling any existing autofocus code and discarding any further on touch events using flags once the picture is being captured. But still some time app crashes due to unknow reason.
There may be more reason behind app crashes. So my question is
1) Is there a way I can identify that app has crashed and handle that event so that instead of just showing the messsage ""UnForunately App has stopped working. Force Close." I can give a better user friendly message to user and stop the app programatically.
2) If out of memory every happens, is there a way I can identify that my App is running low on allocated memory and I can handle the scenario. OnLowMemory will give the low memory status of entire device, not just my application. I use lot of cache to store the images & heap for bitmap.
3) If the camera ever crashes (because of any reason), is there a way I can handle the scenario.
Thanks in advance.
Gagan
The android application usually crashes if there is a run time error.
You can locate and avoid the crash,using android monitor and write your code in try catch block.
Example:
try{
"your code which might give error"
}
catch(Exception e)
{
Log.e("TAG or Some text",e.toString());
finish(); //or some methods you want to do if the code inside try block fails;
}
Now, the app wont crash and you can locate the error in android monitor by searching for TAG error.

Network availability to make a call

In my application I have written code to make a call to helpline just by clicking on button. When I copy this application to my Android device, if the network is not available it exits the application. I want to display a error message rather than exiting.
One more issue,I have written code to add a helpline number to contactbook when the application is loaded into device. When i check it on emulator it works, but on device it's not working :(
Use logcat to see why your application is exiting.
For example if its due to an error or uncaught exception, detect it (make sure an object isn't null before calling its methods) or catch and handle the exception.

Categories

Resources