Rainmaker
Rainmaker

Reputation: 11110

catch all types of errors from whole application Android

I have a big Android application and I have a requirement to log all types of errors that could happen in my Application including "Out of memory" etc. Is it possible to accomplish?

I can use

try {

} catch(throwable t) {

}

but adding a whole code to try/catch sounds bad. And how can I catch an errors like "out of memory"?

Upvotes: 1

Views: 3258

Answers (3)

Junaid Hafeez
Junaid Hafeez

Reputation: 1616

yes exactly it is possible. You can write a parent activity and handle exceptions globally like in this example. After that You can use Google analytics, Crashlytics to view your daily reports of crashes etc

Upvotes: 1

Muhammed GÜNEŞ
Muhammed GÜNEŞ

Reputation: 302

To tracking crash reports best way is using 3rd party libraries like Fabric or firebase(google). If you cannot use like this libraries, you can try this.

public class MyActivity extends Activity implements Thread.UncaughtExceptionHandler{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        if(ex.getClass().equals(OutOfMemoryError.class))
        {
            try {
                android.os.Debug.dumpHprofData(fileName);
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
        ex.printStackTrace();
    }
}

Upvotes: 4

Kiran Benny Joseph
Kiran Benny Joseph

Reputation: 6823

Best way is to include analytics.

please refer this. crashlytics is a library that can notify almost all problems. Try it.

Upvotes: 0

Related Questions