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

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

问题描述

我是 android 新手,我以前从未见过或听说过元数据.但是我谷歌它并在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

这里是 DOCK 链接

非常基本的用法

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

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 或 float.

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.

使用,想要使用你的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" />

这将加载 com.google.android.gms.version,其值由 @integer/google_play_services_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.

另一个例子

的另一个用途是何时使用它们来配置 Activity.通过这种方式,您可以将有关您的活动的有价值的信息传递给 android,然后 Android 可以正确处理您的活动.在这种情况下, 标签被添加到 标签内.

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天全站免登陆