Android 如何为服务替换这些方法

Android 如何为服务替换这些方法,android,Android,我正在用安卓系统开发一项服务。我不知道如何用服务替换这些活动方法 @Override protected void onResume() { super.onResume(); checkPlayServices(); // Resuming the periodic location updates if (mGoogleApiClient.isConnected() && mRequestingLocationUpdates) {

我正在用安卓系统开发一项服务。我不知道如何用服务替换这些活动方法

@Override
protected void onResume() {
    super.onResume();

    checkPlayServices();

    // Resuming the periodic location updates
    if (mGoogleApiClient.isConnected() && mRequestingLocationUpdates) {
        startLocationUpdates();
    }
}

@Override
protected void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}
@Override
protected void onPause() {
    super.onPause();
    stopLocationUpdates();
}

请告诉我如何替换它们

这可能有助于您从活动中回调您的服务

在活动生命周期方法中写下这一点

Intent intent = new Intent();
intent.setAction("com.example.ON_RESUME");//change this for appropriate callback
sendBroadcast(intent);
像这样改变你的服务

class YourService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        //Do your stuff
        return null;
    }


    private void onResume() {
        //do your stuff
    }

    private void onStop() {
        //do your stuff
    }

    private void onPause() {
        //do your stuff
    }

    public static class ActivityLifeCycleReceiver extends BroadcastReceiver {

        public String ACTION_ON_RESUME = "com.example.ON_RESUME";
        public String ACTION_ON_STOP = "com.example.ON_STOP";
        public String ACTION_ON_PAUSE = "com.example.ON_PAUSE";

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (ACTION_ON_PAUSE.equals(action)) {
                onPause();
            } else if (ACTION_ON_RESUME.equals(action)) {
                onResume();
            } else if (ACTION_ON_STOP.equals(action)) {
                onResume();
            }
        }
    }
}
最后在清单中注册接收者

    <receiver android:name=".YourService$ActivityLifeCycleReceiver">
        <intent-filter >
            <action android:name="com.example.ON_RESUME"/>
            <action android:name="com.example.ON_STOP"/>
            <action android:name="com.example.ON_PAUSE"/>
        </intent-filter>
    </receiver>

您不需要“替换它们”。这些行动没有等价物。相反,考虑到应用程序的业务逻辑和
服务
API的性质,您可以在其他有意义的地方调用类似于
checkPlayServices()
的方法。