I want to store some basic data like players_name,levels_completed,fastest_speed,fastest_time and bring it up each time the player starts a silly little game I am making... which is the perffered method to do this?
Sharedprefs or internal storage?
I am at http://developer.android.com/guide/topics/data/data-storage.html
and have just gotten confused as to which to use as both look good and easy enough to do.
Advise?
Thanks!
This is pretty much taken from one of the Facebook sdks examples (it allows you to save the FB session so the user doesn't have to login each time)... I'll modify it a bit for you though
public class SessionStore {
private static final String PLAYER_NAME = "player_name";
private static final String LEVELS_COMPLETED = "levels_completed";
private static final String HIGH_SCORE = "high_score";
private static final String KEY = "player_session";
int highScore;
int levelsCompleted;
String pName;
public static boolean saveSession(Context context, String player_name, int levels_completed, int high_score) {
Editor editor =
context.getSharedPreferences(KEY + player_name, Context.MODE_PRIVATE).edit();
editor.putString(PLAYER_NAME,player_name);
editor.putInt(LEVELS_COMPLETED, levels_completed);
editor.putInt(HIGH_SCORE,high_score);
return editor.commit();
}
public static void restoreSession(Context context, String player_name) {
SharedPreferences savedSession =
context.getSharedPreferences(KEY + player_name, Context.MODE_PRIVATE);
highScore = savedSession.getInt(HIGH_SCORE,0);
levelsCompleted = savedSession.getInt(LEVELS_COMPLETED,0);
pName = savedSession.getString(PLAYER_NAME,"NO_NAME!");
}
public String getName()
{
return pName;
}
}
I think you get the basic idea...
some points: I use "KEY + player_name" in case different players play on the same phone (if it was static you would overwrite the data of one player with anothers data).
Also, when you do pName = savedSession.getString(PLAYER_NAME,"NO_NAME!"); if nothing exists in the shared preferences it defaults to the value "NO_NAME!" and likewise for the getInts (which in this case I have them default to 0)
in the program you would do SessionStore.saveSession("Alex",50000,50000); to save the session, etc. Hope this gives a good gist of how to use it... Also keep in mind I'm an android newb - this works great for me but I'm no expert :D
If it is game data that is static you could use shared preferences. If it is dynamic data like player high scores etc I would use sqlite database. I actually think that a database is a simpler option as creating read / write buffers can be a bit tricky on internal storage.
public void StoreTimeAppend(String MY_DATA, File file) {
try {
FileOutputStream fos = openFileOutput(String.valueOf(file), MODE_APPEND);
Writer out = new OutputStreamWriter(fos, "UTF8");
out.write(MY_DATA + '\n');
out.close();
fos.close();
} catch (IOException e) {
Tip.setText("ISSUE");
}
}
Related
I have a wearable app. The app after it finishes has data like time/date, UUID, Geo location, parameters selected displayed in front of me like a Data Report or Log in several TextViews underneath each other. Like a list. I want this data to be transferred from my wearable device to my android phone.
Now I have to ask does the WearOS app the pairs the phone with the watch enables such a thing? Like can the data be sent through it? OR what exactly can I do? I read about Sync data items with the Data Layer API in the documentation, but I'm not sure if the code snippets provided would help achieve what I want.
public class MainActivity extends Activity {
private static final String COUNT_KEY = "com.example.key.count";
private DataClient dataClient;
private int count = 0;
...
// Create a data map and put data in it
private void increaseCounter() {
PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/count");
putDataMapReq.getDataMap().putInt(COUNT_KEY, count++);
PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
Task<DataItem> putDataTask = dataClient.putDataItem(putDataReq);
}
...
}
The data I display in the textviews are called through methods that I call things like: getLocation, getUUID, getDateTime, getSelections, etc... when I click a button I call them in the setOnClickListener. I want this data in the TextViews to be placed in a file or something like that and send them over to the mobile phone from the watch when they're generated.
private void getDateTime()
{
SimpleDateFormat sdf_date = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat sdf_time = new SimpleDateFormat("HH:mm:ss z");
String currentDate= sdf_date.format(new Date());
String currentTime= sdf_time.format(new Date());
textView_date_time.setText("Date: "+currentDate+"\n"+"Time: "+currentTime);
}
#SuppressLint("SetTextI18n")
private void getUUID()
{
// Retrieving the value using its keys the file name
// must be same in both saving and retrieving the data
#SuppressLint("WrongConstant") SharedPreferences sh = getSharedPreferences("UUID_File", MODE_APPEND);
// The value will be default as empty string because for
// the very first time when the app is opened, there is nothing to show
String theUUID = sh.getString(PREF_UNIQUE_ID, uniqueID);
// We can then use the data
textView_UUID.setText("UUID: "+theUUID);
}
#SuppressLint("SetTextI18n")
private void getSelections()
{
textView_data_selected.setText("Tool No.: "+c.getToolNo()+
"\nTool Size: " +c.getToolSizeStr()+
"\nFrom Mode: " +c.getCurrentModeStr()+
"\nGoto Mode: " +c.getModeStr()+
"\nMethod: " +c.getMethodStr()+
"\nBit Duration: " +c.getBitDuration()+
"\nUpper bound" +c.getUpStageValue()+
"\nLower bound: "+c.getDownStageValue());
}
The above are examples of the methods I use to get the data. then I call them here:
gps_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= 26) {
getLocation();
getDateTime();
getUUID();
getSelections();
}
else
{
//ActivityCompat.requestPermissions(get_location.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
Toast.makeText(get_location.this,"Build SDK too low",Toast.LENGTH_SHORT);
}
}
});
Now how do I take all this and send it over from my device to the the phone?
Note: The data report I want to send as a file, I want it done subtly like something done in the background. I don't know what else to do or where to look.
You have two options if you want to use the Data Layer, one is to use the MessageClient API to bundle your data up in a message and send it directly to the handheld. The easiest here would be to create an arbitrary JSONObject and serialize your data as a JSON string you can stuff into a message. For example:
try {
final JSONObject object = new JSONObject();
object.put("heart_rate", (int) event.values[0]);
object.put("timestamp", Instant.now().toString());
new MessageSender("/MessageChannel", object.toString(), getApplicationContext()).start();
} catch (JSONException e) {
Log.e(TAG, "Failed to create JSON object");
}
In my case, I do this in my onSensorChanged implementation, but you can insert this wherever you are updating your text.
MessageSender is just a threaded wrapper around the MessageClient:
import java.util.List;
class MessageSender extends Thread {
private static final String TAG = "MessageSender";
String path;
String message;
Context context;
MessageSender(String path, String message, Context context) {
this.path = path;
this.message = message;
this.context = context;
}
public void run() {
try {
Task<List<Node>> nodeListTask = Wearable.getNodeClient(context.getApplicationContext()).getConnectedNodes();
List<Node> nodes = Tasks.await(nodeListTask);
byte[] payload = message.getBytes();
for (Node node : nodes) {
String nodeId = node.getId();
Task<Integer> sendMessageTask = Wearable.getMessageClient(context).sendMessage(nodeId, this.path, payload);
try {
Tasks.await(sendMessageTask);
} catch (Exception exception) {
// TODO: Implement exception handling
Log.e(TAG, "Exception thrown");
}
}
} catch (Exception exception) {
Log.e(TAG, exception.getMessage());
}
}
}
The other option is to create a nested hierarchy of data items in the Data Layer and implement DataClient.OnDataChangedListener on both sides, such that changes that are written in on one side are automatically synchronized with the other. You can find a good walkthrough on how to do that here.
For your specific case, just packing it in a JSON object would probably be the simplest. The writing out to your preferred file format you can then implement on the handheld side without needing to involve the wear side.
Problem
I'm saving a byte[] in my shared preferences. I am able to close the app and reopen it with the value persisting in the Shared Preferences. When running the app and closing it via the 'Task Manager' or 'Force Close', the Shared Preference value for the byte[] is cleared. I don't understand this because other values persist fine.
This lead me to believe that this was due to some gson or Shared Preference issue with the byte[] so I converted it to a String and I still have the issue.
Edit:
I save the data during normal activity usage... after onCreate(), for example. It's not during onPuse() or onDestroy() I forgot to mention this. It would make sense if I did call it here and one or both of those weren't being called on the 'Force Close' scenario.
Shared Preference Code
Slightly modified to remove app specific implementation and data
private static final String SHARED_PREFERENCES_FILE_NAME = "SharedPreferenceName";
public static void setSharedPreferenceObjectBase64Encoded(Context context, String key, Object object) throws Exception {
// Need an editor to update shared preference values
SharedPreferences.Editor editor = context.getSharedPreferences(SHARED_PREFERENCES_FILE_NAME, Context.MODE_PRIVATE).edit();
Gson gson = new GsonBuilder().serializeNulls().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").create();
String encodedKey = Base64.encodeToString(key.getBytes(), 0, key.getBytes().length, Base64.DEFAULT);
String stringObject = gson.toJson(object);
String encodedObject = Base64.encodeToString(stringObject.getBytes(), 0, stringObject.getBytes().length, Base64.DEFAULT);
editor.putString(encodedKey, encodedObject);
editor.apply();
}
public static Object getSharedPreferenceObjectBase64Encoded(Context context, String key, Class<? extends Serializable> objectClass) throws Exception {
// Need an editor to update shared preference values
SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_FILE_NAME, Context.MODE_PRIVATE);
Gson gson = new GsonBuilder().serializeNulls().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").create();
String encodedKey = Base64.encodeToString(key.getBytes(), 0, key.getBytes().length, Base64.DEFAULT);
String encodedObject = prefs.getString(encodedKey, null);
if (encodedObject == null) {
throw new NullPointerException("encodedObject is null : No shared preference exists for key.");
}
String decodedObject = new String(Base64.decode(encodedObject, Base64.DEFAULT));
if(decodedObject == null){
throw new NullPointerException("decodedObject is null : Json decoding error.");
}
Object resultObject = gson.fromJson(decodedObject, objectClass);
if (resultObject == null) {
throw new NullPointerException("resultObject is null : Json decoding error.");
}
return resultObject;
}
`byte[]` Code
public static final String VALUE_KEY= "value.key";
public static void saveTheValue(Context context, byte[] encryptedPin) {
try {
USharedPreferenceManager.setSharedPreferenceObjectBase64Encoded(context, VALUE_KEY, encryptedPin);
} catch (Exception e) {
}
}
public static byte[] getTheValue(Context context) {
try {
return (byte[]) USharedPreferenceManager.getSharedPreferenceObjectBase64Encoded(context, VALUE_KEY, byte[].class);
} catch (Exception e) {
return null;
}
}
Any input would be greatly appreciated..
Sadly, I haven't been able to make any progress here. Any thoughts?
Update:
As per Super-califragilistic recommendation, I iterated through the key/value pairs in the SharedPreferences immediately before retrieving the value. I was Base64 encoding my key and value values; in order to read the key to ensure the value was in the SharedPreferences I had to use the keys in plain text. This solved the issue for me as the byte[] value was now being retrieved properly.
This seems strange to me but I can use it as a solution. I would still like to Base64 encode the keys, but it's not incredibly important.
Current Solution:
Removed the Base64 encoding of the SharedPreference Key for storage and retrieval and the value is now persisting in all cases.
This line of code String encodedObject = prefs.getString(encodedKey, null); means if the key does not exist it should return null, hence your key that you are checking does not exist.
To validate if your key/value exist use this code
for(Entry<String, ?> en : sharedPref.getAll().entrySet()){
en.getKey();//key
en.getValue();//value
}
you could stop that from happening override onPause() in the Activity or Fragment and call saveTheValue(Context context, byte[] encryptedPin) if you detect you need to save data or have already tried saving data eg.
private boolean forceSaveInOnPause= false;//global variable
//in your saving method
....//after you save
forceSaveInOnPause = true;
//in your onPause of Activity
if(forceSaveInOnPause){
//re-save
forceSaveInOnPause = false;
but since you already have a solution scratch all that :)
Try once with editor.commit() instead of apply(), see if that works
I think using Base64.NO_PADDING instead of Base64.DEFAULT both while reading and writing may solve the problem.
I have an issues reading from sharedpreferences in android. The data is stored in the preference file, and i ma sure of this because i used logcat to log the data when i read it back IMMEDIATELY as it was stored as well as i used a toast message to display the data after i read it back from preferences. However when i try to read the data again from a new activity it returns the default of an empty sting. When i exist the app and run it again it read the data that was previously stored. Here is my code for when i initially save the data, it also contains the comments i used to verify that the data stored was read back:
public void downloadMatchesAsync() {
brawtaAPIAdapter.runGetMatches(new Callback<JSONKeys>() {
#Override
public void success(JSONKeys jsonKeys, Response response) {
Log.d(ApplicationConstants.TAG, "blarg");
Success = jsonKeys.isSuccess();
message = jsonKeys.getErrorMessage().toString();
if(!message.isEmpty()){
Toast.makeText(MyApplication.getAppContext(), message, Toast.LENGTH_SHORT).show();
}
Gson gson = new Gson();
String jsonObject = gson.toJson(jsonKeys); //converts java object into json string'
Preferences.saveToPreferences(activity, ApplicationConstants.match_data, jsonObject);
//data =Preferences.readFromPreferences(activity, ApplicationConstants.match_data, "no data");
//Toast.makeText(MyApplication.getAppContext(), "In networkOperation" + data, Toast.LENGTH_LONG).show();
}
#Override
public void failure(RetrofitError error) {
}
}); // Call Async API
//return data;
}
In a different activity i try to read the data previously written like so:
jsonString = Preferences.readFromPreferences(MatchSelect.this, ApplicationConstants.match_data, "");
but the code only returns the data stored if i exist the app and run it again.
this is my preference class:
public class Preferences {
private static final String PrefName = "net.brawtasports.brawtasportsgps";
public static void saveToPreferences(Context context, String key, String value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(PrefName, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.apply();
}
public static String readFromPreferences(Context context, String key, String defaultValue) {
SharedPreferences sharedPreferences = context.getSharedPreferences(PrefName, Context.MODE_PRIVATE);
return sharedPreferences.getString(key, defaultValue);
}
}
What is the issue?
apply() is an asynchronous call, so the data doesn't have to be stored immediately. Try calling .commit() instead.
!! Please Help, my users are loosing data due to this, and I don't know what to do.
This only happens on Ice Cream Sandwich, works fine on Jelly Bean, Hoenycomb, what causes this?
Since one of my strings is just a number, would it be better to save it as a float or int??
Weird thing is that, it works fine on my acer a500 tablet, with android 4.0.3, but it doesn't work on the emulator with 4.0.3, I've gotten a complaint from a user with a galaxy s3 on 4.0.4, for him is didn't work either..
I'm saving two strings to Shared Preferences like this:
Thanks
private static final String PREFS_NAME = "com.MY_PACKAGE.MY_APP.MY_WIDGET_PROVIDER_CLASS";
private static final String PREF_PREFIX_KEY = "prefix_";
private static final String PREF_SIZE_PREFIX_KEY = "prefix_";
...
static void saveTitlePref(Context context, int mAppWidgetId, String text) {
SharedPreferences.Editor editor = context.getSharedPreferences(PREFS_NAME, 0).edit();
editor.putString(PREF_PREFIX_KEY + mAppWidgetId, text);
editor.commit();
}
static void saveSizePref(Context context, int mAppWidgetId, String size) {
SharedPreferences.Editor editor = context.getSharedPreferences(PREFS_NAME, 0).edit();
editor.putString(PREF_SIZE_PREFIX_KEY, size);
editor.commit();
}
static String loadTitlePref(Context context, int mAppWidgetId) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
String prefix = prefs.getString(PREF_PREFIX_KEY + mAppWidgetId, null);
// If there is no preference saved, get the default from a resource
if (prefix != null) {
return prefix;
} else {
return context.getString(R.string.appwidget_prefix_default);
}
}
static String loadSizePref(Context context, int mAppWidgetId) {
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
String sizeprefix = prefs.getString(PREF_SIZE_PREFIX_KEY, null);
// If there is no preference saved, get the default from a resource
if (sizeprefix != null) {
return sizeprefix;
} else {
return "24";
}
}
Strings xml
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="appwidget_prefix_default"></string>
<string name="appwidget_text_format"><xliff:g id="prefix">%1$s</xliff:g></string>
<string name="appwidget_size_format"><xliff:g id="sizeprefix">%s</xliff:g></string>
</resources>
First of all you should not believe on what is happening on emulator as it works weirdly some times.
Your code seems fine.
I would say that you should check the same issue on other devices and check if things work fine, because it is quite possible that there is some other issue that one of your user experienced, which ultimately deduced that things are not working fine with preferences
I have myself experienced some cases where user says the app does not work for them even though I have tested it on the same device they were using.
Your size preference isn't unique per widget is this intentional?
Your doing
editor.putString(PREF_SIZE_PREFIX_KEY, size);
instead of
editor.putString(PREF_SIZE_PREFIX_KEY + appWidgetId, size);
Same with retrieval:
String sizeprefix = prefs.getString(PREF_SIZE_PREFIX_KEY, null);
Therefore every new call to this will override it for other widgets.
Is it possible to save SharedPreferences files into custom dir? Let's say into /data/data/package.name/my_prefs/.
Or is it possible to retrieve the directory SharedPreferences are saved by default to?
P.S. Hardcoding the path /data/data/package.name/shared_prefs/ is not the solution.
Or is it possible to retrieve the directory SharedPreferences are
saved by default to?
Yes.
This is basically the dataDir /shared_prefs which you can get from the ApplicationInfo object (which in turn you can get from the PackageManager). (Also, it might be the same as the "getFilesDir" dir you can get easily from Context itself? Not sure, didn't check that.)
From the source, starting with Context.getSharedPreferences (ContextImpl source):
public SharedPreferences getSharedPreferences(String name, int mode) {
SharedPreferencesImpl sp;
File prefsFile;
boolean needInitialLoad = false;
synchronized (sSharedPrefs) {
sp = sSharedPrefs.get(name);
if (sp != null && !sp.hasFileChangedUnexpectedly()) {
return sp;
}
prefsFile = getSharedPrefsFile(name);
...
public File getSharedPrefsFile(String name) {
return makeFilename(getPreferencesDir(), name + ".xml");
}
private File getPreferencesDir() {
synchronized (mSync) {
if (mPreferencesDir == null) {
mPreferencesDir = new File(getDataDirFile(), "shared_prefs");
}
return mPreferencesDir;
}
}
private File getDataDirFile() {
if (mPackageInfo != null) {
return mPackageInfo.getDataDirFile();
}
throw new RuntimeException("Not supported in system context");
}
And "mPackageInfo" is an instance of LoadedApk:
public File getDataDirFile() {
return mDataDirFile;
}
mDataDirFile = mDataDir != null ? new File(mDataDir) : null;
mDataDir = aInfo.dataDir;
And that brings us back to ApplicationInfo.
I'd say if you don't want to rely on the convention /data/data/<package_name>/shared_prefs then it should be safe to get the "dataDir" and rely on "shared_prefs" from there?
Or is it possible to retrieve the directory SharedPreferences are
saved by default to?
See this answer to know how get path in safe way: https://stackoverflow.com/a/33849650/1504248