如何读取android清单文件或android中的xml中的特定值?

发布于 2024-10-03 06:11:47 字数 120 浏览 4 评论 0原文

让我们假设我在应用程序中使用服务或数据库路径,因此每当我更改位置路径时,我不想在编码中进行大量编辑。那么是否可以将值存储在某个 xmlfile 或清单文件中并将其直接读取到变量中?或者我必须使用 xml 解析器?有什么想法吗?

lets us assume that i am using service or database path in my application,so whenever i change the location path, i don't want to edit in all the ton times in coding. so is it possible to store the value in some xmlfile or manifest file and read it directly to a variable? or i have to use xml parser? any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终难愈 2024-10-10 06:11:47

您有多个选项:

1-您可以创建自己的文件(txt 或 xml),这取决于您,当您创建活动或服务时,您可以使用以下方式读取该文件:

FileInputStream fileInputStream = openFileInput("test.txt");
DataInputStream in = new DataInputStream(fileInputStream);
BufferedReader br = new BufferedReader(new InputStreamReader(in) ,  1<<10<<3);
String strLine = null;
while ((strLine = br.readLine()) != null)   {
// strLine has one line from you file, it will have fixed format, for example: 
//  path=xyz
}

2-您可以将其添加为字符串资源文件夹中的值(eclipse 默认为您创建 res 文件夹,因此您可以使用以下路径找到名为 strings.xml 的文件:
res->values->strings.xml
因此,在此文件中,您可以添加字符串,例如:

<string name="path">hello</string>

因此,当您想从活动或服务中读取它时,您可以说:

getResources().getText(R.string.path);

或者您可以使用包含您使用的所有常量的静态接口;因为您无法修改运行时的值,但是否能够在运行时修改该值;您需要存储在 SharedPreference 中,或者创建您自己的数据库并存储它。

You have more than an option:

1- you can create your own file (txt or xml) this is up to you, and when you create your activity or service you can read the file by using:

FileInputStream fileInputStream = openFileInput("test.txt");
DataInputStream in = new DataInputStream(fileInputStream);
BufferedReader br = new BufferedReader(new InputStreamReader(in) ,  1<<10<<3);
String strLine = null;
while ((strLine = br.readLine()) != null)   {
// strLine has one line from you file, it will have fixed format, for example: 
//  path=xyz
}

2-You can add it as String value in your resource folder(eclipse creates res folder for you by default so you can find a file called strings.xml using the following path:
res->values->strings.xml
so in this file you can add your string like :

<string name="path">hello</string>

so when you want to read it from your activity or service, you can say:

getResources().getText(R.string.path);

or you can use a static interface which has all the constants you use;since you can't modify the value at run time, but if there is an ability to modify this value at runtime; you need to store either in SharedPreference, or create your own DB and store it.

随心而道 2024-10-10 06:11:47

我建议使用简单的方法:

创建一个包含此类常量的类常量(例如绝对文件路径)。将其导入到需要此类常量的所有类中。常量中文件路径的更改会导致引用它的所有位置发生更改。

例如,在 Android 代码中,不应直接使用常量来启动显示显示设置的意图

"android.settings.DISPLAY_SETTINGS"

,而应使用 android.provider.Settings 常量

android.provider.Settings.ACTION_DISPLAY_SETTINGS

I would suggest simple approach:

Create one class Constants that contains such constants (e.g. absolute file paths). Import it in all classes where you need such constants. Change of file path in Constants causes change in all places where it was referenced.

For example, in Android code you should not use constant directly to start intent for displaying Display Settings

"android.settings.DISPLAY_SETTINGS"

but rather android.provider.Settings constant

android.provider.Settings.ACTION_DISPLAY_SETTINGS
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文