Bạn có thể tìm thấy dự án mẫu mà tôi đã tạo ở đây.
https://github.com/cyfung/ActivityRecognitionSample
Lưu ý quan trọng: bạn có thể không nhận được dữ liệu như thường xuyên như bạn yêu cầu!
Bắt đầu từ năm API 21, các hoạt động có thể được nhận ít thường xuyên hơn tham số detectionIntervalMillis nếu điện thoại đang ở chế độ tiết kiệm điện năng và màn hình tắt.
thành phần chính:
Tạo GoogleApiClient trong onCreate
mGoogleApiClient =
new GoogleApiClient.Builder(this).addApi(ActivityRecognition.API)
.addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
Kết nối và ngắt kết nối khách hàng api trong onStart
và onStop
như đề xuất trong tài liệu Google Api.
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
mStatusView.setText("connecting");
}
@Override
protected void onStop() {
super.onStop();
mGoogleApiClient.disconnect();
mStatusView.setText("disconnected");
}
Bắt đầu nhận dạng hoạt động (không nên gọi trước khi kết nối Google Api). Sử dụng PendingIntent.getService
để tạo mục đích đang chờ xử lý dưới dạng gọi lại.
final PendingResult<Status>
statusPendingResult =
ActivityRecognition.ActivityRecognitionApi
.requestActivityUpdates(mGoogleApiClient, DETECT_INTERVAL, PendingIntent
.getService(this, 0, new Intent(this, ActivityDetectionService.class),
PendingIntent.FLAG_UPDATE_CURRENT));
statusPendingResult.setResultCallback(this);
IntentService
là phương pháp tiêu chuẩn được đề xuất cho callback
public class ActivityDetectionService extends IntentService {
protected static final String TAG = "activityDetectionService";
public ActivityDetectionService() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
final ActivityRecognitionResult
activityRecognitionResult =
ActivityRecognitionResult.extractResult(intent);
if (activityRecognitionResult == null) {
return;
}
//process the result here, pass the data needed to the broadcast
// e.g. you may want to use activityRecognitionResult.getMostProbableActivity(); instead
final List<DetectedActivity>
probableActivities =
activityRecognitionResult.getProbableActivities();
sendBroadcast(MainActivity.newBroadcastIntent(probableActivities));
}
}
Đăng ký dịch vụ trong biểu hiện.
<service
android:name=".ActivityDetectionService"
android:exported="false">
</service>
Để sử dụng API, bạn cũng cần thêm các thông tin sau vào tệp kê khai.
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
Để lấy lại dữ liệu đến các hoạt động tôi đã sử dụng một BroadcastReceiver tạo ra trong onCreate
mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
...
}
}
Đăng ký và unregister trong onResume
và tương ứng.
@Override
protected void onResume() {
super.onResume();
registerReceiver(mBroadcastReceiver, newBroadcastIntentFilter());
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mBroadcastReceiver);
}
đoạn mã chúng ta sẽ hiểu rõ hơn. – dhams
đã đăng/chỉnh sửa .. – steve
Bạn nên chỉnh sửa tiêu đề và nội dung của câu hỏi để cho biết rằng "hoạt động" không đề cập đến "android.app.Activity", mà là chỉnh sửa "chuỗi hoạt động thể dục". –