This shows you the differences between two versions of the page.
smd:laboratoare:03 [2021/03/31 21:35] adriana.draghici |
smd:laboratoare:03 [2021/04/08 16:57] (current) adriana.draghici [Using Intent Services] |
||
---|---|---|---|
Line 83: | Line 83: | ||
====Start a service==== | ====Start a service==== | ||
+ | |||
<code java> | <code java> | ||
Line 111: | Line 112: | ||
intent.putExtra(EXTRA_INFO, info); | intent.putExtra(EXTRA_INFO, info); | ||
return intent; | return intent; | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | :!: If your project is configured for versions higher than Oreo, there's no need to perform the check for Oreo. | ||
+ | |||
+ | |||
+ | ====Start foreground service==== | ||
+ | * Examples: [[https://developer.android.com/guide/components/foreground-services#java|documentation]], [[https://androidwave.com/foreground-service-android-example/|tutorial]] | ||
+ | * Create a persistent notification like in the links provided above. | ||
+ | * Call ''startForeground'' | ||
+ | |||
+ | <code java> | ||
+ | // In LocationService | ||
+ | public void startLocationTracking(int interval) { | ||
+ | Log.d(TAG, "Location tracking is active"); | ||
+ | Notification notification = buildNotification(); | ||
+ | setServiceForeground(true, notification); | ||
+ | } | ||
+ | |||
+ | private void setServiceForeground(Boolean serviceIsForeground, Notification notification) { | ||
+ | if (this.serviceIsForeground != serviceIsForeground) { | ||
+ | this.serviceIsForeground = serviceIsForeground; | ||
+ | if (serviceIsForeground) { | ||
+ | startForeground(LOCATION_NOTIFICATION_ID, notification); | ||
+ | } else { | ||
+ | stopForeground(true); | ||
+ | } | ||
+ | } | ||
} | } | ||
</code> | </code> | ||
Line 116: | Line 145: | ||
==== Tasks details ==== | ==== Tasks details ==== | ||
- | Create a new project with a basic MainActivity. You will use this project for all the tasks in this lab. | + | Create a new project with a basic MainActivity, supporting versions > Oreo. You will use this project for all the tasks in this lab. |
For better code readability and to avoid errors caused by typos please define all the names of the intent actions and their parameters in constant fields. Place the constants in the classes they are related to (e.g. intent actions for a component should be declared in that class/file if in Kotlin). Provide static methods for obtaining a component's starting intent. | For better code readability and to avoid errors caused by typos please define all the names of the intent actions and their parameters in constant fields. Place the constants in the classes they are related to (e.g. intent actions for a component should be declared in that class/file if in Kotlin). Provide static methods for obtaining a component's starting intent. | ||
Line 124: | Line 153: | ||
=== Task 1 StartedService (2p) === | === Task 1 StartedService (2p) === | ||
- | In this task we will create a StartedService which prints a message using a Toast. | + | In this task we will create a Foreground Started Service which prints a message using a Toast. |
- | * Create a new project and add a new Service, and name it //MyStartedService//. | + | * Add a new Service, and name it //MyStartedService//. |
* In //MyStartedService// override ''onCreate'', ''onStartCommand'', ''onBind'' and ''onDestroy'' and add a debug log message with the name of the method and the name of the current thread | * In //MyStartedService// override ''onCreate'', ''onStartCommand'', ''onBind'' and ''onDestroy'' and add a debug log message with the name of the method and the name of the current thread | ||
* Start the service as //START_NOT_STICKY// | * Start the service as //START_NOT_STICKY// | ||
Line 190: | Line 219: | ||
* Show the date using a Toast message | * Show the date using a Toast message | ||
- | ====Start foreground service==== | ||
- | * Create a persistent notification (see [[https://proandroiddev.com/deep-dive-into-android-services-4830b8c9a09|example]]) | ||
- | * Call ''startForeground'' | ||
- | |||
- | <code java> | ||
- | // In LocationService | ||
- | public void startLocationTracking(int interval) { | ||
- | Log.d(TAG, "Location tracking is active"); | ||
- | Notification notification = buildNotification(); | ||
- | setServiceForeground(true, notification); | ||
- | } | ||
- | |||
- | private void setServiceForeground(Boolean serviceIsForeground, Notification notification) { | ||
- | if (this.serviceIsForeground != serviceIsForeground) { | ||
- | this.serviceIsForeground = serviceIsForeground; | ||
- | if (serviceIsForeground) { | ||
- | startForeground(LOCATION_NOTIFICATION_ID, notification); | ||
- | } else { | ||
- | stopForeground(true); | ||
- | } | ||
- | } | ||
- | } | ||
- | </code> | ||
==== Using Intent Services==== | ==== Using Intent Services==== | ||
Line 222: | Line 228: | ||
* If we want to send a response from the IntentService to the activity, we can broadcast the result using intents and register a broadcast receiver for them. | * If we want to send a response from the IntentService to the activity, we can broadcast the result using intents and register a broadcast receiver for them. | ||
+ | |||
+ | /* | ||
+ | TODO INTENT SERVICE IS DEPRECATED, LEAVE THE TEXT< ADD A WARNING AND REMOVE THE EXERCISE | ||
+ | */ | ||
=== Task 3 LuckyIntentService (2p) === | === Task 3 LuckyIntentService (2p) === | ||