Android原生开发笔记 读取元数据

java yekong

添加元数据

AndroidManifest.xml添加元数据

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

读取元数据

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_request = findViewById(R.id.tv_request);
        tv_response = findViewById(R.id.tv_response);
        PackageManager pm = getPackageManager();
        try {
//            从应用包管理器中获取当前的活动信息
            ActivityInfo info = pm.getActivityInfo(getComponentName(), PackageManager.GET_META_DATA);
//            获取活动附加的元数据信息
            Bundle bundle = info.metaData;
            String weather = bundle.getString("weather");
            tv_response.setText(weather);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }
喜欢