开发者

how to read a particular value in android manifest file or xml in android?

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 re开发者_如何学Cad it directly to a variable? or i have to use xml parser? any idea?


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜