如何在android中修改我的自定义属性文件

May*_*ank 1 android properties file apk

我正在使用属性文件来获取我从我的android调用的各种web服务的URL.我想提供一个配置选项,以便可以修改Web服务的IP地址.如何进行 ?

我在src文件夹中有一个资源文件夹,其中包含以下值:update = http://10.52.165.226:50000/android/rest/get/updateSfc ShopOrder = http://10.52.165.226:50000/android/rest/getShopOrder/bySite?网站=

我正在使用资源包在android中使用这个值.?

我正在考虑读取文件并替换IP地址的所有occrence.如何rad属性文件并在android中编辑它

Jav*_*tor 8

这是一个完整的解决方案,您可以在项目中使用.properties文件.

1在android项目的assets文件夹中创建一个名为app.properties的文件

2编辑文件并使用您要使用的属性进行写入,例如

test=success 
Run Code Online (Sandbox Code Playgroud)

并保存文件

3在您的活动类中编写此方法

  private Properties loadPropties() throws IOException {
  String[] fileList = { "app.properties" };
  Properties prop = new Properties();
  for (int i = fileList.length - 1; i >= 0; i--) {
     String file = fileList[i];
     try {
        InputStream fileStream = getAssets().open(file);
        prop.load(fileStream);
        fileStream.close();
     } catch (FileNotFoundException e) {
        Log.d(TAG, "Ignoring missing property file " + file);
     }
  }
  return prop;
  }
Run Code Online (Sandbox Code Playgroud)

4在OnCreate方法中写一些这样的东西

     Properties prop = null; 
     try {
        prop = loadPropties();
     } catch (IOException e) {
        Log.e(TAG, "Exception", e);
     }
     Toast.makeText(getApplicationContext(), "Result " + prop.getProperty("test"),
                    Toast.LENGTH_LONG).show();
Run Code Online (Sandbox Code Playgroud)

5添加必要的进口

希望这可以帮助 :)