Ask Your Question
2

Error Opencv4Android: Caused by: java.lang.IllegalArgumentException: Service Intent must be explicit

asked 2015-02-03 14:32:10 -0600

Uéliton Freitas gravatar image

Hi!!

I'm programming in Android Lollipop and i had this exception running OpenCV. Previously I ran the same program in JellyBean and worked properly. Anyone know how to fix this error?

Thanks, and sorry by the english.

02-03 17:15:09.340 11168-11168/com.example.ueliton.helloopencv E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.ueliton.helloopencv, PID: 11168 java.lang.RuntimeException: Unable to resume activity {com.example.ueliton.helloopencv/com.example.ueliton.helloopencv.MainActivity}: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=org.opencv.engine.BIND } at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2995) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3030) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2393) at android.app.ActivityThread.access$800(ActivityThread.java:148) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5312) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) Caused by: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=org.opencv.engine.BIND } at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:1808) at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1907) at android.app.ContextImpl.bindService(ContextImpl.java:1885) at android.content.ContextWrapper.bindService(ContextWrapper.java:538) at org.opencv.android.AsyncServiceHelper.initOpenCV(AsyncServiceHelper.java:24) at org.opencv.android.OpenCVLoader.initAsync(OpenCVLoader.java:85) at com.example.ueliton.helloopencv.MainActivity.onResume(MainActivity.java:49) at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1264) at android.app.Activity.performResume(Activity.java:6039) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2978)             at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3030)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2393)             at android.app.ActivityThread.access$800(ActivityThread.java:148)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292)             at android.os.Handler.dispatchMessage(Handler.java:102)             at android.os.Looper.loop(Looper.java:135)             at android.app.ActivityThread.main(ActivityThread.java:5312)             at java.lang.reflect.Method.invoke(Native Method)             at java.lang.reflect.Method.invoke(Method.java:372)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

edit retag flag offensive close merge delete

3 answers

Sort by » oldest newest most voted
1

answered 2015-02-14 11:46:59 -0600

updated 2015-02-14 11:48:46 -0600

The problem is explained here. Since Android 5.0 service intents must be explicit intents and the OpenCV library uses implicit intents in the AsyncServiceHelper.java (new Intent("org.opencv.engine.BIND")), so it doesn't work.

Changing the targetSDK is just a workaround for now, but eventually the source-code has to be changed in the future (Alex might know more). The solution would be to change the Method initOpenCV to something like this:

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

but since parts of OpenCV are built with the JNI, I don't know what to put there instead of the ??? because 'BIND' is just a placeholder it seems to me.

edit flag offensive delete link more

Comments

Hi, I am very new to android and opencv. Can you tell me how can i use this initOpenCV function in my MainActivity, please?

xfan1024 gravatar imagexfan1024 ( 2016-02-02 16:30:59 -0600 )edit

Open the AsyncServicehelper class in the library folder and you'll find the initOpenCv method. replace it with the one above. I also had to replace;

Intent explicitIntent = new Intent(AppContext, org.opencv.engine.???.class);

with

Intent intent = new Intent("org.opencv.engine.BIND");
intent.setPackage("org.opencv.engine");
chidi gravatar imagechidi ( 2016-10-16 20:51:06 -0600 )edit
1

answered 2015-02-06 00:35:42 -0600

Hi Uéliton Freitas,

In your AndroidManifest.xml if targetSDKVersion = 21 change to targetSDKVersion = 19

edit flag offensive delete link more
0

answered 2015-02-19 12:15:14 -0600

SimonH gravatar image

@Alexander I had the same idea, you can do it specifying the package to make the intent explicit:

Intent intent = new Intent("org.opencv.engine.BIND");
    intent.setPackage("org.opencv.engine");
    if (AppContext.bindService(intent, helper.mServiceConnection,
            Context.BIND_AUTO_CREATE)) {
        ....
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-02-03 14:32:10 -0600

Seen: 14,078 times

Last updated: Feb 19 '15