免責事項:これは、電話がスタンバイ(画面オフ状態)になるとアクティビティ認識が更新の受信を停止するのと同じではありません。私はすでにそのアプローチを使用しており、役に立たないためです。
問題:
デバイスがスリープ状態になると、Activity Recognition サービスが動作を停止し、更新を送信していないようです。デバイスがスリープしていないときは正常に動作しますが (認識されたアクティビティは sqlite に保存されます)、HTC M8 の電源ボタンを押すとすぐに、デバイスを起動して電源ボタンをもう一度押すまで何も保存されません。
私が活動認識をどのように行っているかについての詳細:
アクティビティ認識の更新をリクエストすると、次のようになります (公式の Google サンプルから取得しましたが、に変更IntentService
しましたBroadcastReceiver
)。
public class DevActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
//...
private PendingIntent getActivityDetectionPendingIntent() {
Intent intent = new Intent(this, ActRecReceiver.class);
return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}
public void requestActivityUpdatesButtonHandler(View view) {
if (!googleApiClient.isConnected()) {
Toast.makeText(this, "not_connected", Toast.LENGTH_SHORT).show();
return;
}
final Preferences pref = new Preferences(this);
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(
googleApiClient,
pref.getActRecInterval(0),
getActivityDetectionPendingIntent()
).setResultCallback(this);
}
}
アクティビティ認識アップデートの受け取りはこんな感じ(標準WakefulBroadcastReceiver
+IntentService
連携)
public class ActRecReceiver extends WakefulBroadcastReceiver {
private static final String TAG = "ActRecReceiver";
private static final boolean LOG = BuildConfig.LOGGING;
@Override
public void onReceive(Context context, Intent intent) {
if (LOG) Log.d(TAG, "onReceive");
Intent serviceStarter = new Intent(context, DetectedActivitiesIntentService.class);
serviceStarter.putExtra("ActivityRecognitionResult", ActivityRecognitionResult.extractResult(intent));
startWakefulService(context, serviceStarter);
}
}
public class DetectedActivitiesIntentService extends IntentService {
protected static final String TAG = "DetectedActivitiesIS";
public DetectedActivitiesIntentService() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
ActivityRecognitionResult result = intent.getParcelableExtra("ActivityRecognitionResult");
List<DetectedActivity> detectedActivities = result.getProbableActivities();
HashMap<Integer, Integer> detectedActivitiesMap = new HashMap<>();
for (DetectedActivity activity : detectedActivities) {
detectedActivitiesMap.put(activity.getType(), activity.getConfidence());
}
storeEventToSqlite(detectedActivitiesMap);
ActRecReceiver.completeWakefulIntent(intent);
}
}
私は使用していますcompile 'com.google.android.gms:play-services-location:8.1.0'
EDIT1:
ユーザー bjiang からのヒントとこの回答https://stackoverflow.com/a/32965481/2401535を読んだ後、アクティビティ認識 API ドキュメントのように機能しないため、HTC デバイスに問題があるようです:
バッテリーを節約するために、デバイスが長時間「STILL」の状態にあると、アクティビティ レポートが停止する場合があります。デバイスが再び移動すると再開されます。これは、Sensor.TYPE_SIGNIFICANT_MOTION ハードウェアをサポートするデバイスでのみ発生します。
HTC M8 は実際にサポートしSensor.TYPE_SIGNIFICANT_MOTION
ているため、Activity Recognition は の後に再度開始する必要がありdevice moves again
ます。しかし、そうではありません。電源ボタンでデバイスを起動した後、アクティビティ認識を開始します。