码迷,mamicode.com
首页 > 移动开发 > 详细

判断android应用是否在运行等方式

时间:2014-09-22 23:38:53      阅读:350      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   color   io   ar   for   div   sp   

/***
     * 判断应用当前是否前台运行
     * 
     * @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;
    }
/***
     * 判断应用是否在运行
     * 
     * @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;
    }

 

判断android应用是否在运行等方式

标签:android   style   blog   color   io   ar   for   div   sp   

原文地址:http://www.cnblogs.com/chrono/p/3986765.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!