为Android中PackageManagerService添加方法

PackageManagerService相关类结构


技术分享


分析

从上图可以看出若想为PackageManagerService添加方法需要进行以下改动
1,在IPackageManager.aidl文件中增加方法的声明;
2,在PackageManager类中增加抽象方法说明;
3,在ApplicationPackageManager中增加方法,具体实现为调用远程PackageManagerService的方法;
4,在PackageManagerService中增加方法的具体实现;
注:由于MockPackageManager类也继承了PackageManager类,故也需要加入一些实现的代码;增加的方法要设置为hide,或者更改API-List加入新增加的方法


Demo

目标:PackageManagerService加入方法public String getHello();

第一步:在IPackageManager.aidl增加getHello()方法声明

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();
}


第二步:PackageManager类中增加抽象方法说明

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();
}

第三步:在PackageManagerService中增加getHello()方法的具体实现
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";
    }
}

第四步:在ApplicationPackageManager中增加getHello()方法,具体实现为调用远程PackageManagerService的getHello()方法
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
     */
    @Override
	public String getHello() {
		// TODO Auto-generated method stub
		try {
			return mPM.isThirdParty(name);
		} catch (RemoteException e) {
			throw new RuntimeException("Package manager has died", e);
		}
	}
}


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。