How to kill an application based on the meta-data content

Source: Internet
Author: User

Application scenarios:
During the specific application process on the all-in-one development machine, it is found that all-in-one resource switching and different applications use different resources. Because you need to switch between different signal source Modes
Use different resources. Therefore, the description "meta-data" is used to kill the specified application.

In androidmanifest. XML, the <meta-data> element can be used as a child element and is contained in <activity>, <Application>, <service>, and <Cycler> elements,
However, the methods for reading different parent elements in an application are also different.

My example is:
<Meta-data Android: Name = "androidtv. meidasource. mode" Android: value = 'digit-mode "/>
At the <Application>... </Application> level

OK. It's easy to implement this function. Just go to the code !!


The following code directly implements a kill method for specifying the meta-data process and a method for running an activity based on the package name.


Package COM. skyworth. DTV; import Java. lang. reflect. method; import Java. util. arraylist; import Java. util. list; import android. app. activitymanager; import android. content. componentname; import android. content. context; import android. content. intent; import android. content. PM. applicationinfo; import android. content. PM. packageinfo; import android. content. PM. packagemanager; import android. content. PM. packagemanager. Namenotfoundexception; import android. content. PM. resolveinfo; import android. util. log; public class dtvapphelper {public static final string meta_source_mode = "androidtv. meidasource. mode "; public static final string meta_source_mode_dtv =" DTV "; static final string tag =" dtvappclosehelper "; static method forcestoppackage = NULL; public static void shutdowndtvapps (context CTX) {packagemanager PM = CTX. g Etpackagemanager (); activitymanager AM = (activitymanager) CTX. getsystemservice (context. activity_service); List <string> List = getrunningdtvapps (AM, PM); For (INT I = 0; I <list. size (); I ++) {log. I (TAG, "will stop package name:" + list. get (I); forcestoppackage (AM, list. get (I) ;}} Private Static void forcestoppackage (activitymanager AM, string pkgname) {try {If (forcestoppackage = NULL) {forcestopp Ackage = aM. getclass (). getdeclaredmethod ("forcestoppackage", String. Class);} If (forcestoppackage! = NULL) {forcestoppackage. setaccessible (true); forcestoppackage. invoke (AM, pkgname) ;}} catch (exception e) {log. E (TAG, "forcestoppackage error:" + E) ;}} Private Static list <string> getrunningdtvapps (activitymanager AM, packagemanager pm) {list <string> plist = new arraylist <string> (); List <activitymanager. runningappprocessinfo> applist = aM. getrunningappprocesses (); For (activitymanager. runningappprocess Info appprocess: applist) {string [] pkgnamelist = appprocess. pkglist; // get the package name list int pid = appprocess. PID; For (INT I = 0; I <pkgnamelist. length; I ++) {string pkgname = pkgnamelist [I]; try {applicationinfo AI = PM. getapplicationinfo (pkgname, packagemanager. get_meta_data); If (AI. metadata! = NULL) {string value = AI. metadata. getstring (meta_source_mode); If (meta_source_mode_dtv.equals (value) {log. I (TAG, "packagename:" + pkgname + "process PID:" + PID); plist. add (pkgname) ;}} catch (namenotfoundexception e) {log. E (TAG, "Get meta data is empty") ;}}return plist;} public static void rundtvapps (context CTX, string packagename) {log. I (TAG, "will start packagename:" + packagename); packageinf O Pi = NULL; try {Pi = CTX. getpackagemanager (). getpackageinfo (packagename, 0); intent resolveintent = new intent (intent. action_main, null); resolveintent. setpackage (Pi. packagename); packagemanager pmanager = CTX. getpackagemanager (); List <resolveinfo> apps = pmanager. queryintentactivities (resolveintent, 0); resolveinfo rI = apps. iterator (). next (); If (Ri! = NULL) {packagename = Ri. activityinfo. packagename; string classname = Ri. activityinfo. name; log. I (TAG, "packagename:" + packagename + "classname:" + classname); intent = new intent (intent. action_main); componentname Cn = new componentname (packagename, classname); intent. setcomponent (CN); intent. setflags (intent. flag_activity_new_task); CTX. startactivity (intent) ;}} catch (exception e) {log. E (TAG, "Start activity failed" + E );}}}


The following describes the reading methods for different parent elements (you can split them as needed to facilitate comparison and writing them into a function ):

Void test (context CTX, string packagename, string classname) {/* Application in application */try {applicationinfo appinfo = CTX. getpackagemanager (). getapplicationinfo (packagename, packagemanager. get_meta_data); string MSG = appinfo. metadata. getstring ("mymsg"); log. I (TAG, "message:" + MSG); // todo} catch (exception e) {log. E (TAG, "getapplicationinfo error:" + E);}/* Application in activity */componentname Cn = new componentname (packagename, classname); try {activityinfo info = CTX. getpackagemanager (). getactivityinfo (CN, packagemanager. get_meta_data); string MSG = info. metadata. getstring ("mymsg"); log. I (TAG, "message:" + MSG); // todo} catch (exception e) {log. E (TAG, "getactivityinfo error:" + E);}/* Application in Service */try {serviceinfo info = CTX. getpackagemanager (). getserviceinfo (CN, packagemanager. get_meta_data); string MSG = info. metadata. getstring ("mymsg"); log. I (TAG, "message:" + MSG); // todo} catch (exception e) {log. E (TAG, "getserviceinfo error:" + E);}/* Application in worker er */try {componentname CRN = new componentname (CTX, classname); activityinfo info = CTX. getpackagemanager (). getreceiverinfo (CRN, packagemanager. get_meta_data); string MSG = info. metadata. getstring ("mymsg"); log. I (TAG, "message:" + MSG); // todo} catch (exception e) {log. E (TAG, "getreceiverinfo error:" + E );}}

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.