我有一个IntentService类:
public class PSMotionService extends IntentService {
像这样设置清单:
<service
android:name=".core.tracking.PSMotionService"
android:permission="com.google.android.gms.permission.ACTIVITY_RECOGNITION"></service>
这是我的转换:
transitions.clear();
ArrayList<Integer> activities = new ArrayList<>(Arrays.asList(
DetectedActivity.STILL,
DetectedActivity.WALKING,
DetectedActivity.ON_FOOT,
DetectedActivity.RUNNING,
DetectedActivity.ON_BICYCLE,
DetectedActivity.IN_VEHICLE));
for (int activity :
activities) {
transitions.add(new ActivityTransition.Builder()
.setActivityType(activity)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_ENTER).build());
transitions.add(new ActivityTransition.Builder()
.setActivityType(activity)
.setActivityTransition(ActivityTransition.ACTIVITY_TRANSITION_EXIT).build());
}
这是我激活它的方式:
/**
* Will start updating motions.
*/
public void startUpdatingMotion() {
Log.i(TAG, "Motion startUpdatingMotion");
try {
ActivityTransitionRequest request = new ActivityTransitionRequest(transitions);
Task<Void> task =
ActivityRecognition.getClient(context).requestActivityTransitionUpdates(request, getActivityDetectionPendingIntent());
task.addOnSuccessListener(
new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void result) {
Utils.appendLog("Motion PSMotionService onConnected called", "I", Constants.MOTIONS);
// Handle success
}
}
);
task.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Utils.appendLog("Motion PSMotionService Connection failed: ConnectionResult.getErrorCode() = " + e.getMessage(), "E", Constants.MOTIONS);
// Handle error
}
}
);
} catch (Exception e) {
Utils.appendLog("Motion Service: Error startUpdatingMotion:" + e.getMessage(), "E", Constants.MOTIONS);
}
}
停止:
/**
* Will stop updating motions.
*/
public void stopUpdatingMotion() {
Log.i(TAG, "Motion stopUpdatingMotion");
try {
ActivityRecognition.getClient(context).removeActivityUpdates(getActivityDetectionPendingIntent());
} catch (Exception e) {
Utils.appendLog("Motion Service: Error stopUpdatingMotion:" + e.getMessage(), "E", Constants.MOTIONS);
}
}
待定意图:
/**
* Will create the pending Intent needed for the MotionService
*/
private PendingIntent getActivityDetectionPendingIntent() {
Log.i(TAG, "getActivityDetectionPendingIntent");
// Reuse the PendingIntent if we already have it.
if (activityDetectionPendingIntent != null) {
return activityDetectionPendingIntent;
}
Intent intent = new Intent(context, PSMotionService.class);
// FLAG_UPDATE_CURRENT is needed to reuse the same PendingIntent
activityDetectionPendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return activityDetectionPendingIntent;
}
还有我的onHandleIntent:
@Override
protected void onHandleIntent(Intent intent) {
Log.i(TAG, "Motion onHandleIntent");
}
但是我从来没有调用过onHandleIntent,为什么?