Adding methods to Packagemanagerservice in Android

Source: Internet
Author: User

Packagemanagerservice Related class structure


Analysis can be seen if you want to add a method for Packagemanagerservice to make the following changes 1, Add the declaration of the method in the Ipackagemanager.aidl file, 2, add the abstract method description in the Packagemanager class, 3, add the method in Applicationpackagemanager, implement to call the remote Packagemanagerserv Ice method; 4, the concrete realization of the method is added in Packagemanagerservice;
Note: Since the Mockpackagemanager class also inherits the Packagemanager class, it is also necessary to add some implementation code, the added method to be set to hide, or to change the Api-list add New method


Demo

Goal: Add method public String Gethello () in Packagemanagerservice;

Step one: Add Gethello () method declaration in Ipackagemanager.aidl

interface    Ipackagemanager {packageinfo getpackageinfo (String packagename, int flags);    int Getpackageuid (String packagename);        Int[] Getpackagegids (String packagename);    String[] Currenttocanonicalpackagenames (in string[] names);    String[] Canonicaltocurrentpackagenames (in string[] names);        Permissioninfo Getpermissioninfo (String name, int flags);        List<permissioninfo> Querypermissionsbygroup (String Group, int flags);        Permissiongroupinfo Getpermissiongroupinfo (String name, int flags);        list<permissiongroupinfo> getallpermissiongroups (int flags);    ApplicationInfo Getapplicationinfo (String packagename, int flags);    Activityinfo Getactivityinfo (in componentname className, int flags);    Activityinfo Getreceiverinfo (in componentname className, int flags);    .      .    .    .    .    . String Gethello ();} 


Step Two: Add abstract method description in Packagemanager class

Public abstract class Packagemanager {    /**     * This exception are thrown when a given package, application, or Compon ENT     * Name can not be found.     *    /public static class Namenotfoundexception extends Androidexception {public        namenotfoundexception () {        }        Public namenotfoundexception (String name) {            super (name);        }    }    ...    /**     * @hide * *    /public abstract String Gethello ();}

Step Three: the implementation of adding Gethello () method in Packagemanagerservice
public class Packagemanagerservice extends Ipackagemanager.stub {    static final String TAG = "Packagemanager";    Static Final Boolean debug_settings = false;    Private static Final Boolean debug_preferred = false;    Static Final Boolean debug_upgrade = false;    Private static Final Boolean debug_install = false;    Private static Final Boolean debug_remove = false;    Private static Final Boolean debug_show_info = false;    Private static Final Boolean debug_package_info = false;    Public String Gethello () {        return ' Hello,pms ';    }}

Fourth Step: Add the Gethello () method to the Applicationpackagemanager to implement the Gethello () method that calls the remote Packagemanagerservice
Final class Applicationpackagemanager extends Packagemanager {private static final String TAG = "Applicationpackageman    Ager ";    Private Final Static Boolean DEBUG = false;    Private Final Static Boolean debug_icons = false;        @Override public PackageInfo getpackageinfo (String packagename, int flags) throws Namenotfoundexception {            try {packageinfo pi = mpm.getpackageinfo (PackageName, flags);            if (pi! = null) {return pi;        }} catch (RemoteException e) {throw new RuntimeException ("Package manager has died", e);    } throw new Namenotfoundexception (PackageName); } @Override Public string[] Currenttocanonicalpackagenames (string[] names) {try {return Mpm.curr        Enttocanonicalpackagenames (names);        } catch (RemoteException e) {throw new RuntimeException ("Package manager has died", e);    }    }    .    .    .    .    .    .        .    /** * @hide */@Overridepublic String Gethello () {//TODO auto-generated method Stubtry {return Mpm.isthirdparty (name) ;} catch (RemoteException e) {throw new RuntimeException ("Package manager has died", e);}}


Adding methods to Packagemanagerservice in Android

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.