我是靠谱客的博主 淡定小海豚,最近开发中收集的这篇文章主要介绍为Android中PackageManagerService添加方法PackageManagerService相关类结构分析Demo,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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

第五步:在MockPackageManager中增加getHello方法,文件在framework/base/test-runner/src
public class MockPackageManager extends PackageManager {

    @Override
    public PackageInfo getPackageInfo(String packageName, int flags)
    throws NameNotFoundException {
        throw new UnsupportedOperationException();
    }

    @Override
    public String[] currentToCanonicalPackageNames(String[] names) {
        throw new UnsupportedOperationException();
    }public class MockPackageManager extends PackageManager {

    @Override
    public PackageInfo getPackageInfo(String packageName, int flags)
    throws NameNotFoundException {
        throw new UnsupportedOperationException();
    }

    @Override
    public String[] currentToCanonicalPackageNames(String[] names) {
        throw new UnsupportedOperationException();
    }
    .
    .
    .
    .
    .
    .
 /**
     * @hide
     */
	@Override
	public boolean getHello() {
		// TODO Auto-generated method stub
		throw new UnsupportedOperationException();
	}
}



最后

以上就是淡定小海豚为你收集整理的为Android中PackageManagerService添加方法PackageManagerService相关类结构分析Demo的全部内容,希望文章能够帮你解决为Android中PackageManagerService添加方法PackageManagerService相关类结构分析Demo所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(34)

评论列表共有 0 条评论

立即
投稿
返回
顶部