5
我想爲我的應用程序添加步驟計數器。 現在我使用Google fit
來衡量用戶步數。是否可以檢測用戶是否安裝並使用Google健身應用程序?
我面臨的問題是,並非所有設備都安裝了Google Fit
應用程序,即使這樣做 - 並非所有用戶都註冊到應用程序服務(作爲客戶端)。
所以所需流量是:
- 檢測是否安裝
Google fit
應用程序。 - 如果已安裝,請檢查用戶是否註冊了
Google fit
應用程序。 - 如果已安裝並註冊到
Google fit
應用程序,請檢查用戶帳戶類型支持是否使用健身服務。 (記錄步驟等數據) - 如果以上都好,請檢查用戶是否確認了Google適配器彈出窗口。
基本上我想如果用戶使用健身應用(使用上述所有條件)來檢查,如果一個失敗,則它將使用在設備中的傳感器STEPCOUNT(如果存在的話),如果傳感器不存在它將使用其他傳感器來實現這一目標。
下面是我使用與谷歌飛度API連接代碼:
private void buildFitnessClient() {
// Create the Google API Client
mClient = new GoogleApiClient.Builder(getContext())
.addApi(Fitness.HISTORY_API)
.addScope(new Scope(Scopes.FITNESS_ACTIVITY_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.
new getFitnessTask().execute();
}
@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.i(TAG, "Connection lost. Cause: Network Lost.");
} else if (i == GoogleApiClient.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
if (getActivity() != null) {
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(),
getActivity(), 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; activity; "+ getActivity());
if (getActivity() != null) {
authInProgress = true;
result.startResolutionForResult(getActivity(),
REQUEST_OAUTH);
}
} catch (IntentSender.SendIntentException e) {
Log.e(TAG,
"Exception while starting resolution activity", e);
}
}
}
}
)
.build();
}
public void onActivityResult(int requestCode, int resultCode, Intent data){
Log.e(TAG, "onActivityResult requestCode=" + requestCode + ", resultCode=" + resultCode);
if (requestCode == REQUEST_OAUTH) {
authInProgress = false;
if (resultCode == Activity.RESULT_OK) {
Log.e(TAG, "RESULT_OK");
// Make sure the app is not already connected or attempting to connect
if (!mClient.isConnecting() && !mClient.isConnected()) {
mClient.connect();
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.e(TAG, "RESULT_CANCELED");
}
}
}
private class getFitnessTask extends AsyncTask<Void, Void, Integer> {
...
}
}
需要您的幫助球員,
感謝。
我也面臨同樣的問題,任何解決方案呢? – shekar
也尋找相同問題的解決方案。如果你已經知道了,請發佈答案。 –