Search in sources :

Example 1 with DownloadService

use of zlc.season.rxdownload2.function.DownloadService in project RxDownload by ssseasonnn.

the class RxDownload method startBindServiceAndDo.

/**
     * start and bind service.
     *
     * @param callback Called when service connected.
     */
private void startBindServiceAndDo(final ServiceConnectedCallback callback) {
    Intent intent = new Intent(context, DownloadService.class);
    intent.putExtra(DownloadService.INTENT_KEY, maxDownloadNumber);
    context.startService(intent);
    context.bindService(intent, new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder binder) {
            DownloadService.DownloadBinder downloadBinder = (DownloadService.DownloadBinder) binder;
            downloadService = downloadBinder.getService();
            context.unbindService(this);
            bound = true;
            callback.call();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            //注意!!这个方法只会在系统杀掉Service时才会调用!!
            bound = false;
        }
    }, Context.BIND_AUTO_CREATE);
}
Also used : ServiceConnection(android.content.ServiceConnection) IBinder(android.os.IBinder) Intent(android.content.Intent) ComponentName(android.content.ComponentName) DownloadService(zlc.season.rxdownload2.function.DownloadService)

Aggregations

ComponentName (android.content.ComponentName)1 Intent (android.content.Intent)1 ServiceConnection (android.content.ServiceConnection)1 IBinder (android.os.IBinder)1 DownloadService (zlc.season.rxdownload2.function.DownloadService)1