gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 07:54:47 28 4
gpt4 key购买 nike

我正在尝试将我的 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 开始,不再允许绑定(bind)到使用隐式 Intent (as described in the API changes here) 的服务。相反, Intent 应该是明确的

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

由于异常是由 bindService() 抛出并且 Intent 是其参数之一,我们可以覆盖该方法以确保提供的任何 Intent 最终都是显式.

例如:下面的代码确保任何具有以 com.microsoft.band 开头的操作的 Intent (包括但不限于 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com