【问题标题】:java.lang.IllegalArgumentException: Service Intent must be explicit: Intentjava.lang.IllegalArgumentException:服务意图必须是明确的:意图
【发布时间】:2015-04-09 18:27:10
【问题描述】:

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

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

我使用的代码sn-p如下:

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 中给出的示例进行操作,我对此并不陌生。任何帮助将不胜感激。

【问题讨论】:

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


【解决方案1】:

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

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

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

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

由于上述情况发生在混淆频段 SDK 的深处,而且我们没有它的来源,因此“更新”代码以使用明确的意图并非易事。如果您足够精明,您可能可以更改 Java 字节码来这样做,但我想提出一个不同的解决方法:

由于bindService() 引发了异常并且intent 是它的参数之一,我们可以重写该方法以确保提供的任何intent 最终都是显式。

例如:下面的代码确保任何以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。像这样:

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

现在您不应再收到IllegalArgumentException。我自己没有 Microsoft Band,所以不能保证在此之后一切都会完全正常,但它至少应该可以解决您的问题中列出的问题。

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

【讨论】:

  • 如果您改用 Health App 的包名称,则此方法有效:“com.microsoft.kapp”
  • @Matthew:是的,我应该更清楚地说明包名应该是应用程序包名,Intent 应该限制其解析范围。我使用的是 Band SDK 示例应用,这就是 com.microsoft.band 为我工作的原因。
  • 我想确认这非常有效。很棒的解决方案@MH
【解决方案2】:

如果有人使用 Xamarin.Android:

private class BandContextWrapper : ContextWrapper
{
    public BandContextWrapper(Context baseContext)
        : base (baseContext)
    {
    }

    public override bool BindService(Intent service, IServiceConnection conn, Bind flags)
    {
        var action = service.Action;
        if (!string.IsNullOrEmpty(action) && action.StartsWith("com.microsoft.band"))
        {
            service.SetPackage("com.microsoft.kapp");
        }
        return base.BindService(service, conn, flags);
    }
}

然后使用它:

var bandContextWrapper = new BandContextWrapper(Application.Context);
var client = BandClientManager.Instance.Create(bandContextWrapper, info);

这里我使用的是http://components.xamarin.com/view/microsoft-band-sdk

【讨论】:

    猜你喜欢
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-22
    • 1970-01-01
    相关资源
    最近更新 更多