android - java.lang.IllegalArgumentException : Service Intent must be explicit: Intent

标签 android android-intent android-5.0-lollipop illegalargumentexception microsoft-band

我正在尝试将我的 Android 应用程序连接到 MS Band,但收到以下错误:

java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.microsoft.band.service.action.BIND_BAND_SERVICE }

我使用的代码 fragment 如下:

private View.OnClickListener mButtonConnectClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View button) {

        pairedBands = BandClientManager.getInstance().getPairedBands();
        bandClient = BandClientManager.getInstance().create(getApplicationContext(), pairedBands[0]);

        // Connect must be called on a background thread.
         new ConnectTask().execute();
        }

};


private class ConnectTask extends AsyncTask<BandClient, Void, Void> {
    @Override
    protected Void doInBackground(BandClient... clientParams) {

            BandPendingResult<ConnectionResult> pendingResult = null;
        try {
            pendingResult = bandClient.connect();
        } catch (BandIOException e) {
            e.printStackTrace();
        }

        try {
            ConnectionResult result = pendingResult.await();
            if(result == ConnectionResult.OK) {

                BandConnection.setText("Connection Worked");

            } else {
                BandConnection.setText("Connection Failed");
            }
        } catch(InterruptedException ex) {
            // handle InterruptedException
        } catch(BandException ex) {
            // handle BandException
        }
        return null;
    }

    protected void onPostExecute(Void result) {

    }
}

我正在遵循 Microsft Band SDK 中给出的示例,我对此很陌生。任何帮助将不胜感激。

最佳答案

尽管@tyczj 的评论绝对正确,但它缺少解决方法。我将尝试在此答案中描述问题、其根源和(临时)解决方法。

我们首先要找出问题所在。在 Band SDK 内部的某个地方,正在发生这样的调用:

Intent service = new Intent("com.microsoft.band.service.action.BIND_BAND_SERVICE");
context.bindService(service, connections, flags);

创建一个隐式 Intent 并用于绑定(bind)到服务。不幸的是,从 Android 5.0 开始,不再允许使用隐式 Intent 绑定(bind)到服务 ( as described in the API changes here )。相反, Intent 应该明确

由于上述情况发生在混淆的 Band SDK 内部,并且我们没有其源代码,因此“更新”代码以使用显式 Intent 并非易事。如果您足够精明,您可能可以更改 Java 字节代码来执行此操作,但我想提出一种不同的解决方法:

由于异常是由 bindService() 引发的,并且 Intent 是其参数之一,因此我们可以重写该方法以确保提供的任何 Intent 最终都是显式的 em>.

例如:下面的代码可确保以 com.microsoft.band 开头的操作(包括但不限于 com.microsoft.band. service.action.BIND_BAND_SERVICE),通过设置包名称来明确。

ContextWrapper bandContextWrapper = new ContextWrapper(getApplicationContext()) {
    @Override public boolean bindService(Intent service, ServiceConnection conn, int flags) {
        final String action = service.getAction();
        if (!TextUtils.isEmpty(action) && action.startsWith("com.microsoft.band")) {
            service.setPackage("com.microsoft.band");
        }
        return super.bindService(service, conn, flags);
    }
};

您现在可以使用此包装器拦截有问题的调用,并通过将其传递给 BandClientManager 作为启动服务的“上下文”来更新 Intent (如果需要)。就像这样:

bandClient = BandClientManager.getInstance().create(bandContextWrapper, pairedBands[0]);

现在您不应再收到IllegalArgumentException。我自己没有 Microsoft Band,因此不能保证此后一切都能正常工作,但它至少应该解决您问题中概述的问题。

请注意,这只能被视为临时解决方法。正如其中一条评论所指出的,微软确实应该更新其 SDK 以符合 Android 5+。这是唯一正确的解决方案。在那之前,希望这会有所帮助。

关于android - java.lang.IllegalArgumentException : Service Intent must be explicit: Intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29546158/

相关文章:

java - 允许用户从画廊中选择图像以上传到 Activity 中的 ImageView ?

java - 当我尝试从 Intent putExtra 时,出现空指针异常

java - Android 5,camera2 只使用闪光灯

android - 如何更改 ANDROID API 22 中的默认向上按钮图标

java - 如何避免 Java Android Studio 中的 EXP 表示法

c# - Xamarin 自定义 DatePicker 失去绑定(bind)

java - 除非连接在超时前关闭,否则 Android 套接字读取不起作用

java - 在 android 中使用 php-cgi 和网络服务器

android - 如何完成除第一个 Activity 之外的所有 Activity ?

android - 在 SDK 中看不到 android L