activitymanager--gets the top of the stack activity and its owning process, Activitymanager

Source: Internet
Author: User
activitymanager--Gets the top of the stack activity and its owning process, Activitymanager

Gets the stack top activity and its owning 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 ("\ \");
	    	 Top of the stack activity name
	    	 topactivityname=temp[temp.length-1];
	    	 int Index=topactivityclassname.lastindexof (".");
	    	Stack top activity belongs to the process name
	    	 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"/>
Android in the periphery, how to get stack top acitivity context

Do not know whether the following methods meet your problem:
When a method in the perimeter class is called in the top activity of the stack, the context of the top activity is passed through.

How to close the specified activity is obtained first through the method below. your way of doing this is to get activity through a task, only to find the corresponding activity[mw_shl_code=java,true]activitymanager am = (Activitymanager) based on the location of the task. ) This.getsystemservice (Context.activity_service); ComponentName cn = Am.getrunningtasks (2). Get (0). topactivity; [/mw_shl_code]

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.