how to pass a data from one android application to another? - android

I'm developing one app, I need to execute some functionality in one app from the result of another app, how to I pass a data from one to another ,Please give me any example programs for this.
Thank you!

Use the Json or XMl. Read the Json from the url that the another app that wants to read and
post data in form of the json or xml to the url
For Api level 14 or getter ShareActionProvider

you can use content provider for this but i dont use this personally. what i do. I make a folder in sd card and the data that i want to share, is place in that folder and then access that data from other application becuse i know all the things that i used in creating that folder or whatever thing like database etc.

The best Option is to Use to shared Preferences ,which will help passing data between apllications and the main note if you uninstall parent application shared preferences will get deleted,,,,
private SharedPreferences mSharedPrefs;
mSharedPrefs = getSharedPreferences("PrefName",
Context.MODE_WORLD_READABLE);
mPrefsEditor = mSharedPrefs.edit();
mPrefsEditor.putString("tagName", "value");
mPrefsEditor.commit();
and in your next application or activity
final ArrayList<HashMap<String,String>> LIST = new ArrayList<HashMap<String,String>>();
Context myContext = createPackageContext("com.example",
Context.MODE_WORLD_WRITEABLE); // where com.example is the owning app containing the preferences
SharedPreferences testPrefs = myContext.getSharedPreferences
("PrefName", Context.MODE_WORLD_READABLE);
Map<String, ?> items = testPrefs .getAll();
for(String s : items.keySet()){
//do somthing like String value= items.get(s).toString());
}

Related

AndroidStudio saving data from bluetooth device

I am working with Android Studio 2.2.3 in order to watch data received from a bluetooth module in my smartphone's screen. I can see these values in my activity but as I am receiving values each 10 seconds (for example) I would like to save all this data in an array, database or anyplace. Once it was saved I would like to build a graphic using this saved data.
Here I have a picture of what I receive:
enter image description here
So now I would like to save those values (53,54,55...) and I don't know how. Is there any way like making an ArrayList or some function to save it? I read about "SharedPreferences" but I think it is not designed for what I am looking for.
Thank you very much!
P.D: I hope I explained okay and sorry for my english.
Shared Preferences could be, it allow to use Sets. You could convert your List into a HashSet or something similar and store it like that. When your read it back, convert it into an ArrayList.
//Retrieve the values
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
Set<String> set = prefs .getStringSet("key", null);
yourListOfStrings.addAll(set);
//Set the values
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
Set<String> set = new HashSet<String>();
set.addAll(yourListOfStrings);
prefs.putStringSet("key", set);
prefs.commit();
Regards,

how to save json array to android internal storage

So I'm working on a project like this http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ which let's me to display my database's values to my android application GUI.
I need to save the database values I need to the android internal storage so I can access it even if my application is not connected to the server. Any help?
Thank you
You can write whole json as string in shared prefernces, and then get it and parse it to display in GUI even when device is offline:
String str = json.toString();
SharedPreferences sharedPref = getSharedPreferences( "appData", Context.MODE_WORLD_WRITEABLE );
SharedPreferences.Editor prefEditor = getSharedPreferences( "appData", Context.MODE_WORLD_WRITEABLE ).edit();
prefEditor.putString( "json", str );
prefEditor.commit();
If you chose local sqlite database as your solution, I wrote a lightweight utility for saving your json into a sqlite.
checkout git repo here. https://github.com/wenchaojiang/JSQL
You only need one line of code.
new JSQLite(yourJsonString, yourDB).persist();
Hope it helps
Consider using a local database to cache the server data locally, that is how most apps does it, here is a good tutorial for sqlite on android Android SQLite
If you use only one or few JSONObjects from the server you can use SharedPreferences, it is much easier and faster to edit/update. example
For more about android storage: Android Storage
I'm working on an application that connects to a php-mysql database in our server, instead of saving the result of the query in a file, you should save it (at least that is what I think so) in an internal Android Database (sqlite). There is a lot of information about databases in Android.
With this example you can see how to easily use sqlite and ContentProviders (a cleaner way of accessing data saved in your database.
In order to save correcty an JSONArray in your database i recommend you to use Jackson libraries in order to create objects from JSON making them easier to be saved.
Finally if the amount of information is relatively small you can use SharedPreferences aswell, this way the data can be accessed faster because it's saved in the mobile memory.
Hope it helps :)
//if you have already the json string
String str = json.toString();
//if you want to convert list to json (with Gson):
//foo - will be your list
String str = new Gson().toJson(foo );
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putString("pref_json_key", str);
prefEditor.apply();
You can try to use with Realm.io A Simple Example like this:
public class City extends RealmObject {
private String city;
private int id;
// getters and setters left out ...
}
// Insert from a string
realm.beginTransaction();
realm.createObjectFromJson(City.class, "{ city: \"Copenhagen\", id: 1 }");
realm.commitTransaction();
Regards!!!

Safe a sorted list in shared preferences

I want to use the shared preferences to save a list of integers. it's simple by using the putStringSet() methode.
final Set<String> set = new TreeSet<String>();
for (final Station station : stations) {
set.add(String.valueOf(station.getId()));
}
editor.putStringSet(USER_STATIONS, set);
but there's a big problem. the list isn't sorted anymore after loading the prefereneces with getStringSet()
Is there an other/better way than save the list as json or as a string with comma seperated values?
I think there is no way to save sorting with preferences (at least SharedPreferences reference is clear about that) , instead you can do it with LiteSQL or by creating a custom file: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal .

Save JSON from url for local development

I have an android app that downloads json data from a url, parses and then displays it.
Unfortunately, I will be away from internet for awhile, but would like to continue to work on the app.
Is there a way that I can save that data for offline use?
Essentially, is there a way I can hard code a JSONObject to use the data found at link
So that I will have it locally?
You have following options:
use RoboSpice library
use android volley library
Both above libraries have caching capability. Conditionally you can invalidate cache to retrieve updated data.
Use shared preferences to store JSON string and update that as and when required.
To store data:
SharedPreferences settings = getApplicationContext().getSharedPreferences("PREF_NAME", MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("strJSON", "" + strJSONfromServer);
editor.commit();
To retrieve data :
SharedPreferences settings = getApplicationContext().getSharedPreferences("PREF_NAME", MODE_PRIVATE);
String strData = settings.getString("strJSON", "");
To clear data :
SharedPreferences settings = getApplicationContext().getSharedPreferences("strJSON",MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.remove("strJSON");
editor.commit();
if you have http server installed in your machine , you can save that json data as text file, and access it from the device with the url: "http://10.0.2.2:PORT_NUMBER"
another solution, no need for http server, you can save the data as text file and access it from your device, as explained in this answer:
Reading a simple text file
Edit:
simpler way would be just save the data in a String:
String stringJson = "{\"Baseball\":[{\"Opponent\": ... "
(be aware you will have to escape the characters with \")
to parse the json data, all you need to do is
JSONObject jsonObject = new JSONObject(stringJson);
Another option in to store your json in file under the assets directory. Then you will be able to acces it like any other file in your application:
http://developer.android.com/reference/android/content/res/AssetManager.html

Android storing string array

I've been looking for over the past day and a half at several websites about how to store strings/string arrays/etc. and I can't seem to figure it out. I'm not sure if I'm just not quite comprehending how to implement data storage..or what. But here is my problem, simply put, I have two activity pages (we'll name 'A,B' respectively) . All I want to do is get a string from a text view in Activity B, store it in an array, and then have it accessed by clicking a button in Activity A.
I know it is simple, but I hit a block for some reason... I am trying to use SharedPreferences but how would I obtain the string from Activity B, store it in a global array, and let it be accessed by a different activity (Activity A) ?
Just store it into shared preferences (usually in onPause()):
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putString(GAME_STATE, writer.toString());
editor.commit();
in one activity and load in another (usually in onResume()):
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
String jsonState = preferences.getString(GAME_STATE, null);
And nothing prevents you from using public static variables
You can't store an array directly in shared preferences, but you can store a set.
See putStringSet and getStringSet. You can add all the items from your array to a LinkedHashSet (as long as they are unique) if you wish to store them in SharedPreferences. See Konstantin's answer for the general idea on how to use SharedPreferences.
Yes you can obtain and store value in global array but you will loss everything once you close the application. So its better you create a table and store/get values from the table whenever you required.
Here is an example for creating SQLite example.

Categories

Resources