Mor*_*itz 1 android google-fit-sdk google-fit
我在使用Google提供的Google Fit运行示例应用时遇到问题.
我想通了,到目前为止,那resultCode我得到的onActivityResult是0我选择了后Account.
我现在使用的示例应用程序BasicRecordingApi可以在这里找到.但我也尝试过其他人.
我为该应用创建了一个API密钥,并且我已经启用了Google Fit API.
什么都没有帮助.
有任何想法吗?
vis*_*gid 12
Android入门
这解释了如何在Android上开始使用Google Fit进行开发.Google Fit是Google Play服务的一部分.
第1步:获取Google Play服务
Google健身适用于具有Google Play服务7.0或更高版本的Android设备.运行Android 2.3或更高版本且具有Google Play商店应用的设备会自动接收Google Play服务的更新.
要查看设备上安装的Google Play服务版本,请转到"设置">"应用">"Google Play服务".
确保您在开发主机上拥有最新的Google Play服务客户端库:
1.打开Android SDK Manager.2.在Extras下,找到Google Play服务和Google Repository.3.如果这些软件包的状态与"已安装"不同,请同时选择它们并单击"安装软件包".
第2步:获取Google帐户 要使用Google健身API,您需要拥有Google帐户.
第3步:启用Fitness API 在Google控制台上创建项目
1.转到Google Developers Console.2.单击"创建项目".3.在"项目名称"字段中,键入项目的名称,然后单击"创建".
激活Fitness API 1. 转到Google Developers Console.2.在左侧边栏中,单击API和Auth.3.找到Fitness API并将其状态设置为ON.
创建新的客户端ID
1.转到Google Developers Console.2.在左侧边栏中,单击"凭据".3.单击"创建新客户端ID".将出现"创建客户端ID"对话框.4.在应用程序类型下,选择已安装的应用程序.5.在已安装的应用程序类型下,选择Android.6.在"包名称"字段中,输入Android应用程序的包名称.7.在签名证书指纹(SHA1)字段中,输入证书的SHA1指纹.8.单击"创建客户端ID".
第4步:配置项目
在IDE中,打开模块的build.gradle文件,并将Google Play服务客户端库添加为依赖项:
apply plugin: 'com.android.application'
...
dependencies {
compile 'com.google.android.gms:play-services-fitness:8.3.0'
}
Run Code Online (Sandbox Code Playgroud)
第5步:连接健身服务
Android入门
本文档介绍了如何在Android上开始使用Google健身版进行开发.Google Fit是Google Play服务的一部分.
第1步:获取Google Play服务
Google健身适用于具有Google Play服务7.0或更高版本的Android设备.运行Android 2.3或更高版本且具有Google Play商店应用的设备会自动接收Google Play服务的更新.
要查看设备上安装的Google Play服务版本,请转到"设置">"应用">"Google Play服务".
确保您在开发主机上拥有最新的Google Play服务客户端库:
打开Android SDK Manager.在Extras下,找到Google Play服务和Google Repository.如果这些程序包的状态与"已安装"不同,请同时选择它们并单击"安装程序包".第2步:获取Google帐户
要使用Google Fit API,您需要一个Google帐户.如果您已经拥有一个帐户,那么您已经完成了设置.您可能还需要一个单独的Google帐户进行测试.
第3步:启用Fitness API
要进行身份验证并与Google健身联系,您必须在Google Developers Console中创建项目,激活Fitness API,创建OAuth 2.0客户端ID,并从您应用的已签名APK中注册公共证书.
创建一个项目
注意:对Android和REST版本的应用程序使用相同的项目.转到Google Developers Console.单击创建项目.在"项目名称"字段中,键入项目的名称,然后单击"创建".激活Fitness API
转到Google Developers Console.在左侧边栏中,单击API和Auth.找到Fitness API并将其状态设置为ON.Fitness API现在显示在API列表的顶部.
获取证书的SHA1指纹
要为Android应用创建新的客户端ID,您需要使用用于签署APK的证书的SHA1指纹.要获得此指纹:
找到密钥库的位置.在终端中,从JDK运行keytool实用程序.例如,如果您使用的是debug密钥库:$ keytool -exportcert -alias\androiddebugkey -keystore \〜/ .android/debug.keystore -list -v注意:在Mac OS和Linux上,调试密钥库通常位于〜/ .android/debug.keystore.在Windows上,它通常位于%USERPROFILE%.android\debug.keystore.
keytool命令的输出包含certicate的SHA1指纹.
图1:"创建客户端ID"对话框.创建新的客户端ID
转到Google Developers Console.在左侧边栏中,单击"凭据".单击"创建新客户端ID".将出现"创建客户端ID"对话框.在Application Type下,选择Installed application.在已安装的应用程序类型下,选择Android.在"包名称"字段中,输入Android应用的包名称.在"签名证书指纹(SHA1)"字段中,输入证书的SHA1指纹.单击"创建客户端ID" 第4步:配置项目
Android Studio可让您轻松为Google Fit创建项目.按照创建项目中描述的步骤进行操作.
在IDE中,打开模块的build.gradle文件,并将Google Play服务客户端库添加为依赖项:
apply plugin: 'com.android.application'
...
dependencies {
compile 'com.google.android.gms:play-services-fitness:8.3.0'
}
Run Code Online (Sandbox Code Playgroud)
第5步:连接健身服务
在您从Google Fit API调用方法之前,您必须连接到相应的健身服务或服务,这些服务或服务是Google Play服务的一部分.您可以使用以下API:
1.Fitness.SENSORS_API
2.Fitness.RECORDING_API
3.Fitness.HISTORY_API
4.Fitness.SESSIONS_API
5.Fitness.BLE_API
6.Fitness.CONFIG_API
Run Code Online (Sandbox Code Playgroud)
创建API客户端,如下所示:
1.定义活动中的变量以帮助您跟踪连接状态:
private static final int REQUEST_OAUTH = 1;
/**
* Track whether an authorization activity is stacking over the current activity, i.e. when
* a known auth error is being resolved, such as showing the account chooser or presenting a
* consent dialog. This avoids common duplications as might happen on screen rotations, etc.
*/
private static final String AUTH_PENDING = "auth_state_pending";
private boolean authInProgress = false;
private GoogleApiClient mClient = null;
Run Code Online (Sandbox Code Playgroud)
2.确定onCreate方法中的身份验证是否正在进行中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Put application specific code here.
if (savedInstanceState != null) {
authInProgress = savedInstanceState.getBoolean(AUTH_PENDING);
}
buildFitnessClient();
}
Run Code Online (Sandbox Code Playgroud)
**3.创建Google API客户端并提供所需的回调方法:*
/**
* Build a {@link GoogleApiClient} that will authenticate the user and allow the application
* to connect to Fitness APIs. The scopes included should match the scopes your app needs
* (see documentation for details). Authentication will occasionally fail intentionally,
* and in those cases, there will be a known resolution, which the OnConnectionFailedListener()
* can address. Examples of this include the user never having signed in before, or having
* multiple accounts on the device and needing to specify which account to use, etc.
*/
private void buildFitnessClient() {
// Create the Google API Client
mClient = new GoogleApiClient.Builder(this)
.addApi(Fitness.SENSORS_API)
.addScope(new Scope(Scopes.FITNESS_LOCATION_READ))
.addConnectionCallbacks(
new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Connected!!!");
// Now you can make calls to the Fitness APIs.
// Put application specific code here.
}
@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 == ConnectionCallbacks.CAUSE_NETWORK_LOST) {
Log.i(TAG, "Connection lost. Cause: Network Lost.");
} else if (i == ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
Log.i(TAG, "Connection lost. Reason: Service Disconnected");
}
}
}
)
.addOnConnectionFailedListener(
new GoogleApiClient.OnConnectionFailedListener() {
// Called whenever the API client fails to connect.
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "Connection failed. Cause: " + result.toString());
if (!result.hasResolution()) {
// Show the localized error dialog
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(),
MainActivity.this, 0).show();
return;
}
// The failure has a resolution. Resolve it.
// Called typically when the app is not yet authorized, and an
// authorization dialog is displayed to the user.
if (!authInProgress) {
try {
Log.i(TAG, "Attempting to resolve failed connection");
authInProgress = true;
result.startResolutionForResult(MainActivity.this,
REQUEST_OAUTH);
} catch (IntentSender.SendIntentException e) {
Log.e(TAG,
"Exception while starting resolution activity", e);
}
}
}
}
)
.build();
}
Run Code Online (Sandbox Code Playgroud)
4.管理活动中客户端的连接生命周期:
@Override
protected void onStart() {
super.onStart();
// Connect to the Fitness API
Log.i(TAG, "Connecting...");
mClient.connect();
}
@Override
protected void onStop() {
super.onStop();
if (mClient.isConnected()) {
mClient.disconnect();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_OAUTH) {
authInProgress = false;
if (resultCode == RESULT_OK) {
// Make sure the app is not already connected or attempting to connect
if (!mClient.isConnecting() && !mClient.isConnected()) {
mClient.connect();
}
}
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4361 次 |
| 最近记录: |