Android - 以正确的方式启动后台服务

时间:2018-03-19 15:15:08

标签: android service background

我已经在网上阅读了有关服务和许多示例的文档。但是,大多数示例只包含相同的代码,我仍然完全不了解后台服务的生命周期。

这是我正在尝试做的事情:

  • 开始活动
      

    活动启动服务,接收位置数据

  • 退出活动
      

    服务不断收集

  • 开始活动
      

    绑定服务并做一些事情,例如显示一些结果

这就是我为实现这一目标而做的事情:

  • 实施服务(非IntentService)

  • 尝试以两种方式启动它:

    1. 通过绑定来启动服务:

      bindService(intent, myLocationService, Context.BIND_AUTO_CREATE);

      当我调用unbindService(...),例如,当调用unbindService(...)时,这会导致在活动开始和beeing销毁时创建服务。在活动的onStop()方法中。

    2. 通过明确创建服务来启动服务:

      Intent intent = new Intent(this, MyLocationService.class);
      startService(intent);

      这会导致在活动开始时创建服务。退出活动时,服务崩溃(尽管我已经实施了所有清理工作)并重新启动。再次启动活动时,服务也会再次启动。


 那么,我怎样才能优雅地启动,绑定和取消绑定一个服务器,而不会出现所有崩溃和重启行为?

这些是服务的相关代码行:

public class MyLocationService extends Service {

    final IBinder myServiceBinder = new MyServiceBinder();

    public static class MyServiceConnection implements ServiceConnection {

        MyLocationService service;
        boolean bound = false;

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            MyServiceBinder binder = (MyServiceBinder)iBinder;
            service = binder.getService();
            bound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            bound = false;
        }

        public MyLocationService getService() {
            return service;
        }

        public boolean isBound() {
            return bound;
        }

    }

    /**
     * Binder for GPSService
     */
    public class MyServiceBinder extends Binder {
        MyLocationService getService() {
            return MyLocationService.this;
        }
    }

    public MyLocationService() {
    }

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


}

这些是活动的相关代码行:

public class MyMainActivity extends AppCompatActivity {

    MyLocationService.MyServiceConnection myServiceConnection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent(this, MyLocationService.class);
        startService(intent);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Intent intent = new Intent(this, MyLocationService.class);
        bindService(intent, myServiceConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unbindService(myServiceConnection);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }
}

1 个答案:

答案 0 :(得分:0)

https://developer.android.com/guide/components/activities/activity-lifecycle.html

根据您的要求,应该是onPause()onResume()或onStart()和onStop()。

如果您希望服务在后台运行,您可以使用

/**
     * Called when all clients have disconnected from a particular interface
     * published by the service.  The default implementation does nothing and
     * returns false.
     * 
     * @param intent The Intent that was used to bind to this service,
     * as given to {@link android.content.Context#bindService
     * Context.bindService}.  Note that any extras that were included with
     * the Intent at that point will <em>not</em> be seen here.
     * 
     * @return Return true if you would like to have the service's
     * {@link #onRebind} method later called when new clients bind to it.
     */ public boolean onUnbind(Intent intent) {
       return false;    }

onRebind()做点什么。