Service | SampleService

Health Service provides a sample application 'SampleService' to show how to post your tracker tile and work with Samsung Health. Check the sample application by importing its project with Android Studio (or Eclipse).

Prerequisites

Prerequisites to run SampleService are shown below.

  • Prepare an Android device that supports Android 4.4 KitKat (API level 19) or above.
    (Samsung and non-Samsung devices are available both).

  • Install Samsung Health on the device.

  • Turn on the developer mode.

  • Import SampleService with Android Studio (or Eclipse).

  • Run SampleService on the device.

Overview

If Samsung Health and SampleService are installed properly, you can:

  • find the Sample tracker on the Samsung Health‘s tracker list after installing the sample app.

  • launch some Samsung Health's available tracker.

Figure 15: PluginTracker Figure: SampleService

If you subscribe to the Sample tracker and go back to the Samsung Health‘s main screen, its tile will be shown as the middle figure. Its tile is posted with TRACKER_TILE_TYPE_1 at the first time. The sample app's posted tile will be changed to the different tile template - TRACKER_TILE_TYPE_3 that includes a content value, unit, and updated time whenever the tile's button is tapped.

The posted tracker tile's ID can be checked on SampleService as the figure's third screenshot and you can remove the posted tracker tile.

Connection to Health Data Store

The app needs to use the SDK's Health Data basically and connect to the health data store to handle various exceptions whether the app can work with Samsung Health on the device.

But SampleService doesn't include the Health Data API usage because it focuses to show the tracker usage only.

See the SimpleHealth sample application in the SDK or the Health Data‘s Programming Guide for the health data store connection.

Source Description

The following table describes source and resource files of SampleService.

Source / Resource Description
AndroidManifest.xml
  • Registering the tracker tile button's service intent (MyTrackerService) with the <service> element.

  • Declaring Samsung Health's PluginService with the <service> element.

res/values/
sample_tracker.xml Defining tracker with its metadata.
src/com/samsung/android/app/sampleservice/
MainActivity.java SampleService’s main activity.
  • TrackerManager

    • Showing a list for some Samsung Health trackers.

    • Launching the selected tracker.

  • TrackerTileManager

    • Showing the posted tile's ID on the Samsung Health's main screen.

    • Containing a button to remove a posted tile.

MyTracker.java Checking the feature availability.
Implementing the app's tracker.
  • Implementing TrackerEventListener to receive the following tracker events from Samsung Health:

    • If the user subscribes or unsubscribes the SampleService's tracker,
      => onSubscribed(), onUnsubscribed()

    • If Samsung Health goes to the background,
      => onPaused()

    • If Samsung Health requests the tracker tile of SampleService,
      => onTileRequested()

    • If the posted tile is removed from the Samsung Health's main screen,
      => onTileRemoved()

  • Posting or updating the tracker tile of SampleService depending on the received tracker event.

SampleService.java Initializing the Health Service.
MyTrackerService Defining the tracker tile's button intent to be performed in the background.
Table: SampleService's source description

See detailed implementation in the imported SampleService's project through Android Studio (or Eclipse).