服务不具有约束力 [英] Service not binding

查看:111
本文介绍了服务不具有约束力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有集成条形码扫描仪的android设备.我正在按以下步骤设置服务:

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
    }
}

该服务也位于 AndroidManifest.xml 中.使用此服务的类为:

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);
        }
    }
}

但是该服务未绑定,即 barcodeService 始终为null.该代码永远不会到达 onServiceConnected .

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

我想念什么?是否有必要使用扩展Activity的类?

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

推荐答案

常见的Android服务疑难解答

仅需一些一般性的说明和内容,即可检查您的服务是否未启动.

Common Android Service troubleshooting

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

常见的错误是不在清单中显示该服务(Android不会警告您)或在清单中包含该服务,但拼写了错误的类名.

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>

或者您可能将其包含在清单(或清单之一)中,但合并后的apk中使用的最终清单没有服务定义.对于该检查:

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.

调试时,请检查 bindService 调用上的布尔返回值,以查看服务是否成功启动.

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

此外,该服务可能正在运行,但未绑定或可能未执行任何操作,因此,它在后台运行没有视觉效果.为此,请在绑定的 Activity Service 本身上使用调试器.

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.

检查 Service 类中的 onBind onStartCommand ,甚至在那里的 onCreate .

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

活动中,检查 bindService ServiceConnection 等.

还要检查 https://developer.android.com/guide/components/services.html

这篇关于服务不具有约束力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆