Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the...

29
Android on Intel Course App Development - Advanced Paul Guermonprez www.Intel-Software-Academic-Program.com [email protected] Intel Software 2013-02-08

Transcript of Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the...

Page 1: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

Android on Intel CourseApp Development - Advanced

Paul Guermonprezwww.Intel-Software-Academic-Program.com

[email protected] Software

2013-02-08

Page 2: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

Persistence

Page 3: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

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.

Page 4: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

Preferences

Here is how to save data.

And here is how to load data.

Page 5: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

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.

Page 6: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

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.

Page 7: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

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/

Page 8: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

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

Page 9: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

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.

Page 10: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

Background process

Page 11: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

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

Page 12: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

AsyncTask

AsyncTask can process data on a different thread than the UI thread.

They can also refresh the UI (ProgressBar for example)

Page 13: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

AsyncTask

Create an inner class that inherits from AsyncTask

Page 14: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

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.

Page 15: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

Sensors

Page 16: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

Sensors

To retrieve a sensor, you need to use the SensorManager

Then you can get the sensor

You also need to implement a SensorEventListener

Page 17: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

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.

Page 18: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

Sensors – full code

Using the sensors is simple with Android*. The Sensor interface allows to manipulate any kind of sensor.

Page 19: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

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.

Page 20: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

NFC

Here is the Java implementation

Page 21: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

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.

Page 22: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

Good to know

Page 23: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

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.

Page 24: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

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.

Page 25: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

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.

Page 26: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

Note : Logs

Then you can add a new channel

Tag name used to filter your logs

Page 27: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

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

Page 28: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background
Page 29: Android on Intel Course App Development - Advanced · AsyncTask In Android* you can also use the Java threads but remember that if you want to update the UI during your background

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/