Распознавание активности Android не работает с Nexus 5
У меня был рабочий код, использующий обновления распознавания активности Google. Теперь внезапно они, похоже, отправляют обновления либо несколько раз в секунду, либо никогда, хотя запрашиваются каждые 20 секунд. Я не изменил код и проверил более ранние версии, но получил ту же проблему.
Я создал минимальный пример из учебника, но также не получаю обновлений активности с моим устройством Nexus 5. С моим HTC Desire (MildWild 5.0 на базе Android 2.3.7) он работает отлично. Я подозревал, что Google Play Services, но на обоих телефонах установлена версия 4.2.43
Основная деятельность:
package com.example.testactivities;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.location.ActivityRecognitionClient;
public class MainActivity extends FragmentActivity implements
ConnectionCallbacks, OnConnectionFailedListener {
// Constants that define the activity detection interval
public static final int MILLISECONDS_PER_SECOND = 1000;
public static final int DETECTION_INTERVAL_SECONDS = 1;
public static final int DETECTION_INTERVAL_MILLISECONDS =
MILLISECONDS_PER_SECOND * DETECTION_INTERVAL_SECONDS;
/*
* Store the PendingIntent used to send activity recognition events
* back to the app
*/
private PendingIntent mActivityRecognitionPendingIntent;
// Store the current activity recognition client
private ActivityRecognitionClient mActivityRecognitionClient;
// Flag that indicates if a request is underway.
private boolean mInProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* Instantiate a new activity recognition client. Since the
* parent Activity implements the connection listener and
* connection failure listener, the constructor uses "this"
* to specify the values of those parameters.
*/
mActivityRecognitionClient =
new ActivityRecognitionClient(this, this, this);
/*
* Create the PendingIntent that Location Services uses
* to send activity recognition updates back to this app.
*/
Intent intent = new Intent(
this.getApplicationContext(), ActivityRecognitionIntentService.class);
/*
* Return a PendingIntent that starts the IntentService.
*/
mActivityRecognitionPendingIntent =
PendingIntent.getService(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Start with the request flag set to false
mInProgress = false;
this.startUpdates();
}
/**
* Request activity recognition updates based on the current
* detection interval.
*
*/
public void startUpdates() {
// If a request is not already underway
if (!mInProgress) {
// Indicate that a request is in progress
mInProgress = true;
// Request a connection to Location Services
mActivityRecognitionClient.connect();
//
} else {
/*
* A request is already underway. You can handle
* this situation by disconnecting the client,
* re-setting the flag, and then re-trying the
* request.
*/
}
}
/*
* Called by Location Services once the location client is connected.
*
* Continue by requesting activity updates.
*/
@Override
public void onConnected(Bundle dataBundle) {
Log.v("EXAMPLE","connected");
/*
* Request activity recognition updates using the preset
* detection interval and PendingIntent. This call is
* synchronous.
*/
mActivityRecognitionClient.requestActivityUpdates(
DETECTION_INTERVAL_MILLISECONDS,
mActivityRecognitionPendingIntent);
/*
* Since the preceding call is synchronous, turn off the
* in progress flag and disconnect the client
*/
mInProgress = false;
mActivityRecognitionClient.disconnect();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
Log.v("EXAMPLE","Connection failed");
}
@Override
public void onDisconnected() {
Log.v("EXAMPLE","Disconnected");
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testactivities"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="19" />
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="com.example.testactivities.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.example.testactivities.ActivityRecognitionIntentService"/>
</application>
</manifest>
RecognitionIntentService:
package com.example.testactivities;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
public class ActivityRecognitionIntentService extends IntentService {
public ActivityRecognitionIntentService() {
super("ActivityRecognitionIntent");
Log.v("EXAMPLE","constructor");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.v("EXAMPLE","new activity update");
}
}
ОБНОВЛЕНИЕ: я полагаю, что canopi определил правильную проблему. Когда устройство все еще с высокой степенью достоверности, оно вообще не запускает функции распознавания. Это было определенно иначе год назад (на других устройствах). Чтобы определить, работает ли устройство, я регистрирую последнее входящее действие с его отметкой времени и периодически проверяю это. Старая временная метка сигнализирует, что устройство все еще было в течение этого времени.