Interfejsy Google Fit API, w tym interfejs Google Fit API typu REST, zostaną wycofane w 2026 roku. Od 1 maja 2024 r. deweloperzy nie mogą rejestrować się w celu korzystania z tych interfejsów API.
Sprawdź, czy użytkownik wcześniej zezwolił na dostęp do niezbędnych danych. Jeśli nie, rozpocznij proces autoryzacji:
if(!GoogleSignIn.hasPermissions(account,fitnessOptions)){GoogleSignIn.requestPermissions(this,// your activityGOOGLE_FIT_PERMISSIONS_REQUEST_CODE,// e.g. 1account,fitnessOptions)}else{accessGoogleFit()}
Jeśli proces autoryzacji jest wymagany, zrób odpowiedź użytkownika:
overridefunonActivityResult(requestCode:Int,resultCode:Int,data:Intent?){super.onActivityResult(requestCode,resultCode,data)when(resultCode){Activity.RESULT_OK->when(requestCode){GOOGLE_FIT_PERMISSIONS_REQUEST_CODE->accessGoogleFit()else->{// Result wasn't from Google Fit}}else->{// Permission not granted}}}
Gdy użytkownik zezwoli użytkownikowi na dostęp do żądanych danych, utwórz aktywność
(np. HistoryClient do odczytu lub zapisu historycznej aktywności fizycznej
danych) zależnie od przeznaczenia i potrzeb aplikacji:
privatefunaccessGoogleFit(){valend=LocalDateTime.now()valstart=end.minusYears(1)valendSeconds=end.atZone(ZoneId.systemDefault()).toEpochSecond()valstartSeconds=start.atZone(ZoneId.systemDefault()).toEpochSecond()valreadRequest=DataReadRequest.Builder().aggregate(DataType.AGGREGATE_STEP_COUNT_DELTA).setTimeRange(startSeconds,endSeconds,TimeUnit.SECONDS).bucketByTime(1,TimeUnit.DAYS).build()valaccount=GoogleSignIn.getAccountForExtension(this,fitnessOptions)Fitness.getHistoryClient(this,account).readData(readRequest).addOnSuccessListener({response->
// Use response data hereLog.i(TAG,"OnSuccess()")}).addOnFailureListener({e->Log.d(TAG,"OnFailure()",e)})}
[[["Łatwo zrozumieć","easyToUnderstand","thumb-up"],["Rozwiązało to mój problem","solvedMyProblem","thumb-up"],["Inne","otherUp","thumb-up"]],[["Brak potrzebnych mi informacji","missingTheInformationINeed","thumb-down"],["Zbyt skomplikowane / zbyt wiele czynności do wykonania","tooComplicatedTooManySteps","thumb-down"],["Nieaktualne treści","outOfDate","thumb-down"],["Problem z tłumaczeniem","translationIssue","thumb-down"],["Problem z przykładami/kodem","samplesCodeIssue","thumb-down"],["Inne","otherDown","thumb-down"]],["Ostatnia aktualizacja: 2025-08-31 UTC."],[[["\u003cp\u003eThis code snippet demonstrates how to build a Fitness API client to read aggregate step count data.\u003c/p\u003e\n"],["\u003cp\u003eIt highlights the importance of requesting only necessary data types to improve user consent rates.\u003c/p\u003e\n"],["\u003cp\u003eThe example guides developers through creating \u003ccode\u003eFitnessOptions\u003c/code\u003e, handling user authorization, and accessing historical fitness data using \u003ccode\u003eHistoryClient\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eIt showcases how to query and retrieve aggregate step count data within a specified time frame.\u003c/p\u003e\n"]]],[],null,[]]