-2

In my application there are some configuration data, such as ipaddress, portno, title and etc. I want to keep these data in phone and out of the application in config.And I will read from and write to this file.How can I do that?If not what can you suggest me to do that?

IPAddress=127.0.0.1 Port=1234

MUMBUÇOĞLU
  • 251
  • 1
  • 8
  • 24
  • May I ask why it is important to keep this data separated from your application ? – OYRM Jan 28 '16 at 15:32
  • Because IP address to connect my webservice can change.Is it possible or not.Is what I ask absurt? – MUMBUÇOĞLU Jan 28 '16 at 15:36
  • Of course, yes, it's possible, but that doesn't require external storage. Do you mean to ask if it is possible to include storage which is not compiled, but changeable ? – OYRM Jan 28 '16 at 15:46
  • does my answer, re: shared preferences address the problem, or do you really need external file storage ? – OYRM Jan 28 '16 at 16:55

1 Answers1

1

Yes, you can read and write files, and in reading from a file, you can set a variable. I'll assume that you're capable of handling all of the gui related tasks involved with prompting users for input, as that is really a different question.

My explanation is largely from the Android Developer Docs, where you can read more fully at this link Using External Files

First, we'll assume that your user wants to load the settings file from the Downloads directory. You'll need permissions to use the external storage, add the following to your manifest, between the <manifest> tags:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Before you try to access the file, you'll want to check the state of your system's external storage, to see if you can read from it

String state = Environment.getExternalStorageState();
boolean isReadable =  (Environment.MEDIA_MOUNTED.equals(state) ||
    Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))

If the external storage is readable, you can get a hold of an external file, we're going to call it "settings" within the Downloads directory :

private static final SETTINGS_FILENAME = "settings";
...
public File getSettingsFile() {
    // Get the directory for the user's public downloads directory.
    File file = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), SETTINGS_FILENAME);
    if (!file.exists()) {
        Log.e(LOG_TAG, "The settings file does not exist");
    }
    return file;
}

Now, you have access to the File that you emailed to your client. You will use the standard interface, which I linked to, to read the file and internalize its settings.

Again, this is all just sourced from the developer docs, with slight modification. I hope that that helps you.

OYRM
  • 1,395
  • 10
  • 29
  • Thanks for your answer,I know the shared preferences.But While the application starts for the first time,is shared prefs empty?How can I fill it.The service application(WCF service) which android application can talk can be on different customers. – MUMBUÇOĞLU Jan 29 '16 at 06:32
  • @kkk Can you create a settings activity where your customer can configure the server and port? – OYRM Jan 29 '16 at 12:36
  • I think it won't meet my request.Because the application have a login mechanism.Username and password confirmation which is in related wcf service is required. – MUMBUÇOĞLU Jan 29 '16 at 12:42
  • @kkk Until you describe your requirements fully, I won't be able to help you. Please describe what you need in clear terms in your original post. Otherwise, the goal post will keep moving as I attempt to answer. – OYRM Jan 29 '16 at 15:07
  • @kkk do you need to have the port and server set when you build the apk ? – OYRM Jan 29 '16 at 16:30
  • I don't need to have port and server information when I build the apk.If I set this information before building apk, many different apk occurs for multiple customer.I want to choose this way(using config file) to avoid to create multiple instance.I hope you know what I mean – MUMBUÇOĞLU Jan 30 '16 at 10:09
  • Yes, but, before the customer downloads the apk, you want to set these variables for them. Correct? – OYRM Jan 30 '16 at 12:37
  • Yes I understand what you said,but I dont upload the application to google play.This will be a commercial application.I will send the apk and config file to customers in specially – MUMBUÇOĞLU Jan 30 '16 at 13:04
  • @kkk I've updated the answer to deal with files. Let me know what you think – OYRM Feb 02 '16 at 16:31
  • I think this will probably help me.Thank you very much for your answer and support.I will accept your answer. – MUMBUÇOĞLU Feb 03 '16 at 07:12