Android determines whether the program is active or running in the phone

Source: Internet
Author: User

Shenyangbin in today's project needs to meet a problem like this, in the service need to determine whether the current program is active, in other words, the service in the background run has business needs to detect whether the current program is the program of the service is so good let click Push notification to jump to different pages for different actions. Below paste out I encapsulated the method and share with everyone.

/** * * @ Description: Does the package name program run * @Method_Name: Isrunningapp * @param context context * @param packagename the package name of the program * @re  Turn 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 make a judgment, you must add that permission in the comment, or the program will throw an exception. The general idea is this, get the activity manager, and through the manager to get the currently running 100 programs, and then go to loop the result set, get the package name and the top-level package name of each program in the iterative process, Use the package name and we need to determine the incoming package name parameter for comparison, if found that the program is running, if not found to prove that the program is not running. My existing procedures are implemented in this way, if there are loopholes, please criticize correct. Shenyangbin original.

Android determines whether the program is active or running 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.