什么是元数据?以及它在android中的用途是什么 [英] What is metadata ? And what is the use of it in android

查看:188
本文介绍了什么是元数据?以及它在android中的用途是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android的新手,之前从未见过或听说过元数据.但是,我在Google上搜索它并在YouTube上搜索它,它基本上是您的对象的信息.如果我错了,请纠正我.谁能帮助我更好地理解它.

I am new to android and I have not seen or heard about meta data before. However I google it and search about it on YouTube that it is basically a information of your object. Correct me if I am wrong. Can any one help me to understand it in a better way.

1)什么是元数据?

2)为什么在Android中使用它?

2) Why is it used in Android?

如果给出示例说明为什么要在Android中使用元数据,那将是很好的.我在清单的活动元数据标记中看到了它们.

It will be good if explanation is given with example of why metadata is used in Android. I have seen them inside manifest's activity metadata tag.

推荐答案

在Android中,您可以在AndroidManifest.xml

In Android, you can define meta-data information in your AndroidManifest.xml

此处是链接

非常基本的用法

基本上,这是存储可以通过整个项目访问的信息的附加选项.在这种情况下,<meta-data>是在<activity>标签外部和<application>标签内部定义的.

It is basically an additional option to store information that can be accessed through the entire project. In this case, <meta-data> is defined outside <activity> tag and inside <application> tag.

定义:

<manifest>
    <application 
        android:icon="@drawable/icon" 
        android:label="@string/app_name">

        <meta-data android:name="my_test_metagadata" android:value="testValue" />

        <activity 
            android:name=".MainActivity" 
            android:label="@string/app_name">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

    </application>
<manifest>

阅读:

ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
String myApiKey = bundle.getString("my_test_metagadata");

您可以保存booleanintString或浮点数.

You can save a boolean, an int, String or float.

对库或API有用

假设您创建了一个可以供所有人使用的API/LIB.但是,对于特定过程,您需要KEY,并且KEY必须由将使用您的API的开发人员定义. 这样,您将无法预测开发人员将共享哪个密钥.

Let's say that you created an API/LIB which can be used for everyone. However, for a specific procedure, you need a KEY and that KEY must be defined by the developer who will use your API. This way, you can not predict which key the developer will share.

使用<meta-data>,想要使用您的API/LIB的开发人员可以与您共享KEY.这样,您可以将API配置为读取该KEY,如果用户未定义则引发异常.

Using <meta-data>, a developer who wants to use your API/LIB can share the KEY with you. This way, you leave your API configured to read that KEY and raise an exception if the user did not define.

try {
    ApplicationInfo ai = getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
    Bundle bundle = ai.metaData;
    String myApiKey = bundle.getString("my_test_metagadata");
} catch (Exception e) {
    Log.e(TAG, "Dear developer. Don't forget to configure <meta-data android:name=\"my_test_metagadata\" android:value=\"testValue\"/> in your AndroidManifest.xml file.");
}

一个典型的例子是Google Ads(Admob).

One classic example is Google Ads (Admob).

您必须在AndroidManifest中添加以下行:

You must add following line to your AndroidManifest:

<!--This meta-data tag is required to use Google Play Services.  (adMob)-->
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

这将用@integer/google_play_services_version表示的值加载com.google.android.gms.version.然后,很可能Google Play服务(Admob)将读取此元数据,它将能够确定您在构建应用时使用的Google Play服务的版本.

This will load com.google.android.gms.version with value represented by @integer/google_play_services_version. Then, probably, Google Play Services (Admob) will read this metadata and it will be able to determine the version of Google Play Service that you used when you built your app.

另一个示例

<meta-data>的另一种用法是何时使用它们来配置活动.这样,您可以将有关您的活动的有价值的信息传递给android,然后Android可以正确处理您的活动. 在这种情况下,<meta-data>标记将添加到<activity>标记内.

Another usage for <meta-data> is when to use them to configure an Activity. This way you can pass valuable information to android about your activity, and then Android can handle your activity properly. In this case, the <meta-data> tag is added inside the <activity> tag.

我看到的第一个示例是定义搜索活动时的情况.

The first example I see is when you define a Search Activity.

<manifest>
    <application 
        android:icon="@drawable/icon" 
        android:label="@string/app_name">
        <activity 
            android:name=".MainActivity" 
            android:label="@string/app_name">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

        <activity android:name=".SearchableActivity" >
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
            <meta-data android:name="android.app.searchable"
                       android:resource="@xml/searchable"/>
        </activity>
    </application>
<manifest>

然后,要从活动代码中获取元数据,请使用以下方法:

Then, to get the meta-data from the activity tag, use this:

try {
        ActivityInfo ai = getPackageManager().getActivityInfo(this.getComponentName(), PackageManager.GET_META_DATA);
        Bundle bundle = ai.metaData;
        if (bundle != null) {
            String apiKey = bundle.getString("apikey");
            Log.d(this.getClass().getSimpleName(), "apiKey = " + apiKey);
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        Utilities.log(this.getClass().getSimpleName(), "Failed to load meta-data, NameNotFound: " + e.getMessage());
    } catch (NullPointerException e) {
        Log.e(this.getClass().getSimpleName(), "Failed to load meta-data, NullPointer: " + e.getMessage());
    }

这篇关于什么是元数据?以及它在android中的用途是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆