Aplicaciones ahora pueden saber no sólo donde estamos, sino qué estamos haciendo y reaccionar acorde a esto.
Aplicaciones más inteligentes (y proactivas).
Mejor interacción.
Usuarios más felices :D
Una aplicación para corredores puede saber cuándo dejamos de correr y animarnos a seguir corriendo.
Podemos tener recordatorios basados en nuestras actividades.
0. Instalar Google Play Services SDK
Window > Android SDK Manager
Extras > Google Play Services SDK > Install > Accept License > Install
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>
- Instalable por el usuario.- Disponible en el Play Store.
GooglePlayServicesUtil.isGooglePlayServicesAvailable(...);
- Periódicamente cada n segundos.- Recibidas por un servicio.
activityRecognitionClient.requestActivityUpdates(...);
public class ActivityRecognitionService extends IntentService{
protected void onHandleIntent(Intent intent){
...
}
}
DetectedActivity.IN_VEHICLE
DetectedActivity.ON_BICYCLE
DetectedActivity.ON_FOOT
DetectedActivity.TILTING
DetectedActivity.STILL
DetectedActivity.UNKNOWN
activityRecognitionClient.removeActivityUpdates(...);
public boolean servicesAvailable(Context c){
int res;
res = GooglePlayServicesUtil.isGooglePlayServicesAvailable(c);
if(res == ConnectionResult.SUCCESS){ return true;
}else{
Dialog dialog;
dialog = GooglePlayServicesUtil.getErrorDialog(res, c, 0);
if(dialog != null){
dialog.show();
}
return false;
}
}
public class ActivityRecognitionManager
implements ConnectionCallbacks,
OnConnectionFailedListener{
public void onConnected(){
}
public void onDisconnected(){
}
public void onConnectionFailed(){
}
}
public ActivityRecognitionManager(MainActivity c){
this.context = c;
this.connecting = false;
this.client = new ActivityRecognitionClient(c, this, this);
Intent i = new Intent(c, ARIntentService.class);
this.serviceIntent = PendingIntent.getService(c, 0, i,...);
...
}
public void connect(){
if(servicesAvailable()){
if(!connecting){
connecting = true;
client.connect();
} }
}
public void onConnected(Bundle connectionHint){ connecting = false;
if(requestUpdates){
int lapse = 1000 * 30; // 30s client.requestActivityUpdates(lapse, serviceIntent);
}else{ client.removeActivityUpdates(serviceIntent);
}
client.disconnect(); }
public void onDisconnected(){ this.connecting = false;
... }
public class ActivityRecognitionIntentService extends IntentService{
public ActivityRecognitionIntentService(){ super("ActivityRecognitionService"); }
protected void onHandleIntent(Intent i){
...
}
}
protected void onHandleIntent(Intent i){
ActivityRecognitionResult r;
r = ActivityRecognitionResult.extractResult(i);
DetectedActivity activity = r.getMostProbableActivity();
int type = activity.getType();
int confidence = activity.getConfidence();
...
...
}