and-lec06

flosssnailsΚινητά – Ασύρματες Τεχνολογίες

10 Δεκ 2013 (πριν από 3 χρόνια και 10 μήνες)

108 εμφανίσεις

lec 06

AsyncTask

Local Services

IntentService

Broadcast Receivers











In this example, MyTask extends AsyncTask<String, Integer, Bitmap>


Params:

1/ (String) passed into doInBackground(String... strUrls)


2/ (Integer) The value passed into onProgressUpdate(Integer...
intUpdates)


3/ (Bitmap) returned from doInBackground() and used as input to
onPostExecute(Bitmap bmp)














AsyncTask

An Activity can start a Service via either the startService() method or
stop the service via the stopService() method.


A Service will not automatically run in its own thread, though it can.
You can specify that your Service runs in a separate process via the
android:process=":process_description" attribute.

The colon means it's private to the app, if the colon is missing then it's
global to everything.


<service

android:name=".WordService"

android:process=":my_process"

android:icon="@drawable/icon"

android:label="@string/service_name"

>

</service>


Without the process attribute, the service runs in the main thread of
the hosting process. Therefore you should run performance intensive
tasks in the background. You can use IntentService for this.






Service

Services


>A first
-
class component in Android that must be declared in the the
manifest

>Has no graphical user
-
interface

>Used for long
-
running background tasks; typically networking, I/O,
interaction with content providers, multimedia


>There are two classes of Services;




Service

Started (local)

Bound (remote)

Used within the
same app

Across apps


onStartCommand() returns a flag which tells the OS that the service is
either sticky or not_sticky.


Both codes are only relevant when the phone runs out of memory and
kills the service before it finishes executing.


START_STICKY tells the OS to recreate the service after it has
enough memory and call onStartCommand() again with a null intent.


START_NOT_STICKY tells the OS to not bother recreating the service
again. There is also a third code


START_REDELIVER_INTENT that tells the OS to recreate the service
AND redeliver the same intent to onStartCommand().












Service


An
IntentService
:


1/ running in a separate background thread

2/ once done, it terminates itself.


The IntentService class offers the
onHandleIntent()

method which will
be asynchronously called by the Android system. This is similar to
doInBackground() for AsyncTask.












IntentService


If the Activity want to interact with the Activity it can use the
bindService() method of the service. This requires an
ServiceConnection object which allows to connect to the Service and
which return a IBinder object. This IBinder object can be used by the
activity to communicate with the Service.


Android Interface Definition Language (AIDL) for remote services


Will cover in more detail in a future lecture.








Bound and Advanced Services

Broadcast Reievers


>You can't trigger the system Broadcasts

>You can define intent filters and also your own broadcast
types



Broadcast Receivers

Broadcast Receivers (aka Receivers)


>You must specify the intent filter in the Android Manifest file (or
programmatically).


The first is the constant defined as a String, the second is the value.
Examples:


ACTION_AIRPLANE_MODE_CHANGED

android.intent.action.AIRPLANE_MODE


ACTION_BATTERY_LOW

android.intent.action.BATTERY_LOW



ACTION_BOOT_COMPLETED

android.intent.action.BOOT_COMPLETED


ACTION_TIMEZONE_CHANGED

android.intent.action.TIMEZONE_CHANGED












Broadcast Receivers





1/ extend BroadcastReceiver


2/ override onRecieve()


http://developer.android.com/reference/android/content/Intent.html






<
receiver

android:name
=
".MyPhoneReceiver"

>


<
intent
-
filter
>


<
action

android:name
=
"android.provider.Telephony.SMS_RECEIVED"

>


</
action
>


</
intent
-
filter
>


</
receiver
>


<
receiver

android:name
=
".SillyMessageReceiver"

>


<
intent
-
filter
>


<
action

android:name
=
"edu.uchicago.cs.gerber.sillymessage"

/>


</
intent
-
filter
>


</
receiver
>


















Broadcast Receivers





1/ extend BroadcastReceiver


2/ override onRecieve()


http://developer.android.com/reference/android/content/Intent.html






<
receiver

android:name
=
".MyPhoneReceiver"

>


<
intent
-
filter
>


<
action

android:name
=
"android.provider.Telephony.SMS_RECEIVED"

>


</
action
>


</
intent
-
filter
>


</
receiver
>


<
receiver

android:name
=
".SillyMessageReceiver"

>


<
intent
-
filter
>


<
action

android:name
=
"edu.uchicago.cs.gerber.sillymessage"

/>


</
intent
-
filter
>


</
receiver
>



















A: AsyncTask: How to get/parse JSON data (weather data) from RESTful
web service (with recipe)


B: MediaStore: Taking pictures and videos and storing them for use in an app
(not the standard gallery) (with recipe)


C: Voice Recognition: How to use voice recognition inside an app (with
recipe)


D: Tracking locations (using GPS) for a jog or a race. Data capture so that I
can map where I went and how far I traveled, avg speed (with recipe)






Student
presentations
: