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]