Saving to SharedPreferences

public static void saveIntPreference(Context context, String key, int value) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    Editor editor = prefs.edit();
    editor.putInt(key, value);
    editor.commit();
}

public static void saveStringPreference(Context context, String key, String value) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    Editor editor = prefs.edit();
    editor.putString(key, value);
    editor.commit();
}
  
public static void saveBooleanPreference(Context context, String key, boolean value) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    Editor editor = prefs.edit();
    editor.putBoolean(key, value);
    editor.commit();
}
Add a comment

Getting and Saving Objects from/to files in app data folder:

Get Object from file

public static Object getObjectFromFile(Context context, String filename) {

    try {      
        FileInputStream fis = context.openFileInput(filename);
        ObjectInputStream ois = new ObjectInputStream(fis);
        
        Object object = ois.readObject();
        ois.close();
  
        return object;

    } catch (FileNotFoundException e) {
        Log.e(LOG_TAG, "getObjectFromFile FileNotFoundException: " + e.getMessage());
        return null;
    } catch (IOException e) {
        Log.e(LOG_TAG, "getObjectFromFile IOException: " + e.getMessage());
        return null;
    } catch (ClassNotFoundException e) {
        Log.e(LOG_TAG, "getObjectFromFile ClassNotFoundException: " + e.getMessage());
        return null;       
    } catch (Exception e) {// Catch exception if any
        Log.e(LOG_TAG, "getBookmarksFromFile Exception: " + e.getMessage());
        return null;
    }
}

 

Save Object to file

public static void saveObjectToFile(Context context, String fileName, Object obj) {
    
    try {
        FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        
        oos.writeObject(obj); 
        oos.close();

    } catch (FileNotFoundException e) {
        Log.e(LOG_TAG, "saveObjectToFile FileNotFoundException: " + e.getMessage());
    } catch (IOException e) {
        Log.e(LOG_TAG, "saveObjectToFile IOException: " + e.getMessage());
    } catch (Exception e) {
        Log.e(LOG_TAG, "saveObjectToFile Exception: " + e.getMessage());
    }
}

 

 

Add a comment

Getting information about your app programatically:

Get App Version Code

public static int getAppVersionCode(Context context) {
    PackageInfo pInfo;
    try {
      pInfo = context.getPackageManager().getPackageInfo(
          context.getPackageName(), 0);
      return pInfo.versionCode;
    } catch (NameNotFoundException e) {
      e.printStackTrace();
      return 0;
    }

 }

Get App Version Name

public static String getAppVersionName(Context context) {
    PackageInfo pInfo;
    try {
      pInfo = context.getPackageManager().getPackageInfo(
          context.getPackageName(), 0);
      return pInfo.versionName;
    } catch (NameNotFoundException e) {
      e.printStackTrace();
      return null;
    }

}

Add a comment

Getting a Object from an included asset file:


private static Object getObjectFromAssetsFile(Context context) {

  try {
    InputStream is = null;
    ObjectInputStream ois = null;

    AssetManager assets = context.getAssets();
    is = assets.open(file_name);
    ois = new ObjectInputStream(is);

    Object o = ois.readObject();
                
    // Check object type and cast as required right here 
    
    return o;

  } catch (FileNotFoundException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  } catch (ClassNotFoundException e) {
    e.printStackTrace();
  } catch (Exception e) {// Catch exception if any
    e.printStackTrace();
  }
  return null;
}

Copying an SQLite Database from the assets folder to the app's data folder:


public static void copyDB(Context context, String assetFileName, String outFileName)
      throws IOException {
  InputStream inputStream = context.getAssets().open(assetFileName);
  OutputStream outputStream = new FileOutputStream(outFileName);

  // ---copy 1K bytes at a time---
  byte[] buffer = new byte[1024];
  int length;
  while ((length = inputStream.read(buffer)) > 0) {
    outputStream.write(buffer, 0, length);
  }
  inputStream.close();

  outputStream.flush();
  outputStream.close();
}
Add a comment
Page 2 of 2