Jump to content
  • Hello visitors, welcome to the Hacker World Forum!

    Red Team 1949  (formerly CHT Attack and Defense Team) In this rapidly changing Internet era, we maintain our original intention and create the best community to jointly exchange network technologies. You can obtain hacker attack and defense skills and knowledge in the forum, or you can join our Telegram communication group to discuss and communicate in real time. All kinds of advertisements are prohibited in the forum. Please register as a registered user to check our usage and privacy policy. Thank you for your cooperation.

    TheHackerWorld Official

活动Activity——为活动补充附加信息——利用元数据传递配置信息

 Share


Recommended Posts

1895087-20220709143308537-1454954451.png

 

 

 

 

元数据是一种描述其他数据的数据,它相当于描述固定活动的参数信息。

 

在activity节点内部添加meta-data标签,通过属性name指定元数据的名称,通过属性value指定元数据的值。

 

 

 

 

示例如下:

 


<activity android:name=".MetaDataActivity">
         <meta-data android:name="weather" android:value="晴天" />
</activity>

 

 

 

 

 

 

 


也可引用strings.xml已定义的字符串资源,举例如下:

 


<activity android:name=".MetaDataActivity">
       <meta-data android:name="weather" android:value="@string/weather_str" />
</activity>

 

 

 

 

 

 

 

 

 

1895087-20220709143441620-968329755.png

 

 

 

 

 

 

1895087-20220709144530297-1717235183.png

 

 

 

 

 

 

 

 

=====================================================================================

 

 

 

 

 

 

 

 

 

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_meta"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="17sp" />

</LinearLayout>

1895087-20220709143628380-878812966.png

 

 

 

 

 

 

 

 

 

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data android:name="weather" android:value="@string/weather_str"/>
        </activity>
    </application>

</manifest>

1895087-20220709144100988-1364941218.png

 

 

 

 

 

 

 

 

 

 

 

 

<resources>
    <string name="app_name">My Application</string>
    <string name="weather_str">晴天</string>
    <string name="first_short">first</string>
    <string name="first_long">启停活动</string>
    <string name="second_short">second</string>
    <string name="second_long">来回跳转</string>
    <string name="third_short">third</string>
    <string name="third_long">登录返回</string>
</resources>

1895087-20220709144130254-1841424671.png

 

 

 

 

 

 

 

 

 

 

 

 

package com.example.myapplication;

import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity
{
    private TextView tv_meta; // 声明一个文本视图对象

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv_meta = findViewById(R.id.tv_meta);           // 从布局文件中获取名叫tv_meta的文本视图
        showMetaData();                        // 显示配置的元数据
    }

    private void showMetaData()                  // 显示配置的元数据
    {
        try
        {
            PackageManager pm = getPackageManager(); // 获取应用包管理器

            ActivityInfo act = pm.getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);  // 从应用包管理器中获取当前的活动信息
            Bundle bundle = act.metaData;                                           // 获取活动附加的元数据信息
            String value = bundle.getString("weather");                  // 从包裹中取出名叫weather的字符串

            tv_meta.setText("来自元数据信息:今天的天气是"+value); // 在文本视图上显示文字
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

1895087-20220709144214565-590603510.png

 

 

 

 

 

 

 

 

 

 

 

 

 

1895087-20220709144242573-1201458961.png

 

 

 

 

 

 

 

 

 

 

 

 

1895087-20220709144449203-1549819981.png

 

 1895087-20220709144511547-424583428.png

 

 1895087-20220709144523620-287722198.png

 

Link to post
Link to comment
Share on other sites

 Share

discussion group

discussion group

    You don't have permission to chat.
    • Recently Browsing   0 members

      • No registered users viewing this page.
    ×
    ×
    • Create New...