简体   繁体   中英

Settings file in android application

At first, I need an advice how to store private settings in Android application. This settings will contain server/login/password/clientId which are required by application to log into REST API. Settings could be changed in the future, so I need solution which can "easly" update it on all devices - app will be used on +700 client phones. Phones does not have SD Card.

My concept is to store this settings in a hashed text file. I can send prepared setting file to Client, then Client copy it to device memory via USB to the given location - new folder in the main storage where Alarms, DCIM etc are.

Is this a good solution ?

Example methods to check storage - on my test phone both returning false:

    public boolean isExternalStorageWritable() {
      String state = Environment.getExternalStorageState();
      if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
      }
      return false;
    }

    public boolean isExternalStorageReadable() {
      String state = Environment.getExternalStorageState();
      if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        return true;
      }
      return false;
    }

How to save file in android external memory (not SD Card) ?

Great doc here : https://developer.android.com/training/basics/data-storage/files.html

Please read it to understand that the external storage does not mean SD card for android. And that even a phone without sd card will have a external storage

You need this permission in your manifest :

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

(for me the read permission works as well...)

Then for newer android versions you need to ask the permission

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (PackageManager.PERMISSION_GRANTED != this.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, READ_EXTERNAL_STORAGE_RIGHT_REQUEST_CODE);
        }
    }

Then I use getExternalFilesDir to get my application directory to store some files. https://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)

But since you don't want removeable storage you may be better off with getFilesDir https ://developer.android.com/reference/android/content/Context.html#getFilesDir()

PS: I just noticed the doc says you don't need any permission to access your application directory since KitKat. May take a look at that later...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM