Ask Your Question
3

Using default BaseLoaderCallback in an android service

asked 2013-06-05 12:47:11 -0600

variablexxx gravatar image

0 down vote favorite

I want to use OpenCV in an android service. In the OpenCV documentation I found the following information:

Default BaseLoaderCallback implementation treats application context as Activity and calls Activity.finish() method to exit in case of initialization failure. To override this behavior you need to override finish() method of BaseLoaderCallback class and implement your own finalization method.

But now I don't now how I should implement my own finalization method. What should I do in this method?

And another question: Where should I place the following line of code:

OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_4, this, mOpenCVCallBack)

In an android activity this line of code should be in the onResume(), but in an android service I don't an onResume() method.

edit retag flag offensive close merge delete

Comments

@variablexxx - Did you figure out how to do this?

Torcellite gravatar imageTorcellite ( 2013-07-02 11:46:51 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-07-02 13:38:24 -0600

Torcellite gravatar image

updated 2013-07-02 22:26:32 -0600

Well, I seem to have figured it out.

You need to call initAsync in onStartCommand

if(OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_4, getApplicationContext(), mLoaderCallback)) {
        Log.i(TAG, "Loaded OpenCV");
    }
    else
        Log.i(TAG, "Couldn't load OpenCV");

Wait about 1.5 seconds before you use OpenCV libraries for them to be loaded. You could use a delayed runnable.

Also make sure you override the finish() function of the BaseLoaderCallback.

BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(
        WDMRService.this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
        case LoaderCallbackInterface.SUCCESS: {
            Log.i("", "OpenCV loaded successfully");
        }
            break;
        default: {
            super.onManagerConnected(status);
        }
            break;
        }
    }

    @Override
    public void finish() {
        // implementing own finish method since openCV treats context as
        // activity
    }
};

If you're not going to use OpenCV once the service is destroyed, you should unbind the OpenCV service in finish(). It will work even if you don't, but it is better if you do. I don't because I require OpenCV after the service is destroyed too.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2013-06-05 12:47:11 -0600

Seen: 2,930 times

Last updated: Jul 02 '13