安卓:从AndroidManifest.xml中传递参数到活动 [英] Android: Passing parameters into Activity from AndroidManifest.xml

查看:823
本文介绍了安卓:从AndroidManifest.xml中传递参数到活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道,如果有可能传递参数到从AndroidManifest.xml中的活动文件?我想使用相同的活性在几个应用程序,但有空调的一种方式,活动的基于应用程序的外观

Does anyone know if it's possible to pass parameters into an Activity from the AndroidManifest.xml file? I want to use the same activity in a couple of apps, but have a way of conditioning the appearance of that activity based on the app.

例如。我想写点东西像(在AndroidManifest.xml中)

E.g. I'd like to write something like (in AndroidManifest.xml)

<activity android:name=".MyActivity"
              android:label="@string/app_name"
              android:screenOrientation="portrait">

        <extradata>
              <item name="foo" value="bar"/>
        </extradata>
</activity>

..并能够读出条的值在活动的的onCreate。这是不是可以与数据属性,一个意图过滤器来完成?

.. and be able to read out the value of bar in the onCreate of the activity. Is this something that can be done with the data attribute in an intent filter?

谢谢

推荐答案

其实,有办法做到这一点。检查这篇文章:

Actually, there is way to do just that. Check this article :

<一个href="http://blog.iangclifton.com/2010/10/08/using-meta-data-in-an-androidmanifest/">http://blog.iangclifton.com/2010/10/08/using-meta-data-in-an-androidmanifest/

总之你可以用得到标记的内容如下:

In short you can get the content of the tag using this :

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

元数据标签的定义是这样的:

Metadata tag is defined as this :

<meta-data android:name="my_api_key" android:value="mykey123" />

这是可以访问的数据值,但如果你想指定的东西是相关的活动,这是更清洁的方式。

It is possible to access data in values, but if you want to specify something that is related to the activity, this is much cleaner approach.

希望它帮助。

这篇关于安卓:从AndroidManifest.xml中传递参数到活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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