com.samsung.android.sdk.healthdata
Class HealthDataStore
- java.lang.Object
-
- com.samsung.android.sdk.healthdata.HealthDataStore
-
public class HealthDataStore extends Object
This class provides connection to the health data store.App Manifest
From Android 11, add the following element in your app's manifest. Otherwise, a connection with Samsung Health fails.<manifest . . . > <queries> <package android:name="com.sec.android.app.shealth" /> </queries> </manifest>
Data Store Connection
Health data in the health data store can be accessed after getting connection with the health data store.
HealthDataStore
requests to connect or disconnect with the health data store and its result can be checked withHealthDataStore.ConnectionListener
.An example below shows how to request connection to the health data store and check the result.
public class MainActivity extends Activity { // The state of connection private HealthDataStore mStore; private static final String APP_TAG = "MyApp"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); connect(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } public HealthDataStore connect() { // Connect to the health data store mStore = new HealthDataStore(this, mCntListener); try { mStore.connectService(); } catch (Exception e) { Log.d(APP_TAG, "Connection fails."); e.printStackTrace(); } return mStore; } private final HealthDataStore.ConnectionListener mCntListener = new HealthDataStore.ConnectionListener() { @Override public void onConnected() { Log.d(APP_TAG, "Health data service is connected."); } @Override public void onConnectionFailed(HealthConnectionErrorResult error) { if (error.hasResolution()) { // Resolve an error } else { Log.d(APP_TAG, "Health data service is not available."); } } @Override public void onDisconnected() { Log.d(APP_TAG, "Health data service is disconnected."); } }; }
- Since:
- 1.0.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description static interface
HealthDataStore.ConnectionListener
This interface provides the callback when the client is connected or disconnected with the health data store.
-
Constructor Summary
Constructors Constructor and Description HealthDataStore(Context context, HealthDataStore.ConnectionListener listener)
Constructs and initializes an instance of HealthDataStore with a context and listener.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description void
connectService()
Connects your application to the health data store.void
connectService(long timeout)
Connects your application to the health data store for the given connection timeout.void
disconnectService()
Disconnects your application from the health data store.
-
-
-
Constructor Detail
-
HealthDataStore
public HealthDataStore(Context context, HealthDataStore.ConnectionListener listener)
Constructs and initializes an instance of HealthDataStore with a context and listener.- Parameters:
context
- The context of applicationlistener
- The listener that is implemented to receive the result of the connection or disconnection request.- Throws:
IllegalArgumentException
- If the givencontext
orlistener
isnull
- Since:
- 1.0.0
-
-
Method Detail
-
connectService
public void connectService()
Connects your application to the health data store.
It tries connection for one minute in default. If you want to set a connection timeout differently, useconnectService(long)
.The connection result can be checked in the following handlers.
HealthDataStore.ConnectionListener.onConnected()
is invoked if it succeeds.HealthDataStore.ConnectionListener.onConnectionFailed(HealthConnectionErrorResult)
is invoked if it fails.
- Throws:
IllegalStateException
- If the instance is not constructed with a valid context instance- Since:
- 1.0.0
-
connectService
public void connectService(long timeout)
Connects your application to the health data store for the given connection timeout.The connection result can be checked in the following handlers.
HealthDataStore.ConnectionListener.onConnected()
is invoked if it succeeds.HealthDataStore.ConnectionListener.onConnectionFailed(HealthConnectionErrorResult)
is invoked if it fails.
- Parameters:
timeout
- The timeout value in milliseconds for connection to health data store. If connection doesn't succeed in timeout,HealthDataStore.ConnectionListener.onConnectionFailed(HealthConnectionErrorResult)
is invoked.- Throws:
IllegalStateException
- If the instance is not constructed with a valid context instance- Since:
- 1.0.0
-
disconnectService
public void disconnectService()
Disconnects your application from the health data store.
Note thatHealthDataStore.ConnectionListener.onDisconnected()
is not invoked after calling this method.- Since:
- 1.0.0
-
-