Shenyangbin in today's project needs to meet this problem, in the service need to infer whether the current program is active, in other words, the service in the background run the business needs to detect whether the current program is the program of the service is so good so that click Push notification to jump to different pages for different actions.
The following paste out I encapsulated a way to share with everyone.
/** * * @Description : The package name of the program is executed * @Method_Name : Isrunningapp * @param context context * @ Param PackageName Infer The package name of the program * @return The permissions that must be loaded * <uses-permission android:name= "Android.permission.GET_TASKS "/> * @return : Boolean * @Creation Date : 2014-10-31 pm 1:14:15 * @version : v1.00 * @Author : Jiabin * @Update Date : * @Update Author : Jiabin * /public Static Boolean Isrunningapp (context context, String PackageName) {Boolean isapprunning = false; Activitymanager am = (activitymanager) context.getsystemservice (Context.activity_service); list<runningtaskinfo> list = Am.getrunningtasks (+); for (Runningtaskinfo info:list) {if ( Info.topActivity.getPackageName (). Equals (PackageName) && info.baseActivity.getPackageName (). Equals ( PackageName) {isapprunning = true;//find it, Breakbreak;}} return isapprunning;}
The first thing to note is that in order to infer, you must increase the privilege in the gaze. Otherwise the program will throw an exception. The general idea is this, to get the activity of the manager. And through the manager to get the currently executing 100 programs, and then to loop the result set. Gets the package name and the top-most-stack package name for each program's underlying page during the iteration. Using the package name and we need to infer the incoming packet name parameter comparison, assuming found that the program is executing, if not found to prove that the program is not executing. My existing procedures are implemented in this way, if there are loopholes, please criticize correct. Shenyangbin original.
Whether the Android inference program is active or executing in the phone