Android中PackageManager学习笔记(3)-PackageParser

之前写过2篇关于PackageManager的学习笔记


Android中PackageManager学习笔记(1)-ApplicationInfo

Android中PackageManager学习笔记(2)-PackageInfo


今天继续学习PackageManager中PackageParser这个类。


PackageParser


该类存在于android.content.pm包中,官方文档并没有放出该类,所以只能查看源码


packageparser源码网站


PackageParser为PackageManager专门解析android配置档文件AndroidManifest.xml所建立的。里面的一系列方法都是通过XMLPullParser工具解析该xml文件。由于方法太多,主要对里面的内部类做一些简单的介绍,起一个抛砖的作用。首先进官网查看一下我们的主配置文件含有哪些节点?然后讲解就会很容易了。



public final static class  Activity extends Component<ActivityIntentInfo>


Activity节点信息


public final static class ActivityIntentInfo extends IntentInfo


activity中intent节点信息


public static class Component<II extends IntentInfo>


保存所有组件,activity,provider,receiver都属于组件


public final static class  Instrumentation extends Component 


保存instrumentation节点信息



public static class IntentInfo extends IntentFilter


保存Intent的信息


public static class  NewPermissionInfo


新权限?信息。sdk版本号,文件版本号,权限名称


public final static class Package


保存了该包中所有文件节点信息


 public final static class Package {

        public String packageName;

        // For now we only support one application per package.
        public final ApplicationInfo applicationInfo = new ApplicationInfo();

        public final ArrayList<Permission> permissions = new ArrayList<Permission>(0);
        public final ArrayList<PermissionGroup> permissionGroups = new ArrayList<PermissionGroup>(0);
        public final ArrayList<Activity> activities = new ArrayList<Activity>(0);
        public final ArrayList<Activity> receivers = new ArrayList<Activity>(0);
        public final ArrayList<Provider> providers = new ArrayList<Provider>(0);
        public final ArrayList<Service> services = new ArrayList<Service>(0);
        public final ArrayList<Instrumentation> instrumentation = new ArrayList<Instrumentation>(0);

        public final ArrayList<String> requestedPermissions = new ArrayList<String>();
        public final ArrayList<Boolean> requestedPermissionsRequired = new ArrayList<Boolean>();

        public ArrayList<String> protectedBroadcasts;

        public ArrayList<String> libraryNames = null;
        public ArrayList<String> usesLibraries = null;
        public ArrayList<String> usesOptionalLibraries = null;
        public String[] usesLibraryFiles = null;

        public ArrayList<ActivityIntentInfo> preferredActivityFilters = null;

        public ArrayList<String> mOriginalPackages = null;
        public String mRealPackage = null;
        public ArrayList<String> mAdoptPermissions = null;
        
        // We store the application meta-data independently to avoid multiple unwanted references
        public Bundle mAppMetaData = null;

        // If this is a 3rd party app, this is the path of the zip file.
        public String mPath;

        // The version code declared for this package.
        public int mVersionCode;
        
        // The version name declared for this package.
        public String mVersionName;
        
        // The shared user id that this package wants to use.
        public String mSharedUserId;

        // The shared user label that this package wants to use.
        public int mSharedUserLabel;

        // Signatures that were read from the package.
        public Signature mSignatures[];

        // For use by package manager service for quick lookup of
        // preferred up order.
        public int mPreferredOrder = 0;

        // For use by the package manager to keep track of the path to the
        // file an app came from.
        public String mScanPath;
        
        // For use by package manager to keep track of where it has done dexopt.
        public boolean mDidDexOpt;
        
        // // User set enabled state.
        // public int mSetEnabled = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
        //
        // // Whether the package has been stopped.
        // public boolean mSetStopped = false;

        // Additional data supplied by callers.
        public Object mExtras;

        // Whether an operation is currently pending on this package
        public boolean mOperationPending;

        /*
         *  Applications hardware preferences
         */
        public final ArrayList<ConfigurationInfo> configPreferences =
                new ArrayList<ConfigurationInfo>();

        /*
         *  Applications requested features
         */
        public ArrayList<FeatureInfo> reqFeatures = null;

        public int installLocation;

        /* An app that's required for all users and cannot be uninstalled for a user */
        public boolean mRequiredForAllUsers;

        /* The restricted account authenticator type that is used by this application */
        public String mRestrictedAccountType;

        /* The required account type without which this application will not function */
        public String mRequiredAccountType;

        /**
         * Digest suitable for comparing whether this package's manifest is the
         * same as another.
         */
        public ManifestDigest manifestDigest;

        /**
         * Data used to feed the KeySetManager
         */
        public Set<PublicKey> mSigningKeys;
        public Map<String, Set<PublicKey>> mKeySetMapping;

        public Package(String _name) {
            packageName = _name;
            applicationInfo.packageName = _name;
            applicationInfo.uid = -1;
        }

        public void setPackageName(String newName) {
            packageName = newName;
            applicationInfo.packageName = newName;
            for (int i=permissions.size()-1; i>=0; i--) {
                permissions.get(i).setPackageName(newName);
            }
            for (int i=permissionGroups.size()-1; i>=0; i--) {
                permissionGroups.get(i).setPackageName(newName);
            }
            for (int i=activities.size()-1; i>=0; i--) {
                activities.get(i).setPackageName(newName);
            }
            for (int i=receivers.size()-1; i>=0; i--) {
                receivers.get(i).setPackageName(newName);
            }
            for (int i=providers.size()-1; i>=0; i--) {
                providers.get(i).setPackageName(newName);
            }
            for (int i=services.size()-1; i>=0; i--) {
                services.get(i).setPackageName(newName);
            }
            for (int i=instrumentation.size()-1; i>=0; i--) {
                instrumentation.get(i).setPackageName(newName);
            }
        }

        public boolean hasComponentClassName(String name) {
            for (int i=activities.size()-1; i>=0; i--) {
                if (name.equals(activities.get(i).className)) {
                    return true;
                }
            }
            for (int i=receivers.size()-1; i>=0; i--) {
                if (name.equals(receivers.get(i).className)) {
                    return true;
                }
            }
            for (int i=providers.size()-1; i>=0; i--) {
                if (name.equals(providers.get(i).className)) {
                    return true;
                }
            }
            for (int i=services.size()-1; i>=0; i--) {
                if (name.equals(services.get(i).className)) {
                    return true;
                }
            }
            for (int i=instrumentation.size()-1; i>=0; i--) {
                if (name.equals(instrumentation.get(i).className)) {
                    return true;
                }
            }
            return false;
        }

        public String toString() {
            return "Package{"
                + Integer.toHexString(System.identityHashCode(this))
                + " " + packageName + "}";
        }
    }


public static class PackageLite 

<manifest>节点信息

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
              package="string"
              android:sharedUserId="string"
              android:sharedUserLabel="string resource" 
              android:versionCode="integer"
              android:versionName="string"
              android:installLocation=["auto" | "internalOnly" | "preferExternal"] >
        . . .
    </manifest>


static class ParseComponentArgs extends ParsePackageItemArgs

ParsePackageItemArgs


自己创造,保存一些ParsePackage自身用于唯一标识的信息


public final static class Permission extends Component<IntentInfo>


保存permission节点信息


public final static class PermissionGroup extends Component<IntentInfo> 


保存<permission-group>节点信息


public final static class  Provider extends Component<ProviderIntentInfo>


保存provider节点信息


public static final class ProviderIntentInfo extends IntentInfo


保存provider中的intent节点信息


public final static class  Service extends Component<ServiceIntentInfo>


保存service标签的信息


public final static class  ServiceIntentInfo extends IntentInfo


保存Service标签中的intent属性节点信息


public static class SplitPermissionInfo


将权限信息保存在String数组中


  public static class SplitPermissionInfo {
        public final String rootPerm;
        public final String[] newPerms;
        public final int targetSdk;

        public SplitPermissionInfo(String rootPerm, String[] newPerms, int targetSdk) {
            this.rootPerm = rootPerm;
            this.newPerms = newPerms;
            this.targetSdk = targetSdk;
        }
    }


                                

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android系统PackageManager(包管理器)是一个系统级别的服务,它是一个应用程序包的管理器,可以用来安装、卸载、查询应用程序包信息等操作。PackageManager API是Android系统非常重要的一个API,它可以让开发者获取应用程序包的信息,比如版本号、权限等等。 常用的PackageManager命令有: 1. 查询已安装应用的信息 ``` pm list packages // 列出所有已安装的应用的包名 pm list packages -s // 列出所有已安装的系统应用的包名 pm list packages -3 // 列出所有已安装的第三方应用的包名 pm list packages -f // 列出所有已安装应用的APK路径 pm list packages -d // 只列出已禁用的应用 pm list packages -e // 只列出系统已启用的应用 ``` 2. 安装应用 ``` pm install /path/to/app.apk // 安装应用 pm install -r /path/to/app.apk // 重新安装应用,保留应用数据 pm install -t /path/to/app.apk // 允许安装覆盖其他应用 pm install -i <installer_package_name> /path/to/app.apk // 指定应用安装包的安装来源 ``` 3. 卸载应用 ``` pm uninstall com.package.name // 卸载应用 pm uninstall -k com.package.name // 卸载应用,保留应用数据和缓存 ``` 4. 查询应用信息 ``` pm dump com.package.name // 输出应用信息 pm path com.package.name // 输出应用APK路径 pm list features // 列出系统支持的特性 pm list instrumentation // 列出所有已安装的Instrumentation ``` 5. 其他命令 ``` pm enable com.package.name // 启用应用 pm disable com.package.name // 禁用应用 pm clear com.package.name // 清除应用数据和缓存 ``` 以上是一些常用的PackageManager命令,使用时需要注意权限问题,部分命令需要root权限才能执行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值