Add Method for PackageManagerService in Android, androidsdkmanager

Source: Internet
Author: User

Add Method for PackageManagerService in Android, androidsdkmanager
Structure of related classes of PackageManagerService


The analysis shows that if you want to add the PackageManagerService method, you need to change 1 in IPackageManager. add a method declaration to the aidl file; 2. Add abstract method descriptions to the PackageManager class; 3. Add methods to ApplicationPackageManager to call the remote PackageManagerService method; 4, add the method implementation in PackageManagerService;
Note: Because the MockPackageManager class also inherits the PackageManager class, you also need to add some implementation code. You need to set the added method to hide, or change the API-List to add the new method.


Demo

Objective: To add the public String getHello () method to PackageManagerService ();

Step 1: Add in IPackageManager. aidlGetHello ()Method Declaration

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 2: Add abstract methods in the PackageManager class

public abstract class PackageManager {    /**     * This exception is thrown when a given package, application, or component     * 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 3: add the specific implementation of the getHello () method to 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";    }}

Step 4: add the getHello () method to ApplicationPackageManager. The specific implementation is to call the getHello () method of the remote PackageManagerService.
final class ApplicationPackageManager extends PackageManager {    private static final String TAG = "ApplicationPackageManager";    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.currentToCanonicalPackageNames(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);}}}


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.