S健康是三星的一个健康类应用, 里面集成了一些健康相关的功能, 目前计步器已经集成在其中, 那么现在也集成春雨医生进去吧!
S健康开发网址
下载SDK包和申请合作APP.
SDK -> SHealthService -> Tools 有S Health Simulator模拟器, 可以展示卡片效果. 申请合作App之后, 才可以在S健康里面显示.
导入jar包
samsung-digital-health-shealth-v1.0.0.jar
sdk-v1.0.0.jar
配置AndroidManifest.xml
<!-- 三星SHealth Service SDK接入 -->
<uses-permission android:name="com.samsung.android.providers.context.permission.WRITE_USE_APP_FEATURE_SURVEY"/>
<!-- Registration PluginService with Tracker Information -->
<service
android:name="com.samsung.android.sdk.shealth.PluginService"
android:exported="true">
<meta-data
android:name="tracker.chunyu_doctor"
android:value="@string/testtracker_manifest"/>
</service>
资源文件
<!--三星S健康-->
<string name="testtracker_manifest"><![CDATA[
{
"tracker" : {
"id" : "tracker.chunyu_doctor",
"display-name" : "tracker_display_name",
"icon" : "icon",
"controller" : "me.chunyu.ChunyuDoctor.shealth.ChunyuDoctorTracker"
}
}
]]></string>
<string name="tracker_display_name">春雨医生</string>
<string name="tracker_display_start">免费咨询</string>
跟踪器代码
public class ChunyuDoctorTracker implements TrackerEventListener {
private static final String TAG = "DEBUG-WCL: " + ChunyuDoctorTracker.class.getSimpleName();
private static final String MY_TILE_ID = "chunyu_tracker";
private TrackerTileManager mTrackerTileManager;
@Override
public void onCreate(Context context, String s) {
Log.e(TAG, "onCreate");
if (mTrackerTileManager == null) {
try {
mTrackerTileManager = new TrackerTileManager(context);
} catch (IllegalArgumentException e) {
Log.d(TAG, "MyTracker onCreate() - IllegalArgumentException");
}
}
}
@Override
public void onSubscribed(Context context, String trackerId) {
Log.e(TAG, "onSubscribed");
updateTile(context, trackerId, MY_TILE_ID);
}
@Override
public void onUnsubscribed(Context context, String trackerId) {
Log.e(TAG, "onUnsubscribed");
}
@Override
public void onTileRequested(Context context, String trackerId, String tileId) {
Log.e(TAG, "onTileRequested");
// The main screen requested the tracker tile.
if (tileId == null) {
tileId = MY_TILE_ID;
}
// Update your tracker tile.
updateTile(context, trackerId, tileId);
}
@Override
public void onTileRemoved(Context context, String s, String s1) {
Log.e(TAG, "onTileRemoved");
}
@Override
public void onPaused(Context context, String s) {
Log.e(TAG, "onPaused");
}
public void updateTile(Context context, String trackerId, String tileId) {
Log.d(TAG, "updateTile(" + trackerId + ", " + tileId + ")");
TrackerTile myTrackerTile = null;
if (tileId == null) {
tileId = MY_TILE_ID;
}
Log.e(TAG, "trackerId = " + trackerId + ", tileId = " + tileId);
try {
// Create Intent to do an action
// when the tracker tile is clicked
Intent launchIntent = new Intent(context, WelcomeActivity.class);
Intent serviceIntent = new Intent(context, WelcomeActivity.class);
// Set template
int template = TrackerTile.TRACKER_TILE_TYPE_1;
// Create TrackerTile and set each values and intents
myTrackerTile = new TrackerTile(context, trackerId, tileId, template);
// Set a tracker tile
myTrackerTile
.setTitle(R.string.tracker_display_name)
.setIcon(R.drawable.icon)
.setDate(new Date())
.setContentColor(Color.parseColor("#e75c49"))
.setContentIntent(TrackerTile.INTENT_TYPE_ACTIVITY, launchIntent)
.setButtonIntent(context.getString(R.string.tracker_display_start),
TrackerTile.INTENT_TYPE_ACTIVITY, serviceIntent);
if (mTrackerTileManager != null) {
mTrackerTileManager.post(myTrackerTile);
}
} catch (IllegalArgumentException e) {
Log.d(TAG, "MyTracker updateTile(" + trackerId + ", " + tileId +
") IllegalArgumentException " + e.toString());
} catch (Resources.NotFoundException e) {
Log.d(TAG, "MyTracker updateTile(" + trackerId + ", " + tileId + ") NotFoundException");
}
}
}
错误会抛出异常, 注意读取. 我在开发过程中, 就遇到图片大小的问题.
TrackerTile.TRACKER_TILE_TYPE_1的类型很重要, 不同类型图片大小不同.
初始化SHealth, 在App类.
@Override
public void onCreate() {
super.onCreate();
...
initSHealth(); // SHealth初始化
}
/**
* 初始化SHealth Health Service
*/
private void initSHealth() {
Log.d("DEBUG-WCL", "SHealth初始化");
Shealth shealth = new Shealth();
try {
shealth.initialize(this);
Log.d("DEBUG-WCL", "SHealth初始化");
} catch (SsdkUnsupportedException e) {
int eType = e.getType();
Log.d("DEBUG-WCL", "Samsung Digital Health Initialization failed. Error type : " + eType);
if (eType == SsdkUnsupportedException.VENDOR_NOT_SUPPORTED) {
// It is thrown if the device does not support the S Health SDK.
} else if (eType ==
SsdkUnsupportedException.LIBRARY_NOT_INSTALLED) { // It is thrown if the library of the SDK is not found.
}
} catch (Exception e1) {
Log.d("DEBUG-WCL", "Samsung Digital Health Initialization failed.");
}
}
提取签名密匙:
OK, 程序可以显示啦.