我跑在后台的应用程序 [英] Running my app in background

查看:210
本文介绍了我跑在后台的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检查,我的计划是在后台还是不行!我prgram有一个包(我所有的课是在一个封装)。我搜索,终于让我找到这条路。我写这篇code,但是当我的计划是在后台,我的code无法运行。为什么?

I want to check, my program is in background or not! my prgram have one package(all of my classes is in one package). I searched and finally i found this way. i write this code but when my program is in background, my code not run. why?

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

    @Override
    protected Boolean doInBackground(Context... arg0) {
        // TODO Auto-generated method stub
        Context context=arg0[0];
        return isAppInBackground(context);
    }

    private boolean isAppInBackground(Context context){
        ActivityManager activityManager=(ActivityManager)context.getSystemService(context.ACTIVITY_SERVICE);
        List<RunningAppProcessInfo> appProcess=activityManager.getRunningAppProcesses();
        if (appProcess==null)
            return false;
        final String packageName=context.getPackageName();

        Log.e("packageName", packageName);
        //Toast.makeText(context, "packageName="+packageName,Toast.LENGTH_LONG).show();

        for(RunningAppProcessInfo processInfo:appProcess){
            if((processInfo.importance==RunningAppProcessInfo.IMPORTANCE_BACKGROUND)&&( processInfo.processName.equals(packageName))){
                Log.e("process", "in background");
                Toast.makeText(context, "process in background", Toast.LENGTH_LONG).show();
                Log.e("packagenmae", processInfo.processName);
                return true;
            }
        }
        return false;
    }

}

和我添加此code在这个类,但如果是我跑在后台的程序,否则当!像往常一样,我的计划是在前台!是什么问题?

and i add this code in this class but when my program in background, else if was run ! like always my program is in foreground ! what is problem??

if((processInfo.importance==RunningAppProcessInfo.IMPORTANCE_BACKGROUND)&&( processInfo.processName.equals(packageName))){
    Log.e("process", "in background");
    Toast.makeText(context, "process in background", Toast.LENGTH_LONG).show();
    Log.e("packagenmae", processInfo.processName);
    return true;
}
else if((processInfo.importance==RunningAppProcessInfo.IMPORTANCE_FORGROUND)&&( processInfo.processName.equals(packageName))){
    Log.e("process","in forground");
    return true;
}

我可以使用,在运行的API另一种方式7+?这种方式应该是可靠的?
感谢您的提醒

Can i use another way that run in api 7+? That way should be reliable... Thanks for advise

推荐答案

如果您想知道您的应用程序在背景或没有,那么你必须使用服务

If you want to know that your app is in background or not then you have to use Service for that

您应该与此去:

http://www.vogella.com/articles/AndroidServices/article.html

这篇关于我跑在后台的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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