Hello Health Data
hello health data the following sections give you fundamentals for developing the samsung health's partner app check first the development environment for samsung health's partner apps importing library add the following library to the “libs” folder in your created application project samsung-health-data-a b c aar health data store connection add a <queries> element in your app manifest <manifest > <queries> <package android name="com sec android app shealth" /> </queries> </manifest> connect to the health data store with healthdatastore public class mainactivity extends activity { public static final string app_tag = "simplehealth"; private static mainactivity minstance = null; private healthdatastore mstore; private healthconnectionerrorresult mconnerror; private set<permissionkey> mkeyset; @override public void oncreate bundle savedinstancestate { // minstance = this; mkeyset = new hashset<permissionkey> ; mkeyset add new permissionkey healthconstants stepcount health_data_type, permissiontype read ; // create a healthdatastore instance and set its listener mstore = new healthdatastore this, mconnectionlistener ; // request the connection to the health data store mstore connectservice ; } you can end the health data store connection when the activity is destroyed @override public void ondestroy { mstore disconnectservice ; super ondestroy ; } the connection result is sent to healthdatastore connectionlistener if it succeeds, acquiring data permission or querying data will be available private final healthdatastore connectionlistener mconnectionlistener = new healthdatastore connectionlistener { @override public void onconnected { log d app_tag, "health data service is connected " ; healthpermissionmanager pmsmanager = new healthpermissionmanager mstore ; try { // check whether the permissions that this application needs are acquired // request the permission for reading step counts if it is not acquired // get the current step count and display it if data permission is required // } catch exception e { log e app_tag, e getclass getname + " - " + e getmessage ; log e app_tag, "permission setting fails " ; } } @override public void onconnectionfailed healthconnectionerrorresult error { log d app_tag, "health data service is not available " ; showconnectionfailuredialog error ; } @override public void ondisconnected { log d app_tag, "health data service is disconnected " ; } }; the connection to the health data store can fail and you can check its error result through onconnectionfailed if there is an error, an application checks whether the health framework provides a solution with hasresolution and calls resolve if the health framework provides its solution, resolve makes an application move to one of the following page without a dialog message app market's samsung health page to install or update it device's settings page to make samsung health available samsung health user's agreement page an application needs to show a proper message for each error case and call resolve private void showconnectionfailuredialog healthconnectionerrorresult error { alertdialog builder alert = new alertdialog builder this ; mconnerror = error; string message = "connection with samsung health is not available"; if mconnerror hasresolution { switch error geterrorcode { case healthconnectionerrorresult platform_not_installed message = "please install samsung health"; break; case healthconnectionerrorresult old_version_platform message = "please upgrade samsung health"; break; case healthconnectionerrorresult platform_disabled message = "please enable samsung health"; break; case healthconnectionerrorresult user_agreement_needed message = "please agree with samsung health policy"; break; default message = "please make samsung health available"; break; } } alert setmessage message ; alert setpositivebutton "ok", new dialoginterface onclicklistener { @override public void onclick dialoginterface dialog, int id { if mconnerror hasresolution { mconnerror resolve minstance ; } } } ; if error hasresolution { alert setnegativebutton "cancel", null ; } alert show ; } see the health data store for more information permission request the meta-data element for the required data permission value in manifest works with the permission request api if you want to request data permission for reading the step count, write its value in your application project's manifest as the following example see privacy check flow in privacy <application <meta-data android name="com samsung android health permission read" android value="com samsung health step_count" /> </application> create a permission key set and add a permission key for reading the step count public class mainactivity extends activity { private set<permissionkey> mkeyset; @override public void oncreate bundle savedinstancestate { // mkeyset = new hashset<permissionkey> ; mkeyset add new permissionkey healthconstants stepcount health_data_type, permissiontype read ; // connect to health data store } and call healthpermissionmanager requestpermissions with its listener private final healthdatastore connectionlistener mconnectionlistener = new healthdatastore connectionlistener { @override public void onconnected { log d app_tag, "health data service is connected " ; healthpermissionmanager pmsmanager = new healthpermissionmanager mstore ; try { // check whether the permissions that this application needs are acquired map<permissionkey, boolean> resultmap = pmsmanager ispermissionacquired mkeyset ; if resultmap containsvalue boolean false { // request the permission for reading step counts if it is not acquired pmsmanager requestpermissions mkeyset, mainactivity this setresultlistener mpermissionlistener ; } else { // get the current step count and display it // } } catch exception e { log e app_tag, e getclass getname + " - " + e getmessage ; log e app_tag, "permission setting fails " ; } } // }; if requestpermissions is called successfully, the permission ui is popped up to the user the user's permission information is saved by selecting "done" after the user allows each data permission and it is received through healthresultholder resultlistener private final healthresultholder resultlistener<permissionresult> mpermissionlistener = new healthresultholder resultlistener<permissionresult> { @override public void onresult permissionresult result { log d app_tag, "permission callback is received " ; map<permissionkey, boolean> resultmap = result getresultmap ; if resultmap containsvalue boolean false { // requesting permission fails } else { // get the current step count and display it } } }; } see the health permission manager for more information