0

I want to implement some sort of a monitor function, that tells me when my app is crashed or stopped or on pause. So I have 3 activities and I achieved so far that when in the mainActivity onPause is called it will send me a mail, however, I only want to know if someone stops the whole app and not just one of the activities (since the user jumps between them). Is there some kind of an overall onStop() Method or something that I can use?

Thanks !

This is my code

 protected void onStop() {

    super.onStop();
    new Thread(new Runnable() {
        public void run() {
            try {
                GMailSender sender = new GMailSender(
                        "email address",
                        "pw");
                sender.sendMail("Pause", "The app has paused",
                        "email address",
                        "email address");
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
            }
        }
    }).start();
}

3 Answers3

0

Maybe create three Booleans... activityOneAlive, activityTwoAlive, and activityThreeAlive. When each activity hits onStart(), it writes true, and right before switching to a different activity in your app, it sets its own Boolean to false. When any activity hits onPause(), and its Boolean is true, then send your email.

scubasteve623
  • 637
  • 4
  • 7
0

Create an Application class and in there, create an object that implements the ActivityLifecycleCallbacks interface. In that object, increment an integer in every onStart, and decrement it in every onStop. Also in onStop, if the integer is 0, then send your email.

http://developer.android.com/reference/android/app/Application.ActivityLifecycleCallbacks.html

package com.your.package;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Application;
import android.os.Build;
import android.os.Bundle;


public class ExampleApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            setupLifecycleCallbacks();
        }
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
                sendEmail();
            }
        });
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    private void setupLifecycleCallbacks() {
        registerActivityLifecycleCallbacks( new ActivityLifecycleCallbacks() {
            int mActivityCount = 0;
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(Activity activity) {
                mActivityCount++;
            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {
                mActivityCount--;
                if (mActivityCount == 0){
                    sendEmail();
                }
             }

             @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });
     }

    private void sendEmail() {
        new Thread(new Runnable() {
            public void run() {
                try {
                    GMailSender sender = new GMailSender(
                        "email address",
                        "pw");
                    sender.sendMail("Pause", "The app has paused",
                        "email address",
                        "email address");
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
                }
            }
        }).start();
    }
}
Sam Dozor
  • 40,335
  • 6
  • 42
  • 42
0

Add a private field to your Activity class:

private userIsLeavingActivity = false;

public onStart(){
    ...
    userIsLeavingActivity = false;
    ...
}

public onbackPressed(){
    ...
    userIsLeavingActivity = true;
    ...
}

public onUserLeaveHint(){
    ...
    userIsLeavingActivity = true;
    ...
}

public onPause(){
    if(isFinishing()){
       // activity is finishing, not just paused.
    }
}

public onStop()
{
    if(userIsLeavingActivity){
        // do what you want going to another activity or app
    }
    else{
        // your activity is being stopped by something else
    }

You could this into a master Activity then extend your other activities from this. Of course, I've left out the super() calls and so on. Needs some adaptation but you should be able to combine all of these to determine exactly why your activity is pausing or stopping.

The problem you now need to solve is a universal exception catch. See this question.

Community
  • 1
  • 1
Simon
  • 14,407
  • 8
  • 46
  • 61