Android Gets meta-data in the manifest file, and hit the value is null solution

Copyright: come, do not argue with bald? Burnished burnished kind Oh, Great God reprint please indicate the source https://blog.csdn.net/yue_233/article/details/91453451

Android Gets meta-data in the manifest file, and hit the value is null solution

What 1.meta-data that? How to obtain the meta-data?

In AndroidManifest.xml element as a child element, is wrapped in activity, application, service, or receiver element, a different parent element, is read in the application method is different.

In the activity in:

        ActivityInfo info = null;
        try {
            info = this.getPackageManager().getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        info.metaData.getString("meta_name");

In the application of:

ApplicationInfo appInfo = null;
        try {
            appInfo = this.getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        appInfo.metaData.getString("meta_name");

In the service:

ComponentName cn = new ComponentName(this, XXXXService.class);
        ServiceInfo info = null;
        try {
            info = this.getPackageManager().getServiceInfo(cn, PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        info.metaData.getString("meta_name");

In the receiver in:

ComponentName cn = new ComponentName(getApplicationContext(), XXXXXReceiver.class);
        ActivityInfo info = null;
        try {
            info = getApplicationContext().getPackageManager().getReceiverInfo(cn, PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        info.metaData.getString("meta_name");

2. The problem encountered: Get to the null value

Before acquisition has been key value in application, but it has been acquired are null, then the NPC God said: read the value of the string to use info.metaData.getInt, try a little, bend the Buddha, to get success, if is a value type, when the acquired value, may be employed:

 info.metaData.getInt("meta_name"));
 替代info.metaData.getString("meta_name");

I lack experience, please point out the problem, learn from each other ... bald together

Guess you like

Origin blog.csdn.net/yue_233/article/details/91453451