Vla*_*dan 7 android google-fit
我正在尝试将我的应用程序连接到Google Fit.我正在使用需要执行以下操作的IntentService.当我有关于步骤的信息时开始.此时我正在尝试通过调用以下代码来创建GoogleApiClient:
mClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.HISTORY_API)
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE))
.addScope(new Scope(Scopes.FITNESS_LOCATION_READ_WRITE))
.addConnectionCallbacks(
new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
log.info("FITNESS_API: Connected!!!");
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
insertOrUpdateDataPoints();
}
});
thread.start();
}
@Override
public void onConnectionSuspended(int i) {
// If your connection to the sensor gets lost at some point,
// you'll be able to determine the reason and react to it here.
if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) {
log.info("FITNESS_API: Connection lost. Cause: Network Lost.");
} else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
log.info("FITNESS_API: Connection lost. Reason: Service Disconnected");
}
}
}
).build();
mClient.connect();
Run Code Online (Sandbox Code Playgroud)
创建DataSet并将步骤详细信息添加为DataPoint elemnets后,我将信息同步到Google Fit并关闭GoogleApiClient:
com.google.android.gms.common.api.Status insertStatus =
Fitness.HistoryApi.insertData(mClient, dataSet).await(1, TimeUnit.MINUTES);
// Before querying the data, check to see if the insertion succeeded.
if (!insertStatus.isSuccess()) {
log.info("FITNESS_API: There was a problem inserting the dataset. Status = " + insertStatus.getStatusCode());
}
mClient.disconnect();
mClient = null;
Run Code Online (Sandbox Code Playgroud)
问题是,通过尝试自己管理GoogleApiClient(没有enableAutoManage),我不会被提示允许应用程序将数据发布到Google Fit.如果我在创建GoogleApiClient时使用enableAutoManage,则此行为会更改.但是,为了为客户端启用AutoManage,由于enableAutoManage所需的参数,我需要有一个ActivityFragment.我无法访问IntentyService中的ActivityFragment,我确实希望将客户端和插入操作的管理保存在可以在后台运行的单独服务中.
此外,当我没有使用enableAutoManage时,即使我已经注册了GoogleApiClient的连接回调,也没有任何反应.
如何确保我的应用程序提示用户允许该应用发布到Google健身?如果应用在用户打开应用时无法在Google健身中发布,我需要这样做.有任何想法吗?谢谢.
我已经找到了解决方案。
如果你不想使用“enableAutoManage”,你需要注册 onConnectionFailed 方法,如下所示:
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
if( !authInProgress ) {
try {
authInProgress = true;
connectionResult.startResolutionForResult( MainActivity.this, REQUEST_OAUTH );
} catch(IntentSender.SendIntentException e ) {
}
} else {
Log.e( "GoogleFit", "authInProgress" );
}
}
Run Code Online (Sandbox Code Playgroud)
这将显示对话框。
| 归档时间: |
|
| 查看次数: |
841 次 |
| 最近记录: |