Whether the Android inference program is active or executing in the phone

Source: Internet
Author: User

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

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.