如果应用程式关闭,Android ActivityRecognitionClient会停止回呼至IntentService

时间:2018-07-17 10:20:19

标签: android

下面是我的服务,应在其中接收来自活动识别客户端的回调。我试图将Service更改为IntentService,因为Service消耗大量电池(这是我的主要担心),并且当应用程序在前台运行时,一切工作正常。但是,如果我关闭了该应用程序,那么客户端将不再回调IntentService,所以我的主要问题是“即使关闭了该应用程序,我如何仍可以通过使用IntentService继续接收来自客户端的回调(即,检测到的活动)?”以及“如果不是要使用IntentService,那么在仍然使用Service或其他方法的同时节省电池的最佳方法是什么?”

public class SensingActivity extends Service {

    public static final String ACTIVITY_BROADCAST_NAME = "ACTIVITY_BROADCAST";
    public static final String ACTIVITY_BROADCAST_TYPE = "ACTIVITY_TYPE";
    public static final String ACTIVITY_BROADCAST_CONFIDENCE = "ACTIVITY_CONFIDENCE";
    public static final String NOTIFICATION_CHANNEL_ID = "background_sensing_foreground_service";
    public static final int NOTIFICATION_ID = 6;

    private static Context context;
    private static SensingActivity instance;

    @Override
    public void onCreate() {
        super.onCreate();
        this.context = getApplicationContext();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        if (ActivityRecognitionResult.hasResult(intent)) {
            ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
            DetectedActivity mostLikelyActivity = result.getMostProbableActivity();
            onHandlingActivity(mostLikelyActivity);
        }

        return START_STICKY;
    }

    //@RequiresApi(api = Build.VERSION_CODES.O)
    private void onHandlingActivity(DetectedActivity mostProbableActivity) {
        // log the activity detection
        LoggingDatabaseManager database = LoggingDatabaseManager.getInstance(this);
        database.recordActivityDetection(mostProbableActivity.getType(), mostProbableActivity.getConfidence());

        // send result to broadcast receiver
        Intent intent = new Intent(ACTIVITY_BROADCAST_NAME);
        intent.putExtra(ACTIVITY_BROADCAST_TYPE, mostProbableActivity.getType());
        intent.putExtra(ACTIVITY_BROADCAST_CONFIDENCE, mostProbableActivity.getConfidence());
        intent.setClass(this, SensingBroadcastReceiver.class);
        sendBroadcast(intent);
    }

这是我设置客户端的方法:

public class SensingActivitySetup implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private final Context context;
private GoogleApiClient googleAPIClient;

private static SensingActivitySetup instance;
private ActivityRecognitionClient activityRecognitionClient;

private SensingActivitySetup(Context context) {
    this.context = context;
}

public static SensingActivitySetup getInstance(Context context) {
    if (instance == null) {
        instance = new SensingActivitySetup(context);
    }
    return instance;
}

public ActivityRecognitionClient getActivityRecognitionClient() {
    return activityRecognitionClient;
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    enableActivityUpdates();
}

public void startTracking() {
    googleAPIClient = new GoogleApiClient.Builder(context)
            .addApi(ActivityRecognition.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    googleAPIClient.connect();

    // Verify that the Google Play Services can be connected to.
    checkGooglePlayServicesAvailable(this);
}

private boolean checkGooglePlayServicesAvailable(SensingActivitySetup sensingActivitySetup) {
    GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();

    int result = googleAPI.isGooglePlayServicesAvailable(context);
    if (result != ConnectionResult.SUCCESS) {
        return false;
    }
    return true;
}

public void enableActivityUpdates() {
    UserCurrentStage userCurrentStage = UserCurrentStage.getInstance(context);
    Intent intent = new Intent(context, SensingActivity.class);
    PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    activityRecognitionClient = ActivityRecognition.getClient(context);
    activityRecognitionClient.requestActivityUpdates(userCurrentStage.getSensingInterval()*60*1000, pendingIntent);
}

@Override
public void onConnectionSuspended(int i) {}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Log.e("ConnectionFailed", connectionResult.getErrorCode()+","+connectionResult.getErrorMessage());
}

}

0 个答案:

没有答案