Service not binding

梦想的初衷 提交于 2021-02-10 20:33:36

问题


I have an android device with an integrated barcode scanner. I'm setting up the service as follows:

public class BarcodeService extends Service {
    private final LocalBinder binder = new LocalBinder();

    public class LocalBinder extends Binder {
        public BarcodeService getService() {
            return BarcodeService.this;
        }
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return binder;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        HandlerThread thread = new HandlerThread("ServiceStartArguments");
        thread.start();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //Get scanner
    }
}

The service is also in the AndroidManifest.xml. The class that makes use of this service is:

public class BarcodeReader extends Activity {
    private BarcodeService barcodeService;
    private boolean isBound = false;

    private ServiceConnection barcodeServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            barcodeService = ((BarcodeService.LocalBinder)service).getService();
            isBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            barcodeService = null;
            isBound = false;
        }
    };

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

        if (!isBound) {
            Intent intent = new Intent(this, BarcodeService.class);
            startService(intent);
            bindService(intent, barcodeServiceConnection, Context.BIND_AUTO_CREATE);
        }
    }

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

        if (isBound) {
            unbindService(barcodeServiceConnection);
        }
    }
}

However the service is not binding, ie. barcodeService is always null. The code never reaches onServiceConnected.

What am I missing? And is it necessary to use a class that extends Activity?


回答1:


Common Android Service troubleshooting

Just some general remarks and stuff to check if your service is not starting.

Service class defined in Manifest

Common mistake is not to have the service in manifest (android doesn't warn you about that) or have it there but misspelled the class name.

<manifest ... >
  ...
  <application ... >
      <service android:name=".ExampleService" />
      ...
  </application>
</manifest>

Or you might have it in the manifest (or one of the manifests) but the final manifest after merging that is used within the apk doesn't have the service definition. For that check:

project_folder/app_folder/build/intermediates/manifests/full/...

A project clean and rebuild might help.

Check bindService return value

When debugging check the boolean return value on the bindService call to see if service was started successfully or not.

Debug Activity and Service implementation

Also the service might be running but not bind or might not execute anything hence have no visual effect that it's running in the background. For that use the debugger on both the bound Activity and the Service itself.

Check onBind, onStartCommand in Service class or even the onCreate there.

In Activity check bindService, ServiceConnection and so.

Resources

also check https://developer.android.com/guide/components/services.html



来源:https://stackoverflow.com/questions/31339024/service-not-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!