Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the...
Transcript of Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the...
Android on Intel CourseApp Development - Advanced
Paul Guermonprezwww.Intel-Software-Academic-Program.com
[email protected] Software
2013-02-08
Persistence
Preferences
Shared preference can be used to remember basic information (string, integer, boolean, etc).
It is easy to use.
To save data, you need to retrieve the SharedPreference object editor.
Preferences
Here is how to save data.
And here is how to load data.
Instance State
When your application is put onPause, the method onSaveInstanceState is called.
If you don't want to loose data, you can save them.
SQLite
SQLite is a light database that you can use to persist your application's data
You can use SQLite in Java and the library is already integrated in Android framework.
JSON
JSON is an open standard that can be used to send/receive information.
Google-gson is a free library that allows to serialize/deserialize java objects
Download google gson on https://code.google.com/p/google-gson/
JSON
Unzip the file anywhere you want but put the jar file in the libs folder of your Android* project
Add the jar file to your classpath
Here is a snippet of Java code
JSON
And the result is a JSON string.
You can use this string in a HTTP request for example.
In most of the applications, using the shared preference system is enough and you don't need to use a SQL database.
Background process
AsyncTask
You can't block the UI thread more than few seconds or the application will crash
But you sometimes need more than few seconds to process your data
You can create a thread but...
… you can't refresh UI from the non UI thread
AsyncTask
AsyncTask can process data on a different thread than the UI thread.
They can also refresh the UI (ProgressBar for example)
AsyncTask
Create an inner class that inherits from AsyncTask
AsyncTask
In Android* you can also use the Java threads but remember that if you want to update the UI during your background process, the
AsyncTask offers an elegant solution.
Sensors
Sensors
To retrieve a sensor, you need to use the SensorManager
Then you can get the sensor
You also need to implement a SensorEventListener
Sensors
Then you must register your sensor. A good location is in the OnResume function.
Don't forget to unregister your listener in the OnPause function.
Sensors – full code
Using the sensors is simple with Android*. The Sensor interface allows to manipulate any kind of sensor.
NFC
When your phone detect a NFC Tag, Android* tries to launch an application that can read it.
You can declare specific intent-filter in the manifest to inform Android that your application can read NDEF tags
In this example, our application will be launched when the tag contains ahttp link.
NFC
Here is the Java implementation
NFC
In the Java side, you can check that your application can really handle the intent.
Then you can retrieve the TAG
We saw how to use NFC with Android* Intentsbut it is also possible to read NFC tags in a
running application.
Good to know
Resources
It is often a good practice to separate implementations details like text strings or graphical element sizes from the code or user interface definitions. To do so, use resources.
From the code :Resources res = getResources();CharSequence resMsg = res.getText(R.string.message);
It is also possible to reference resources from other resources, or access system resources.
Localization
It's easy to make your app multilingual if you start using resources from the beginning.
You already have a res/values/strings.xml file.All you need to do is createres/values-XX/strings.xml files where XX is the language-region code.
Note : Logs
Using the Android* log system is really important when you develop your application.
You can configure Eclipse* to display only a subset of the logs.
First, you need to display the Logcat (Windows → Show View → Other...)
In the search tool, type logcat.
Note : Logs
Then you can add a new channel
Tag name used to filter your logs
Note : Logs
In your java code
And here is the result
You can select the log priority– Log.d: For debug messages
– Log.e: For error messages
– Log.i: For information messages
– Log.v: For verbose messages
– Log.w: For warning messages
License Creative Commons - By 3.0
You are free:
to Share — to copy, distribute and transmit the work
to Remix — to adapt the work
to make commercial use of the work
Under the following conditions:
Attribution — You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).
With the understanding that:
Waiver — Any of the above conditions can be waived if you get permission from the copyright holder.
Public Domain — Where the work or any of its elements is in the public domain under applicable law, that status is in no way affected by the license.
Other Rights — In no way are any of the following rights affected by the license:
Your fair dealing or fair use rights, or other applicable copyright exceptions and limitations; The author's moral rights; Rights other persons may have either in the work itself or in how the work is used, such as publicity or privacy rights.
Notice — For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page.
http://creativecommons.org/licenses/by/3.0/