我在一类 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 中,似乎“广播”的 Toast 没有出现,我试图理解为什么不查看日志文件也似乎 ActivityRecognitionService.java 没有被 Pinger.java 调用。请告知,我试图弄清楚一旦我能做到这一点,然后我想我可以从我设置的广播服务中读取活动。