List of all of the activities in our application that are running on the device

夙愿已清 提交于 2019-12-30 02:08:25

问题


How to get the list of all activities in our application that are running on the device.

For example: pdf generation and email activities included.

I can check for activities with the code like:

ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);

     // get the info from the currently running task

     List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);

     Log.d("topActivity", "CURRENT Activity ::"

             + taskInfo.get(0).topActivity.getClassName());

     ComponentName componentInfo = taskInfo.get(0).topActivity;

   componentInfo.getPackageName();

This will however give information about the running activity which is the top activity.

My idea is to whitelist the activities running so that my third party application can run it while on the device. So I basically need all of the running activities in the application.


回答1:


I've found AppLifecycleHandler as documented in this post to be a great solution for knowing what's happening. I use it primarily to just keep count and clean up when the user is done using the app, but you can implement it with an array in which you store all running activities so you can always call in to find out what's running.




回答2:


This is the way to get all the running activities in the application -

try {
    ActivityInfo[] list = getPackageManager().getPackageInfo(getPackageName(),PackageManager.GET_ACTIVITIES).activities;

        for(int i = 0;i< list.length;i++)
        {
            System.out.println("List of running activities"+list[i].name);

        } 
    }

    catch (NameNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }


来源:https://stackoverflow.com/questions/17486672/list-of-all-of-the-activities-in-our-application-that-are-running-on-the-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!