1つのクラスPinger.javaに次のコードがあります
public class Pinger extends Service implements LocationListener, GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
@Override
public void onCreate() {
// start the API for recognition
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addApi(ActivityRecognition.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
// other code here
@Override
public void onConnected(Bundle bundle) {
Intent acr_intent = new Intent(this, ActivityRecognitionService.class);
PendingIntent pIntent = PendingIntent.getService(this, 0, acr_intent, PendingIntent.FLAG_UPDATE_CURRENT);
Toast.makeText(this, "Connection!", Toast.LENGTH_SHORT).show();
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mGoogleApiClient, 0, pIntent);
}
}
私の ActivityRecognitionService.java は次のようになります。 public class ActivityRecognitionService extends IntentService {
private String TAG = this.getClass().getSimpleName();
public ActivityRecognitionService() {
super("My Activity Recognition Service");
}
@Override
protected void onHandleIntent(Intent intent) {
if (ActivityRecognitionResult.hasResult(intent)) {
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
Log.i("AR", getType(result.getMostProbableActivity().getType()) + "t" + result.getMostProbableActivity().getConfidence());
Intent i = new Intent("com.xyz.xyz.ACTIVITY_RECOGNITION_DATA");
i.putExtra("Activity", getType(result.getMostProbableActivity().getType()));
i.putExtra("Confidence", result.getMostProbableActivity().getConfidence());
Toast.makeText(this, "Broadcast!", Toast.LENGTH_SHORT).show();
sendBroadcast(i);
}
}
private String getType(int type) {
if (type == DetectedActivity.UNKNOWN)
return "Unknown";
else if (type == DetectedActivity.IN_VEHICLE)
return "In Vehicle";
else if (type == DetectedActivity.ON_BICYCLE)
return "On Bicycle";
else if (type == DetectedActivity.ON_FOOT)
return "On Foot";
else if (type == DetectedActivity.STILL)
return "Still";
else if (type == DetectedActivity.TILTING)
return "Tilting";
else
return "";
}
ActivityRecognitionService.java では、「Broadcast」のトーストが表示されないようです。ログ ファイルを見ていない理由を理解しようとしていますが、ActivityRecognitionService.java が Pinger.java によって呼び出されていないようです。アドバイスしてください。これができるようになったら、設定したブロードキャスト サービスからアクティビティを読み取ることができると思います。