Tôi muốn thêm bộ đếm bước vào ứng dụng của mình. Hiện tại tôi đang sử dụng Google fit
để đo số bước của người dùng.Có thể phát hiện xem ứng dụng Google Fit có được người dùng cài đặt và sử dụng không?
Vấn đề tôi đang gặp phải là không phải tất cả các thiết bị đều đã cài đặt ứng dụng Google Fit
và thậm chí nếu không - không phải tất cả người dùng đều đăng ký dịch vụ ứng dụng (với tư cách khách hàng).
Vì vậy, dòng chảy mong muốn là:
- Detect nếu
Google fit
ứng dụng được cài đặt. - Nếu được cài đặt, hãy kiểm tra xem người dùng có được đăng ký
Google fit
ứng dụng hay không. - Nếu được cài đặt và đăng ký với ứng dụng
Google fit
, hãy kiểm tra xem hỗ trợ loại tài khoản người dùng có sử dụng dịch vụ thể dục hay không. (ghi dữ liệu chẳng hạn như các bước) - Nếu tất cả ở trên ok, hãy kiểm tra xem người dùng đã xác nhận 'Google fit pop-up chưa.
Về cơ bản, tôi muốn kiểm tra xem người dùng có sử dụng ứng dụng phù hợp không (nếu sử dụng tất cả các điều kiện ở trên), nếu không sử dụng được cảm biến StepCount trên thiết bị (nếu có). sử dụng các cảm biến khác để đạt được mục tiêu này.
Đây là mã mà tôi đang sử dụng để kết nối với Google Fit 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> {
...
}
}
Cần kẻ giúp đỡ của bạn,
Cảm ơn.
Tôi cũng đang đối mặt với cùng một vấn đề, giải pháp nào cho vấn đề này? – shekar
Tìm kiếm một giải pháp cho cùng một câu hỏi. xin vui lòng gửi câu trả lời nếu bạn đã có nó. –