简体   繁体   English

如何为我的 Android 应用程序创建外部共享首选项/配置文件?

[英]How can I create an external shared pref/config file for my Android application?

I am looking for a way to store configuration setting on an external file on the sd card.我正在寻找一种将配置设置存储在 SD 卡上的外部文件中的方法。 The app must look into this external file and then retrieve some kind of settings.该应用程序必须查看此外部文件,然后检索某种设置。 At the moment i am trying to get it to look into a file and get a name.目前我正试图让它查看一个文件并获得一个名称。 I know you can store things in shared preferences but they are internally accessed.我知道您可以将内容存储在共享首选项中,但它们是在内部访问的。

Anybody know an external way?有人知道外部方法吗? Thanks谢谢

The idea is to have a simple text file or xml file on the sd card.这个想法是在 SD 卡上有一个简单的文本文件或 xml 文件。 So when a configuration needs changing it is done thru that file?那么当配置需要更改时,它是通过该文件完成的吗?

EDIT编辑

File sdcard = Environment.getExternalStorageDirectory();
    File file = new File(sdcard,"/Config.txt");
    StringBuilder text = new StringBuilder();
    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line="";
        int c;
        while ((c = br.read()) != -1) {
            line+=(char)c;
            if(String.valueOf(c) == ";" & line =="name;"){
                String name ="";
            }
        }
    }

I have attempted to read in a config.txt file and to separate each value with a ;我试图读取 config.txt 文件并用 ; 分隔每个值。 But i cant seem to understand exactly how i am going to attach what comes after name;但我似乎无法确切理解我将如何附加名称之后的内容; to the variable String name.到变量字符串名称。

the config.txt file has the following: config.txt 文件包含以下内容:

name;fred somethingelse;test名字;弗雷德的东西;测试

The program should know when it has got to name and then set the name variable to fred??程序应该知道什么时候需要命名,然后将 name 变量设置为 fred??

Hi I had same kind of preferences reading in Blackberry with (Comma separated or) semicolon separated values.嗨,我在 Blackberry 中使用(逗号分隔或)分号分隔值有相同的偏好。 When same development came to android the developers (I was not on android at that time.) This is what they have done.当同样的开发来到 android 时,开发人员(当时我不在 android 上。)这就是他们所做的。

We had created a text file like this我们创建了一个这样的文本文件

"name";"value";"data_type"

For example例如

"application_runs";"1";"int"

Or或者

"trial_period_key";"01ab23cd";"string"

The data type is hardcoded so we can detect the datatypes.数据类型是硬编码的,因此我们可以检测数据类型。 For variable names we also had hardcoded list of preferences.对于变量名称,我们也有硬编码的偏好列表。 And values can be parsed accordingly.并且可以相应地解析值。 They have written public readable shared preference based on that txt files, to use default values, they have copied a "default.txt" in assets folder along with a copy in SD card.他们已经根据该 txt 文件编写了公共可读的共享首选项,为了使用默认值,他们在资产文件夹中复制了一个“default.txt”,并在 SD 卡中复制了一个副本。

The drawback of this procedure is这个程序的缺点是

  1. You have to be careful about file name and the data written in that file (this is the reason why we had put a default values file in assets so the app doesn't crash)您必须注意文件名和写入该文件的数据(这就是我们在资产中放置默认值文件以便应用程序不会崩溃的原因)

  2. The text file on SD Card must be readable, you have to program it along with parsing. SD 卡上的文本文件必须是可读的,您必须在解析时对其进行编程。

Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将对象存储到Shared-Pref或Database:Android中? - How can I store object into Shared-Pref or Database : Android? 在Android上以编程方式安装Application而不删除DB和共享的pref文件 - Install Application programmatically on Android without deleting DB and shared pref file Android,如何在应用程序中使用外部APK? - Android, How can I use external APK in my application? android如何用我的应用程序打开外部文件 - android how to open external file with my application 如何在 Android 10 的外部存储(SD 卡)中创建文件? - How can i create file at external storage(sdcard) in Android 10? 如何在共享首选项中存储 Integer Arraylist 并在 Android/Java 中检索相同内容? - How to Store Integer Arraylist in Shared Pref and retrieve the same in Android/Java? 如何使用Eclipse或Ant为Android应用程序创建自己的JAR文件? - How can I create my own JAR file for an Android application using Eclipse or Ant? Android-如何为我的应用程序文件创建“黑名单” - Android - How can i create a “black list” for the files of my application 我如何使用.apk文件到我自己的应用程序android中? - how can i use .apk file into my own application android? 如何在 android 应用程序中显示我的动画 svg 文件? - How can I display my animated svg file in the android application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM