在android中掌握onPause()onStop()

时间:2015-02-18 21:01:07

标签: android crash onpause

我想实现某种监视器功能,告诉我我的应用程序何时崩溃或停止或暂停。所以我有3个活动,到目前为止我已经实现了当调用mainActivity onPause时会向我发送邮件,但是,我只想知道是否有人停止整个应用程序而不仅仅是其中一个活动(因为用户跳转)它们之间)。是否有某种整体onStop()方法或我可以使用的东西?

谢谢!

这是我的代码

 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 个答案:

答案 0 :(得分:0)

也许创建三个布尔值... activityOneAlive,activityTwoAlive和activityThreeAlive。当每个活动点击onStart()时,它会写入true,并且在切换到应用程序中的其他活动之前,它会将自己的布尔值设置为false。当任何活动点击onPause()并且其布尔值为true时,则发送您的电子邮件。

答案 1 :(得分:0)

创建一个Application类,并在其中创建一个实现ActivityLifecycleCallbacks接口的对象。在该对象中,在每个onStart中递增一个整数,并在每个onStop中递减它。同样在onStop中,如果整数为0,则发送您的电子邮件。

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();
    }
}

答案 2 :(得分:0)

在您的Activity类中添加一个私有字段:

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
    }

你可以把它变成一个主人Activity,然后从中扩展你的其他活动。当然,我已经省去了super()调用等等。需要一些改编,但你应该能够将所有这些结合起来,以确定你的活动暂停或停止的确切原因。

您现在需要解决的问题是通用异常捕获。 See this question.