Android快速获取当前Activity类名的三种方法

80 阅读4分钟

ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)

  Main stack:

  * TaskRecord{42955d30 #2 A com.miui.home U 0}

    numActivities=1 rootWasReset=true userId=0

    affinity=com.miui.home

    intent={act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10600000 cmp=com.miui.home/.launcher.Launcher}

    realActivity=com.miui.home/.launcher.Launcher

    askedCompatMode=false

    lastThumbnail=null lastDescription=null

    lastActiveTime=710219844 (inactive for 1158s)

    ignoreFromRecent=false

    * Hist #0: ActivityRecord{42707110 u0 com.miui.home/.launcher.Launcher}

        packageName=com.miui.home processName=com.miui.home

        launchedFromUid=0 userId=0

        app=ProcessRecord{42907f38 13729:com.miui.home/u0a10035}

        Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000000 cmp=com.miui.home/.launcher.Launcher }

        frontOfTask=true task=TaskRecord{42955d30 #2 A com.miui.home U 0}

        taskAffinity=com.miui.home

        realActivity=com.miui.home/.launcher.Launcher

        baseDir=/system/app/MiuiHome.apk

        dataDir=/data/user/0/com.miui.home

        stateNotNeeded=true componentSpecified=false isHomeActivity=true

        compat={240dpi} labelRes=0x7f0c0004 icon=0x7f0200cd theme=0x7f0f0000

        config={1.0 ?mcc?mnc zh_CN ldltr sw360dp w360dp h614dp 240dpi nrml long port finger -keyb/v/h dpad/v skin=/system/framework/framework-res.apk s.5 themeChanged=0 themeChangedFlags=0}

        launchFailed=false haveState=false icicle=null

        state=RESUMED stopped=false delayedResume=false finishing=false

        keysPaused=false inHistory=true visible=true sleeping=false idle=true

        fullscreen=true noDisplay=false immersive=false launchMode=2

        frozenBeforeDestroy=false thumbnailNeeded=false forceNewConfig=false

        thumbHolder: 42955d30 bm=null desc=null

        waitingVisible=true nowVisible=true lastVisibleTime=-22m27s145ms



  Running activities (most recent first):

    TaskRecord{42955d30 #2 A com.miui.home U 0}

      Run #0: ActivityRecord{42707110 u0 com.miui.home/.launcher.Launcher}



  mResumedActivity: ActivityRecord{42707110 u0 com.miui.home/.launcher.Launcher}

  mFocusedActivity: ActivityRecord{42707110 u0 com.miui.home/.launcher.Launcher}

  mLastPausedActivity: ActivityRecord{43859658 u0 com.kingroot.kinguser/.activitys.SuNotifyActivity}

  mSleepTimeout: false

  mDismissKeyguardOnNextActivity: false



  Recent tasks:

  * Recent #0: TaskRecord{42955d30 #2 A com.miui.home U 0}

    numActivities=1 rootWasReset=true userId=0

    affinity=com.miui.home

    intent={act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10600000 cmp=com.miui.home/.launcher.Launcher}

    realActivity=com.miui.home/.launcher.Launcher

    askedCompatMode=false

    lastThumbnail=null lastDescription=null

    lastActiveTime=710219844 (inactive for 1158s)

    ignoreFromRecent=false

  * Recent #1: TaskRecord{46bdfd88 #97 A com.kingroot.kinguser U 0}

    affinity=com.kingroot.kinguser

    intent={flg=0x10800000 cmp=com.kingroot.kinguser/.activitys.SuNotifyActivity}

    realActivity=com.kingroot.kinguser/.activitys.SuNotifyActivity

    askedCompatMode=false

    lastThumbnail=null lastDescription=null

    lastActiveTime=710219836 (inactive for 1158s)

    ignoreFromRecent=false

  * Recent #2: TaskRecord{428d13e8 #96 A com.example.testactivityservice U 0}

    affinity=com.example.testactivityservice

    intent={act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.testactivityservice/.MainActivity}

    realActivity=com.example.testactivityservice/.MainActivity

    askedCompatMode=false

    lastThumbnail=null lastDescription=null

    lastActiveTime=710030246 (inactive for 1348s)

    ignoreFromRecent=false

  * Recent #3: TaskRecord{42a48de0 #95 A com.lancoo.platform U 0}

    numActivities=0 rootWasReset=true userId=0

    affinity=com.lancoo.platform

    intent={act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.lancoo.platform/.activity.SplashActivity}

    realActivity=com.lancoo.platform/.activity.SplashActivity

    askedCompatMode=false

    lastThumbnail=null lastDescription=null

    lastActiveTime=709770805 (inactive for 1607s)

    ignoreFromRecent=false

  * Recent #4: TaskRecord{42831158 #90 A com.android.settings U 0}

    affinity=com.android.settings

    intent={act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10008000 cmp=com.android.settings/.Settings$DevelopmentSettingsActivity}

    origActivity=com.android.settings/.DevelopmentSettings

    realActivity=com.android.settings/.Settings$DevelopmentSettingsActivity

    askedCompatMode=false

    lastThumbnail=null lastDescription=null

    lastActiveTime=709248629 (inactive for 2129s)

    ignoreFromRecent=false

  * Recent #5: TaskRecord{42d06290 #27 A com.example.administrator.myapplication U 0}

    numActivities=0 rootWasReset=true userId=0

    affinity=com.example.administrator.myapplication

    intent={act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.administrator.myapplication/.MainActivity}

    realActivity=com.example.administrator.myapplication/.MainActivity

    askedCompatMode=false

    lastThumbnail=null lastDescription=null

    lastActiveTime=504353483 (inactive for 207024s)

    ignoreFromRecent=false



  mCurTask: 97



是的,看到这么多信息头都是大的。你可以通过下面的命令来搜索你想要的信息:


adb shell dumpsys activity activities | findstr "应用包名" 

但是输入之后,信息还是太多,我只关心的是Activity名!!!

所以我建议下面的这个方法,优点是简单,是单独的一个app,点击开启打印activity的类名信息的服务,不想要是,点击stop service即可。缺点是信息没有上面的全面,但是这就够了。

2)通过服务的方式打印Activity类名

主要用到的是下面的方法。


 ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

        RunningTaskInfo info = manager.getRunningTasks(1).get(0);

    String shortClassName = info.topActivity.getShortClassName();    //类名

        String className = info.topActivity.getClassName();              //完整类名

        String packageName = info.topActivity.getPackageName();          //包名

首先我们得建立这么一个服务,方便自动打印。代码如下:


import android.app.ActivityManager;

import android.app.Service;

import android.content.Context;

import android.content.Intent;

import android.os.Handler;

import android.os.IBinder;

import android.util.Log;



import java.util.List;



/**

 * Created by gongwq on 2017/6/17 0017.

 */



public class MyService extends Service {

    ActivityManager activityManager = null;

    Handler handler = new Handler();

    String activityName = null;

    String activity_last = null;

    Runnable runnable = new Runnable() {



        @Override

        public void run() {

            List<ActivityManager.RunningTaskInfo> runningTaskInfo = activityManager.getRunningTasks(1);

            activityName = (runningTaskInfo.get(0).topActivity).toString();

            if (!(activityName.equals(activity_last))) {

                Log.e("ActivityManager: ", "当前activity是---->" + activityName);

                activity_last = activityName;



            }

            handler.postDelayed(runnable, 10);

        }

### 结尾

**我还总结出了互联网公司Android程序员面试涉及到的绝大部分面试题及答案,并整理做成了文档,以及系统的进阶学习视频资料,免费分享给大家。
(包括Java在Android开发中应用、APP框架知识体系、高级UI、全方位性能调优,NDK开发,音视频技术,人工智能技术,跨平台技术等技术资料),希望能帮助到你面试前的复习,且找到一个好的工作,也节省大家在网上搜索资料的时间来学习。**

### **领取方式:关注+点赞+[点击我的GitHub 免费获取](https://github.com/a120464/Android-P7/blob/master/Android%E5%BC%80%E5%8F%91%E4%B8%8D%E4%BC%9A%E8%BF%99%E4%BA%9B%EF%BC%9F%E5%A6%82%E4%BD%95%E9%9D%A2%E8%AF%95%E6%8B%BF%E9%AB%98%E8%96%AA%EF%BC%81.md)!**

![image](https://p6-xtjj-sign.byteimg.com/tos-cn-i-73owjymdk6/ecb0fcbce8c04c699115b8fd9cf7ec18~tplv-73owjymdk6-jj-mark-v1:0:0:0:0:5o6Y6YeR5oqA5pyv56S-5Yy6IEAg55So5oi3MzIxMjA3NDIwNDUy:q75.awebp?rk3s=f64ab15b&x-expires=1775367047&x-signature=Mfgmrcs2BvJsoT6A9Z8DkTYrYJA%3D)