7

我实际上正在使用此代码来检查 onPause 中的应用程序是否进入后台。

public static boolean isApplicationSentToBackground(final Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService( Context.ACTIVITY_SERVICE );
    List<RunningTaskInfo> tasks = am.getRunningTasks( 1 );
    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get( 0 ).topActivity;
        String name = LockScreenActivity.class.getName();
        String topAPN = topActivity.getPackageName();
        String conAPN = context.getPackageName();

        if (topActivity.getClassName().equals( name ) || !topActivity.getPackageName().equals( context.getPackageName() )) {
            return true;
        }
    }
    return false;
}

到目前为止,这段代码在 Android 4.4 上运行良好。如果现在我检查它们是否相等(topAPN并且conAPN当应用程序发送到 android <= 4.3 的后台时它们总是不相等)。

你知道如何解决这个问题吗?有什么改变吗?

4

2 回答 2

5

我遇到了同样的问题。我为新版本解决了它。只需使用此代码

public static boolean isApplicationSentToBackground(final Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
        ComponentName topActivity = tasks.get(0).topActivity;
        if (!topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
        }
    }
    return false;
}

并在 onPause 方法中以这种方式调用此函数

 @Override
protected void onPause() {
    super.onPause();
    handler.sendMessage(new Message());
}

Handler handler=new Handler(new Handler.Callback() {
    @Override
    public boolean handleMessage(Message msg) {
        if (DeviceManager.isApplicationSentToBackground(getApplicationContext())) {
          paused = true;
      }
        return false;
    }
});

我不知道 excat 的原因,但可能是因为处理程序中的 diff 线程我得到了正确的值

于 2014-02-17T11:37:57.670 回答
0

此代码适用于 android 4.4 我已声明在 onStop 中使用了此方法,它是:

// 应用程序代码返回

@Override
protected void onStop() {
super.onStop();

    if (AppConstants.isAppSentToBackground(getApplicationContext())) {
        // Do what ever you want after app close simply Close session

    }
}

    // Method to Check Our app is running or Not
    public static boolean isAppSentToBackground(final Context context) {

    try {
        ActivityManager am = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        // The first in the list of RunningTasks is always the foreground
        // task.
        RunningTaskInfo foregroundTaskInfo = am.getRunningTasks(1).get(0);
        String foregroundTaskPackageName = foregroundTaskInfo.topActivity
                .getPackageName();// get the top fore ground activity
        PackageManager pm = context.getPackageManager();
        PackageInfo foregroundAppPackageInfo = pm.getPackageInfo(
                foregroundTaskPackageName, 0);

        String foregroundTaskAppName = foregroundAppPackageInfo.applicationInfo
                .loadLabel(pm).toString();

        // Log.e("", foregroundTaskAppName +"----------"+
        // foregroundTaskPackageName);
        if (!foregroundTaskAppName.equals("Your App name")) {
            return true;
        }
    } catch (Exception e) {
        Log.e("isAppSentToBackground", "" + e);
    }
    return false;
}
于 2013-12-06T05:37:50.333 回答