zoukankan      html  css  js  c++  java
  • Android 获取服务是否在后台 & 获取TopActivity

    ActivityManager manager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
    if (serviceName.equals(service.service.getClassName())) {
    return true;
    }
    }

    PS:
    serviceName 服务名称(完整的服务名称,包括包名,如com.xxx.xxxService)



    /**
    * 5.0以上版本
    * 需要使用系统签名,同时需要<uses-permission android:name="android.permission.DUMP"/>或声明系统用户
    *
    */
    public static ComponentName getTopActivity_L_U() {
    String cmd = "dumpsys activity activities | grep mResumedActivity";//"dumpsys activity";
    String pkgName = "";
    String className = "";
    String s = "";
    ComponentName result = null;
    Process p = null;
    try {
    p = Runtime.getRuntime().exec(cmd);
    BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line = null;
    while ((line = in.readLine()) != null) {
    if (line.contains("mResumedActivity")) {
    s = line;
    break;
    }
    }
    if (CommonUtil.isNotEmpty(s)) {
    String info = s.substring(s.indexOf("u0 ") + 3, s.lastIndexOf(" "));
    pkgName = info.split("/")[0];

    className = info.split("/")[1];
    if (className.startsWith(".")) {
    className = pkgName + className;
    }

    if (CommonUtil.isNotEmpty(pkgName) && CommonUtil.isNotEmpty(className)) {
    result = new ComponentName(pkgName, className);
    }
    }

    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } finally {
    try {
    p.getInputStream().close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    return result;

    }

    /**
    * 5.0以下版本
    *
    * @param context
    * @return
    */
    public static ComponentName getTopActivity_L_D(Context context) {
    ActivityManager am = (ActivityManager) context
    .getSystemService(context.ACTIVITY_SERVICE);
    ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
    return cn;
    }
  • 相关阅读:
    pandas模块篇(终章)及初识mataplotlib
    pandas模块篇(之三)
    pandas模块篇(之二)
    numpy最后一部分及pandas初识
    anaconda及jupyter notebook的使用之numpy模块的用法(2)
    anaconda及jupyter notebook的了解及使用方法(1)
    python初略复习(2)及python相关数据分析模块的介绍
    Python回顾笔记(此讲大致说明,详情请看之前的笔记)
    Python第三讲
    折半算法
  • 原文地址:https://www.cnblogs.com/mengdao/p/15201932.html
Copyright © 2011-2022 走看看