OpenCV Service Intent must be explicit, Android 5.0 Lollipop

扶醉桌前 提交于 2020-01-09 12:36:30

问题


I'm building this application for my bachelor's diploma that uses OpenCV. Everything was going fine until I updated my phone's Android to 5.0.

After the update my project stopped working, because of this:

java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=org.opencv.engine.BIND }

I have read and informed myself about the new restrictions regarding implicit intents in Android 5.0, but how can I get around this in order for OpenCV to work?

I could modify the AsyncServiceHelper.java file in the OpenCV SDK in order to try and fix this, but how could I get the Class object of the OpenCV service that needs to be run, in order to use an explicit intent?

Or maybe this approach is a dead end, but are there any other approaches to this, or are my only options either an update to the OpenCV SDK, or to downgrade the Android Version on my device?


回答1:


I think changing the android:targetSdkVersion is not a solution for very long ;) So instead I added the package name to make the intent explicit:

public static boolean initOpenCV(String Version, final Context AppContext,
        final LoaderCallbackInterface Callback) {
    AsyncServiceHelper helper = new AsyncServiceHelper(Version, AppContext,
            Callback);
    Intent intent = new Intent("org.opencv.engine.BIND");
    intent.setPackage("org.opencv.engine");
    if (AppContext.bindService(intent, helper.mServiceConnection,
            Context.BIND_AUTO_CREATE)) {
        return true;
    } else {
        AppContext.unbindService(helper.mServiceConnection);
        InstallService(AppContext, Callback);
        return false;
    }
}

Maybe someone can tell an opencv comitter about this, to push a hotfix.

EDIT: From a comment below: For anyone else wondering the location of this function, it's in src/main/java/org/opencv/android/AsyncServiceHelper.java




回答2:


I saw a answer here when at work, tested it and that solved the issue. Apparently it was deleted in the meanwhile. Posting it again for reference

The solution was changing: android:targetSdkVersion in the AndroidManifest.xml from 21 to 19. Can't believe it was this easy and I lost a day trying to figure it out, buy hey, thanks again to the one who posted the initial answer :)

Thanks stackoverflow!




回答3:


I've changed OpenCV version to 3.0 and the problem was solved:

From

OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_3, this, mLoaderCallbck);

To

OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallbck);



回答4:


In order to run application at any version of android i.e. latest version.

Remove tag - android:targetSdkVersion.



来源:https://stackoverflow.com/questions/27470313/opencv-service-intent-must-be-explicit-android-5-0-lollipop

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