据说程序猿最讨厌的两件事:一是给代码写详细的注释;二是阅读别人的代码没注释。
在程序猿的世界里,大部分都是接手的别人的项目,而对于一些项目文档全面,项目代码规范,在阅读代码的时候相对轻松些,不至于当前是哪个Activity都不知道。But,理想是丰满的,现实是骨感的。规范的一点的,会写个BaseActivity,让项目中所有的Activity继承这个BaseActivity,然后再Activity中获取当前的活动名,包括包名。
那本文就说的是如何来打印当前的Activity类名。所以加上上面的继承自BaseAcitivty的方法,还有其他2种方法:
1). 通过Android studio的控制台的adb命令来打印Activity信息
这个主要是利用的adb命令去打印当前的Activity的信息,包括栈名,栈内Activity的信息,类名,所在线程名等信息。优点是信息全面,缺点是每次切换一个界面,就得重新输入一次命令。
命令如下
adb shell dumpsys activity activities
当然在输入这个命令的时候,首先你得找到你的adb所在的目录。
比如下面这个:
cd D:\android-sdk\platform-tools
嫌每次打印麻烦的,可以直接配置到环境变量中去,在path路径后,添加你的platform-tools的路径名。
打印信息如下:
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 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);
}
};
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handler.postDelayed(runnable, 10);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onDestroy() {
handler.removeCallbacks(runnable);
}
}
最后服务不要忘记在AndroidManifest进行注册服务名,此外还要添加一个权限:
<uses-permission android:name="android.permission.GET_TASKS">uses-permission>
主Activity的代码就不贴了,就两个按钮,对应于开启和关闭服务。
点击此处,下载源码
如果对本文内容有什么好的建议,欢迎留言。当然,如果本文有帮助到你,点个赞呗!手写很累的。