如何检测Android应用程序在后台和前台何时处理一些代码

时间:2017-11-14 10:45:18

标签: android android-activity android-anr-dialog android-application-class

我需要知道用户何时将我的Android应用程序发送到后台,然后将其恢复到前台

当用户将应用程序发送到后台时,我应该执行服务器调用,当返回前台时,我必须清除一些数据

我已按以下方式实施

public class MyApplication implements Application.ActivityLifecycleCallbacks {

@Override
    public void onActivityStopped(final Activity activity) {

// my logic goes here
}
}

但是这种方式在后台制作了很多ANR,当我把我的逻辑放在AsyncTask中时它不起作用而且它在后台关闭应用程序

任何人都可以建议我如何在不产生ANR的情况下以另一种方式满足我的要求

3 个答案:

答案 0 :(得分:1)

最简单的方法是(IMHO):

 itemView.setOnClickListener(this);

@Override
    public void onClick(View v) {
        if (v.getId() == id of itemView(R.id.itemview)){
        //do not use this
        onClickListener.onClick(getAdapterPosition());
       //just call the function you need to call on Click of itemview
}
    }

不要忘记添加build.gradle:

public class YourApplication extends Application implements LifecycleObserver {

    @Override
    public void onCreate() {
        super.onCreate();
        ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    private void onAppBackgrounded() {
        Log.d("YourApplication", "YourApplication is in background");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    private void onAppForegrounded() {
        Log.d("YourApplication", "YourApplication is in foreground");
    }
}

答案 1 :(得分:0)

使用:

@Override
    public void onStop()
    {
        super.onStop();
        // Do your stuff here when you are stopping your activity
    }

@Override
    public void onResume()
    {
        super.onResume();
        // Do your stuff here when comes back to activity
    }

当您调用锁定设备onStop时,以及重新打开onResume时,这也可以起作用。

答案 2 :(得分:-1)

private boolean isAppInBackground(Context context) {
        boolean isInBackground = true;
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
            List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
            for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
                if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    for (String activeProcess : processInfo.pkgList) {
                        if (activeProcess.equals(context.getPackageName())) {
                            isInBackground = false;
                        }
                    }
                }
            }
        } else {
            List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
            ComponentName componentInfo = taskInfo.get(0).topActivity;
            if (componentInfo.getPackageName().equals(context.getPackageName())) {
                isInBackground = false;
            }
        }

        return isInBackground;
    }

并在清单

中添加以下权限
<uses-permission android:name="android.permission.GET_ACCOUNTS" />