ActivityManager (1) -- obtains the Activity on the top of the stack and the process to which it belongs. activitymanager

Source: Internet
Author: User

ActivityManager (1) -- obtains the Activity on the top of the stack and the process to which it belongs. activitymanager

// Obtain the stack top Activity and its Process public static String getTopActivityNameAndProcessName (Context context) {String processName = null; String topActivityName = null; ActivityManager activityManager = (ActivityManager) (context. getSystemService (android. content. context. ACTIVITY_SERVICE); List <RunningTaskInfo> runningTaskInfos = activityManager. getRunningTasks (1); if (runningTaskInfos! = Null) {ComponentName f = runningTaskInfos. get (0 ). topActivity; String topActivityClassName = f. getClassName (); String temp [] = topActivityClassName. split ("\\. "); // name of the stack top Activity topActivityName = temp [temp. length-1]; int index = topActivityClassName. lastIndexOf (". "); // name of the process to which the stack top Activity belongs: processName = topActivityClassName. substring (0, index); System. out. println ("----> topActivityName =" + topActivityName + ", processName =" + processName);} return topActivityName + "," + processName ;}

Note permissions:
<Uses-permission android: name = "android. permission. GET_TASKS"/>
How can android get the context of acitivity on the top of the stack?

I wonder if the following method meets your problem:
When a method in the peripheral class is called in the stack top Activity, the context of the stack top Activity is passed over.
 
How can I close a specified activity and obtain it through the following method?

In this way, you obtain the Activity through the task. You can only find the corresponding Activity [mw_shl_code = java, true] ActivityManager am = (ActivityManager) this according to the position of the task. getSystemService (Context. ACTIVITY_SERVICE); ComponentName cn = am. getRunningTasks (2 ). get (0 ). topActivity; [/mw_shl_code]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.