У меня есть класс 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, почему?