zoukankan      html  css  js  c++  java
  • 判断android应用是否在运行等方式

    /***
         * 判断当前程序是不在前台显示
         * 
         * @return
         */
        public static boolean isTopRun(Context context) {
            Boolean isTopRun = false;
            try {
                ActivityManager activityManager = (ActivityManager) context
                        .getSystemService(Context.ACTIVITY_SERVICE);
                List<RunningTaskInfo> tasksInfo = activityManager
                        .getRunningTasks(1);
                if (tasksInfo.size() > 0) {
                    String packageName = tasksInfo.get(0).topActivity
                            .getPackageName();
                    if (!TextUtils.isEmpty(packageName)
                            && packageName.startsWith(getAppPackageName())) {
                        isTopRun = true;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return isTopRun;
        }
      /***
         * 判断应用是否在运行
         * 
         * @return
         */
        public static boolean isAppRunning(Context context){
            boolean isAppRunning = false;
            try {
                ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
                List<RunningTaskInfo> list = activityManager.getRunningTasks(100);
                
                String pageName = "com.example.packagename";
                pageName = getAppPackageName();
                for (RunningTaskInfo info : list) {
                    if (info.topActivity.getPackageName().equals(pageName) || info.baseActivity.getPackageName().equals(pageName)) {
                    isAppRunning = true;
                    Log.i("SystemUtils",info.topActivity.getPackageName() + " info.baseActivity.getPackageName()="+info.baseActivity.getPackageName());
                    break;
                    }    
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            return isAppRunning;
        }
      /***
         * 获取当前显示的Activity
         * 
         * @return
         */
        public static void getTopActivity(Context context){
            
            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            ComponentName cn = am.getRunningTasks(1).get(0).topActivity; 
        }
  • 相关阅读:
    第三章 SpringCloud之Eureka-Client服务提供者
    第二章 SpringCloud之Eureka-Server服务发现组件
    第一章 SpringCloud简介
    Storm之WordCount初探
    Solr之java实现增删查操作
    Solr安装并导入mysql数据
    java之消息队列ActiveMQ实践
    Ant Design使用方法
    Reactjs之实现js跳转路由
    React之js实现跳转路由
  • 原文地址:https://www.cnblogs.com/chrono/p/3986765.html
Copyright © 2011-2022 走看看