1

アプリケーションに 10 個の URL があり、現在ハードコードされています。これを外部化してファイルに入れ、アプリケーションが読み取れるようにしたいと考えています。

これを行う最善の方法は何ですか?

private String appURL = "http://..."
private String storeURL = "http://..."
private String someURL = "http://..."

注: アプリでこのファイルに書き出すことは決してありません。開発者がファイルを開いて、必要に応じて URL を変更できるようにしてほしい。

4

3 に答える 3

3

Android アプリケーションで文字列を外部化する最も簡単な方法は、文字列リソースを使用することです。フォルダにファイルuris.xmlを作成res/values:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="appUrl">http://...</string>
    <string name="storeUrl">http://...</string>
    <!-- etc /-->
</resources>

その後、次の方法で文字列にアクセスできます。

String appUrl = getString(R.string.appUrl);
String storeUrl = getString(R.string.storeUrl);
//..etc

getString()Contextクラスのメソッドです。Contextまた、リソースへのアクセスを開始する前に、が初期化されるまで待つ必要があることに注意してください。したがって、またはのメソッドでgetString()それより前に呼び出さないでください。onCreate()ActivityApplication

于 2010-11-11T16:46:09.240 に答える
0

You can put the values in a XML "configuration" file, then just read the values from the XML file.

Example of reading an XML file.

Or just use Android's native res folder XML resources files as pointed out by Konstantin Burov.

于 2010-11-11T16:40:37.127 に答える
0

If you want ability to have external flat file to be editable you can simply create a text file on say SD (and have some defaults as static strings or properties). Then your code reads the file (standard Java IO) and pics the changes say in onCreate of your Activity

You can also have such file in /assets folder; see this for example

于 2010-11-11T16:41:08.423 に答える