如何检查是否在活动背景/前景从服务运行?前景、背景

2023-09-12 05:28:14 作者:帅的不明显

我已经创建了开始从活动的onCreate,并停止在活动的方法的onDestroy服务。现在我已经从服务的方法来检查,在前景/背景活动是否正在运行(对于某些情况下,像应用程序强制关闭)。我该怎么办呢?

我需要做的这个堂妹,据我所知,或者不能保证调用的onDestroy活动的方法,如果任何应用程序强制关闭的任何形式的崩溃。所以,我的服务,开始时,我的活动推介会不会任何崩溃或任何有力的闭幕活动后停止。

我看到此链接的前景在哪里活动可以进行检查。但我需要检查在运行活动的任何状态(无论是前台还是后台)

解决方案

最后更新

要检查所有活动:

  @覆盖
保护无效的onStop(){
super.onStop();

    如果(AppConstants.isAppSentToBackground(getApplicationContext())){
        //你什么都想要的应用程序关闭简单地关闭会话之后

    }
}
 

方法来检查我们的应用程序运行与否:

 公共静态布尔isAppSentToBackground(最终上下文的背景下){

    尝试 {
        ActivityManager AM =(ActivityManager)上下文
                .getSystemService(Context.ACTIVITY_SERVICE);
        //第在RunningTasks列表总是前景
        // 任务。
        RunningTaskInfo foregroundTaskInfo = am.getRunningTasks(1)获得(0);
        字符串foregroundTaskPackageName = foregroundTaskInfo.topActivity
                .getPackageName(); //获取顶级脱颖而出地面活动
        PackageManager下午= context.getPackageManager();
        PackageInfo foregroundAppPackageInfo = pm.getPackageInfo(
                foregroundTaskPackageName,0);

        字符串foregroundTaskAppName = foregroundAppPackageInfo.applicationInfo
                .loadLabel(PM)的ToString();

        // Log.e(,foregroundTaskAppName +----------+
        // foregroundTaskPackageName);
        如果(!foregroundTaskAppName.equals(您的应用程序名称)){
            返回true;
        }
    }赶上(例外五){
        Log.e(isAppSentToBackground,+ E);
    }
    返回false;
}
 
我国检验检测服务业发展背景及重要性

回答再次更新

使用下面的方法,你的包name.It将返回true,如果你的任何活动是在前台。

 公共布尔isForeground(字符串的mypackage){
ActivityManager AM =(ActivityManager)getSystemService(ACTIVITY_SERVICE);
 名单< ActivityManager.RunningTaskInfo> runningTaskInfo = am.getRunningTasks(1);

     组件名componentInfo = runningTaskInfo.get(0).topActivity;
   如果(componentInfo.getPackageName()等于(mypackage中))返回true;
返回false;
}
 

回答更新时间:

首先检查这个链接Checking如果Android应用程序在后台运行

http://developer.android.com/guide/topics/fundamentals。 HTML#lcycles 是一个Android应用程序的生命周期的描述。

该方法的onPause()被调用时,活动进入后台。所以,你可以禁用更新通知,这种方法。

 公共静态布尔isApplicationSentToBackground(最终上下文的背景下){
    ActivityManager AM =(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
    名单< RunningTaskInfo>任务= am.getRunningTasks(1);
    如果(!tasks.isEmpty()){
      组件名topActivity = tasks.get(0).topActivity;
      如果(!topActivity.getPackageName()。等于(context.getPackageName())){
        返回true;
      }
    }

    返回false;
  }
 

在menifest添加permisions以及

 <使用-权限的Andr​​oid:名称=android.permission.GET_TASKS/>
 

I have created a service which starts from an activity's onCreate and stops on the activity's onDestroy method. Now I have to check from a method of the service that whether the activity is running in foreground/background(for some cases like application is forcefully closed). How can I do that?

I need to do this coz as far I know there is no guarantee of calling onDestroy method of an activity if any the application is forcefully closed or any kind of crash. So, my service which starts when my activity launches won't stop after any crash or any forceful closing event.

I have seen this link where foreground activities can be checked. But I need to check a running activity in whatever state (either foreground or background)

解决方案

Final Update

To check for all activities:

@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;
}

Answer updated again

Use the below method with your package name.It will return true if any of your activity is in foreground.

public boolean isForeground(String myPackage){
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
 List< ActivityManager.RunningTaskInfo > runningTaskInfo = am.getRunningTasks(1); 

     ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
   if(componentInfo.getPackageName().equals(myPackage)) return true;
return false;
}

Answer Updated

Check this link first Checking if an Android application is running in the background

http://developer.android.com/guide/topics/fundamentals.html#lcycles is a description of the Life Cycle of an android application.

The method onPause() gets called when the activity goes into the background. So you can deactivate the update notifications in this method.

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

add permisions in the menifest as well

<uses-permission android:name="android.permission.GET_TASKS" />