为Android中PackageManagerService添加方法
PackageManagerService相关类结构
分析
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(); }
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(); }
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"; } }
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); } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。