如何自动停止后台服务程序时不使用? [英] How to stop BackGround service automatically when app is not in use?

查看:252
本文介绍了如何自动停止后台服务程序时不使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我打算用我的通知一部分,使用的后台服务目前该服务将运行即使我的应用程序得到停止或任我使用其他应用程序

但现在我需要的是如果应用程序没有被激活5分钟以上的后台服务应停止自动

我已经走了具有以下,但没有用它仍然是相同的:

 公共类ForegroundCheckTask扩展的AsyncTask<上下文,太虚,布尔> {  @覆盖
  保护布尔doInBackground(上下文... PARAMS){
    最后上下文的背景下=参数[0] .getApplicationContext();
    返回isAppOnForeground(上下文);
  }  私人布尔isAppOnForeground(上下文的背景下){
    ActivityManager activityManager =(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
    清单< RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    如果(appProcesses == NULL){
      返回false;
    }
    最后弦乐的packageName = context.getPackageName();
    对于(RunningAppProcessInfo appProcess:appProcesses){
      如果(appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND&放大器;&放大器; appProcess.processName.equals(的packageName)){
        返回true;
      }
    }
    返回false;
  }
}

尝试了很多修正,但不变的是还有没有其他的走请帮我的朋友来解决这个问题。

更新:

 公共类ApplicationActivity实现Application.ActivityLifecycleCallbacks {
        //我在这里使用四个独立的变量。你可以,当然,只是用两个
        //递增/递减他们,而不是使用四个递增它们。
        公共静态INT恢复;
        公共静态INT暂停;
        公共静态INT开始;
        公共静态INT停止;
    公共静态布尔stopStatus = FALSE;
        @覆盖
        公共无效onActivityCreated(活动活动,捆绑savedInstanceState){
        }        @覆盖
        公共无效onActivityDestroyed(活动活动){
        }        @覆盖
        公共无效onActivityResumed(活动活动){
            ++恢复;
        }        @覆盖
        公共无效onActivityPaused(活动活动){
            ++暂停;
            android.util.Log.w(测试,应用程序在前台:+(复会>暂停));
        }        @覆盖
        公共无效onActivitySaveInstanceState(活动活动,捆绑outState){
        }        @覆盖
        公共无效onActivityStarted(活动活动){
            ++开工;
        }        @覆盖
        公共无效onActivityStopped(活动活动){
            ++停止;
            stopStatus =真;
            android.util.Log.w(测试,应用程序可见:+(开始>停止));
        }公共类MyApplication的扩展应用{
    @覆盖
    公共无效的onCreate(){
        //只需添加处理程序,这就是它!无需添加任何code
        //每一个活动。一切都包含在MyLifecycleHandler
        //用code的只有几行。现在*这是*漂亮。
        registerActivityLifecycleCallbacks(新ApplicationActivity());
    }
}


解决方案

现在还不清楚到底是什么你正在尝试做的,以及为什么。如果您的服务是的子类服务则可以停止自己或你的活动可以明确地阻止它。如果您的活动,其绑定,而不是调用 startService(),系统会自动停止,一旦所有的客户已绑定的服务。 Android是建立,以便它可以启动,并根据需要阻止他们。请注意,即使你的服务和所有的活动都停止了,你的应用程序的过程,直到系统需要资源坚持。

Now i am going on with my notification part for that using BackGround Service in current the service will run even after my app get stopped or either i use other app

But Now what i need was if the app is not active for 5min or more the BackGround service should stop by automatically.

I had gone with the following but no use it remains the same:

public class ForegroundCheckTask extends AsyncTask<Context, Void, Boolean> {

  @Override
  protected Boolean doInBackground(Context... params) {
    final Context context = params[0].getApplicationContext();
    return isAppOnForeground(context);
  }

  private boolean isAppOnForeground(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses == null) {
      return false;
    }
    final String packageName = context.getPackageName();
    for (RunningAppProcessInfo appProcess : appProcesses) {
      if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
        return true;
      }
    }
    return false;
  }
}

Tried a lot to fix but remains the same is there any other go please help me friends to solve this problem.

Updated:

 public class ApplicationActivity  implements Application.ActivityLifecycleCallbacks {
        // I use four separate variables here. You can, of course, just use two and
        // increment/decrement them instead of using four and incrementing them all.
        public static int resumed;
        public static int paused;
        public static int started;
        public static int stopped;
    public static boolean stopStatus = false;
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        }

        @Override
        public void onActivityDestroyed(Activity activity) {
        }

        @Override
        public void onActivityResumed(Activity activity) {
            ++resumed;
        }

        @Override
        public void onActivityPaused(Activity activity) {
            ++paused;
            android.util.Log.w("test", "application is in foreground: " + (resumed > paused));
        }

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

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

        @Override
        public void onActivityStopped(Activity activity) {
            ++stopped;
            stopStatus = true;
            android.util.Log.w("test", "application is visible: " + (started > stopped));
        }

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        // Simply add the handler, and that's it! No need to add any code
        // to every activity. Everything is contained in MyLifecycleHandler
        // with just a few lines of code. Now *that's* nice.
        registerActivityLifecycleCallbacks(new ApplicationActivity());
    }
}

解决方案

It's still not clear exactly what you are trying to do and why. If your service is a subclass of Service then it can stop itself or your activities can explicitly stop it. If your activities bind with it rather than calling startService(), the system can automatically stop the service once all clients have been unbound. Android is built so that it can start and stop them as needed. Note that even if your service and all activities are stopped, your app's process will stick around until the system needs resources.

这篇关于如何自动停止后台服务程序时不使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆