Kayıt API'si, uygulamanızın abonelikler oluşturarak sensör verilerinin otomatik olarak pil gücü harcayacak şekilde depolanmasını istemesini sağlar. Abonelik, bir Android uygulamasıyla ilişkilendirilir ve bir fitness veri türü veya belirli bir veri kaynağından oluşur.
Uygulamanızdaki farklı veri türleri veya veri kaynakları için birden fazla abonelik oluşturabilirsiniz. Google Fit, uygulamanız çalışmıyorken bile etkin aboneliklerdeki fitness verilerini depolar ve sistem yeniden başlatıldığında bu abonelikleri geri yükler.
Kaydedilen veriler kullanıcının fitness geçmişinde mevcuttur. Uygulamanızdaki verileri gerçek zamanlı olarak göstermek istiyorsanız Kayıt API'siyle birlikte Sensors API'yi kullanın. Fitness verilerini oturum meta verileriyle kaydetmek için Sessions API'yi kullanın.
Fitness verilerine abone olma
Uygulamanızda sensör verilerinin arka planda toplanmasını istemek için aşağıdaki kod snippet'inde gösterildiği gibi RecordingClient.subscribe
yöntemini kullanın:
Kotlin
Fitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions)) // This example shows subscribing to a DataType, across all possible data // sources. Alternatively, a specific DataSource can be used. .subscribe(DataType.TYPE_STEP_COUNT_DELTA) .addOnSuccessListener { Log.i(TAG, "Successfully subscribed!") } .addOnFailureListener { e -> Log.w(TAG, "There was a problem subscribing.", e) }
Java
Fitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions)) // This example shows subscribing to a DataType, across all possible // data sources. Alternatively, a specific DataSource can be used. .subscribe(DataType.TYPE_STEP_COUNT_DELTA) .addOnSuccessListener(unused -> Log.i(TAG, "Successfully subscribed!")) .addOnFailureListener( e -> Log.w(TAG, "There was a problem subscribing.", e));
Abonelik başarıyla eklenirse Google Fit, fitness geçmişinde uygulamanızın adına TYPE_STEP_COUNT_DELTA
türündeki fitness verilerini depolar. Bu abonelik, uygulamanızın etkin abonelikler listesinde görünür.
Uygulamanızda daha fazla fitness verisi türüne abone olmak için bir önceki örnekteki adımları uygulayın ancak her defasında farklı bir fitness veri türü sağlayın.
Etkin abonelikleri listeleme
Uygulamanızla ilgili etkin aboneliklerin listesini almak için aşağıdaki kod snippet'inde gösterildiği gibi RecordingClient.listSubscriptions
yöntemini kullanın:
Kotlin
Fitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions)) .listSubscriptions() .addOnSuccessListener { subscriptions -> for (sc in subscriptions) { val dt = sc.dataType Log.i(TAG, "Active subscription for data type: ${dt.name}") } }
Java
Fitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions)) .listSubscriptions() .addOnSuccessListener(subscriptions -> { for (Subscription sc : subscriptions) { DataType dt = sc.getDataType(); Log.i(TAG, "Active subscription for data type: ${dt.name}"); } }); }
Fitness verileri aboneliğinden çık
Uygulamanızda sensör verilerinin toplanmasını durdurmak için aşağıdaki kod snippet'inde gösterildiği gibi RecordingClient.unsubscribe
yöntemini kullanın:
Kotlin
Fitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions)) // This example shows unsubscribing from a DataType. A DataSource should // be used where the subscription was to a DataSource. Alternatively, if // the client doesn't maintain its subscription information, they can use // an element from the return value of listSubscriptions(), which is of // type Subscription. .unsubscribe(DataType.TYPE_STEP_COUNT_DELTA) .addOnSuccessListener { Log.i(TAG,"Successfully unsubscribed.") } .addOnFailureListener { e-> Log.w(TAG, "Failed to unsubscribe.") // Retry the unsubscribe request. }
Java
Fitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions)) // This example shows unsubscribing from a DataType. A DataSource // should be used where the subscription was to a DataSource. // Alternatively, if the client doesn’t maintain its subscription // information, they can use an element from the return value of // listSubscriptions(), which is of type Subscription. .unsubscribe(DataType.TYPE_STEP_COUNT_DELTA) .addOnSuccessListener(unused -> Log.i(TAG,"Successfully unsubscribed.")) .addOnFailureListener(e -> { Log.w(TAG, "Failed to unsubscribe."); // Retry the unsubscribe request. }); }