获取当前运行的包android

时间:2017-03-09 13:00:58

标签: android service package

我正在尝试使用Android 5.0和7.0在两台设备上获取当前正在运行的任务。 我用了这段代码:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="Body">
        <xsl:for-each-group select="*" group-starting-with="h1">
           <xsl:if test="not(self::p/img)">
             <xsl:variable name="image" select="preceding-sibling::*[1][self::p/img]" />
             <topic>
                <xsl:attribute name="id">topic_<xsl:number count="h1 | h2"/></xsl:attribute>
                <title>
                   <xsl:apply-templates select="node()"/>
                </title>
                <xsl:for-each-group select="current-group() except ." group-starting-with="h2">
                   <xsl:choose>
                      <xsl:when test="self::h2">
                        <xsl:if test="position() = 1">
                            <body><xsl:apply-templates select="$image"/></body>
                        </xsl:if>
                         <topic>
                            <xsl:attribute name="id">topic_<xsl:number count="h1 | h2"/></xsl:attribute>
                            <title>
                               <xsl:apply-templates select="node()"/>
                            </title>
                            <body><xsl:apply-templates select="current-group()[not(self::p/img)] except ."/></body>
                         </topic>
                      </xsl:when>
                      <xsl:otherwise>
                         <body><xsl:apply-templates select="$image|current-group()[not(self::p/img)]"/></body>
                      </xsl:otherwise>
                   </xsl:choose>
                </xsl:for-each-group>
                </topic>
             </xsl:if>
          </xsl:for-each-group>
       </xsl:template>

       <xsl:template match="p">
       <p><xsl:apply-templates/></p>
       </xsl:template>

       <xsl:template match="img">
      <xsl:element name="image">
            <xsl:if test="@src">
               <xsl:attribute name="href"><xsl:value-of select="@src"/></xsl:attribute>
            </xsl:if>
       </xsl:element>
   </xsl:template>
</xsl:stylesheet>

问题是我得到了我的应用程序包,但是当我启动另一个应用程序时,我得到了:

mActivityManager =(ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
       if(Build.VERSION.SDK_INT > 20){
                      mCurrentPackageName = mActivityManager.getRunningAppProcesses().get(0).processName;
                    }
                    else{
                        mCurrentPackageName = mActivityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
                }

我想知道为什么会这样,因为在Android 4.4上它运作正常。

1 个答案:

答案 0 :(得分:0)

检查这个答案: Android 5.1.1 and above - getRunningAppProcesses() returns my application package only

此代码适用于我:

if (Build.VERSION.SDK_INT < 22) { 
                runningAppProcesses = ((ActivityManager) getSystemService(ACTIVITY_SERVICE)).getRunningAppProcesses();
} else runningAppProcesses = ProcessManager.getRunningAppProcessInfo(this);