Android 查看任务的活动堆栈

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2442713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:52:21  来源:igfitidea点击:

View the Task's activity stack

androidandroid-activity

提问by Mic

I just started developing a simple Android application while I'm still learning the platform.

我刚刚开始开发一个简单的 Android 应用程序,同时我还在学习这个平台。

I'm using Eclipse IDE with the ADT plugin 0.9.6.

我正在使用带有 ADT 插件 0.9.6 的 Eclipse IDE。

I need to know if it's possible to view the Activitystack that is associated with a Task?

我需要知道是否可以查看Activity与任务关联的堆栈?

Is there any way through the DDMS tool or through any other technique?

有没有办法通过 DDMS 工具或任何其他技术?

Essentially what I need is to be able to see the stack activity of a task to make sure that the application behaves as expected.

基本上我需要的是能够查看任务的堆栈活动以确保应用程序按预期运行。

I know that it's possible to control the task behavior in some extent through the use of flags in the Intentobject and through some attributes of the <activity>element.

我知道可以通过在Intent对象中使用标志和<activity>元素的某些属性在一定程度上控制任务行为。

However it would be nice to have a sort of tool - especially in debug mode or so - that would allow developers to see the Activitystack straight forward.

然而,如果有一种工具——特别是在调试模式左右——这将允许开发人员直接查看Activity堆栈会很好。

回答by hackbod

From the command line, you can use: adb shell dumpsys activity

从命令行,您可以使用: adb shell dumpsys activity

This asks the activity manager to print a dump of its current state. The first part of that is the complete activity history, organized by task. There is also a lot of stuff printed after that, so you may need to scroll up a bit to find what you want.

这要求活动管理器打印其当前状态的转储。第一部分是完整的活动历史,按任务组织。之后还有很多东西打印出来,所以你可能需要向上滚动一点才能找到你想要的。

Here is an example of its output (the exact contents varies across platform versions), showing the top task being contacts with two activities, and behind that the launcher with one activity:

这是其输出的示例(确切内容因平台版本而异),显示了与两个活动联系的首要任务,以及在其后的启动器与一个活动:

Activities in Current Activity Manager State:
  * TaskRecord{44d07218 #4 A android.task.contacts}
    clearOnBackground=true numActivities=2 rootWasReset=true
    affinity=android.task.contacts
    intent={act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10600000 cmp=com.android.contacts/.DialtactsActivity bnds=[125,640][235,758]}
    origActivity=com.android.contacts/.DialtactsContactsEntryActivity
    realActivity=com.android.contacts/.DialtactsActivity
    lastActiveTime=288203177 (inactive for 14s)
    * Hist #8: HistoryRecord{44b87a30 com.android.contacts/.ViewContactActivity}
        packageName=com.android.contacts processName=android.process.acore
        launchedFromUid=10004 app=ProcessRecord{44c4f348 1168:android.process.acore/10004}
        Intent { act=android.intent.action.VIEW dat=content://com.android.contacts/contacts/lookup/144i148.144i461a29500afc8eeb/1927 cmp=com.android.contacts/.ViewContactActivity }
        frontOfTask=false task=TaskRecord{44d07218 #4 A android.task.contacts}
        taskAffinity=android.task.contacts
        realActivity=com.android.contacts/.ViewContactActivity
        base=/system/app/Contacts.apk/system/app/Contacts.apk data=/data/data/com.android.contacts
        labelRes=0x7f090012 icon=0x7f02006b theme=0x7f0e0004
        stateNotNeeded=false componentSpecified=false isHomeActivity=false
        configuration={ scale=1.0 imsi=310/4 loc=en_US touch=3 keys=2/1/2 nav=2/2 orien=1 layout=34}
        resultTo=HistoryRecord{44d174d0 com.android.contacts/.DialtactsContactsEntryActivity} resultWho=favorites resultCode=2
        launchFailed=false haveState=false icicle=null
        state=RESUMED stopped=false delayedResume=false finishing=false
        keysPaused=false inHistory=true persistent=false launchMode=0
        fullscreen=true visible=true frozenBeforeDestroy=false thumbnailNeeded=false idle=true
        waitingVisible=false nowVisible=true
    * Hist #7: HistoryRecord{44d174d0 com.android.contacts/.DialtactsContactsEntryActivity}
        packageName=com.android.contacts processName=android.process.acore
        launchedFromUid=10004 app=ProcessRecord{44c4f348 1168:android.process.acore/10004}
        Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.contacts/.DialtactsContactsEntryActivity bnds=[125,640][235,758] }
        frontOfTask=true task=TaskRecord{44d07218 #4 A android.task.contacts}
        taskAffinity=android.task.contacts
        realActivity=com.android.contacts/.DialtactsActivity
        base=/system/app/Contacts.apk/system/app/Contacts.apk data=/data/data/com.android.contacts
        labelRes=0x7f090007 icon=0x7f02006b theme=0x7f0e0000
        stateNotNeeded=false componentSpecified=true isHomeActivity=false
        configuration={ scale=1.0 imsi=310/4 loc=en_US touch=3 keys=2/1/2 nav=2/2 orien=1 layout=34}
        launchFailed=false haveState=true icicle=Bundle[mParcelledData.dataSize=4196]
        state=STOPPED stopped=true delayedResume=false finishing=false
        keysPaused=false inHistory=true persistent=false launchMode=2
        fullscreen=true visible=false frozenBeforeDestroy=false thumbnailNeeded=false idle=true
  * TaskRecord{44c4ee90 #2 A com.android.launcher}
    clearOnBackground=true numActivities=1 rootWasReset=true
    affinity=com.android.launcher
    intent={act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10600000 cmp=com.android.launcher/.Launcher}
    realActivity=com.android.launcher/.Launcher
    lastActiveTime=214734838 (inactive for 73483s)
    * Hist #6: HistoryRecord{44c4d988 com.android.launcher/.Launcher}
        packageName=com.android.launcher processName=android.process.acore
        launchedFromUid=0 app=ProcessRecord{44c4f348 1168:android.process.acore/10004}
        Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000000 cmp=com.android.launcher/.Launcher }
        frontOfTask=true task=TaskRecord{44c4ee90 #2 A com.android.launcher}
        taskAffinity=com.android.launcher
        realActivity=com.android.launcher/.Launcher
        base=/system/app/Launcher.apk/system/app/Launcher.apk data=/data/data/com.android.launcher
        labelRes=0x7f0a0000 icon=0x7f020015 theme=0x103005f
        stateNotNeeded=true componentSpecified=false isHomeActivity=true
        configuration={ scale=1.0 imsi=310/4 loc=en_US touch=3 keys=2/1/2 nav=2/2 orien=1 layout=34}
        launchFailed=false haveState=true icicle=Bundle[mParcelledData.dataSize=5964]
        state=STOPPED stopped=true delayedResume=false finishing=false
        keysPaused=false inHistory=true persistent=false launchMode=2
        fullscreen=true visible=false frozenBeforeDestroy=false thumbnailNeeded=false idle=true

回答by Gerald.K

You can use the following command in your command line to see the tasks and backstacks in the system:

您可以在命令行中使用以下命令查看系统中的任务和后台堆栈:

adb shell dumpsys activity activities | sed -En -e '/Stack #/p' -e '/Running activities/,/Run #0/p'

Or you can try TaskLogger, a simple tool I created which can monitor all activities and tasks in your App, and output them in Logcat in real-time.

或者您可以尝试TaskLogger,这是我创建的一个简单工具,它可以监控您应用程序中的所有活动和任务,并将它们实时输出到 Logcat 中。

回答by tir38

I know this is an old question but, this functionality is now baked into Android Studio:

我知道这是一个老问题,但是,此功能现在已集成到 Android Studio 中:

android studio screenshot

安卓工作室截图

Then in the resulting text file, search for ACTIVITY(all caps):

然后在生成的文本文件中,搜索ACTIVITY(全部大写):

android studio text file screenshot

android studio 文本文件截图

回答by neevek

If you want to inspect the task stack of a specific package, the following command will do:

如果要检查特定包的任务堆栈,可以使用以下命令:

adb shell dumpsys activity activities | grep PACKAGE_NAME | grep Hist

回答by cmcromance

I always check this part of long dump messages..

我总是检查这部分长转储消息..

  Running activities (most recent first):
TaskRecord{4307f828 #56 A com.demo.proj U 0}
  Run #4: ActivityRecord{425a6838 com.demo.proj/com.demo.proj.Activity2}
  Run #3: ActivityRecord{427dc860 com.demo.proj/com.demo.proj.Activity1}
  Run #2: ActivityRecord{420cba18 com.demo.proj/com.demo.proj.MainActivity}
TaskRecord{430341d0 #2 A com.lge.launcher2 U 0}
  Run #1: ActivityRecord{41e0af68 com.lge.launcher2/.Launcher}
TaskRecord{44e26ce0 #18 A com.lge.appbox.client U 0}
  Run #0: ActivityRecord{41e9dbe8 com.lge.appbox.client/.AppBoxClient}

Note: Run #4 is the activity that you see now on the screen. :)

注意:Run #4 是您现在在屏幕上看到的活动。:)

回答by Xazen

You can use the tool hierarchyviewer.bat. It is part of the android SDK. It only works with emulator though. But it is much more confortable and clearer.

您可以使用工具hierarchyviewer.bat。它是 android SDK 的一部分。不过它只适用于模拟器。但它更舒适,更清晰。

Edit: I just found the Hierarchy Viewer within Eclipse! And it works with real devices as well. Just open the perspective Windows->Open Perspective-> Hierarchy View In the list you can see the all the connected devices and emulators and the activity stack. And in addition in the tree view you can see much more information about the view itself.

编辑:我刚刚在 Eclipse 中找到了层次结构查看器!它也适用于真实设备。只需打开透视图 Windows->Open Perspective-> Hierarchy View 在列表中您可以看到所有连接的设备和模拟器以及活动堆栈。此外,在树视图中,您可以看到更多关于视图本身的信息。

Edit: The Hierarchy Viewer will work only with developer devices. Production devices cannot do it for security reasons. For more information please take a look at following answer

编辑:层次结构查看器仅适用于开发人员设备。出于安全原因,生产设备不能这样做。有关更多信息,请查看以下答案

回答by Prasad

For the list of recent tasks

对于最近的任务列表

adb shell dumpsys activity recents

For the list of Services running

对于正在运行的服务列表

adb shell dumpsys activity services

For the list of current Content Providers

有关当前内容提供商的列表

adb shell dumpsys activity providers

For the list of Broadcast state

广播状态列表

adb shell dumpsys activity broadcasts

For the list of Pending Intents

对于待处理的意图列表

adb shell dumpsys activity intents

For the list of permissions

对于权限列表

adb shell dumpsys activity permissions

回答by Pyraman

Solution: 'adb shell dumpsys activity' does not work with TabActivity. When each tab item selected, corresponding activity will be launch. But when use 'adb shell dumpsys activity' It always return 'main' activity:

解决方案:“adb shell dumpsys activity”不适用于 TabActivity。当每个选项卡项被选中时,相应的活动将被启动。但是当使用“adb shell dumpsys activity”时,它总是返回“main”活动:

public class main extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.e("xyz", "start main...............");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, widgets.class);
        spec = tabHost.newTabSpec("Widgets").setIndicator("Widgets", res.getDrawable(R.drawable.tab1)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, layouts.class);
        spec = tabHost.newTabSpec("Layouts").setIndicator("Layouts",res.getDrawable(R.drawable.tab2)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, composite1.class);
        spec = tabHost.newTabSpec("Composite").setIndicator("Composite",res.getDrawable(R.drawable.tab3)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, imageMedia.class);
        spec = tabHost.newTabSpec("Image_Media").setIndicator("Image&Media",res.getDrawable(R.drawable.tab4)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, timeDate.class);
        spec = tabHost.newTabSpec("Time_Date").setIndicator("Time&Date",res.getDrawable(R.drawable.tab5)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, transitions.class);
        spec = tabHost.newTabSpec("Transitions").setIndicator("Transitions",res.getDrawable(R.drawable.tab6)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, advanced.class);
        spec = tabHost.newTabSpec("Advanced").setIndicator("Advanced",res.getDrawable(R.drawable.tab7)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, others.class);
        spec = tabHost.newTabSpec("Others").setIndicator("Others",res.getDrawable(R.drawable.tab8)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, Dynamic.class);
        spec = tabHost.newTabSpec("Dynamic").setIndicator("Dynamic",res.getDrawable(R.drawable.tab2)).setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);

    }
}