gpt4 book ai didi

java - API 23 (Android 6.0.1) 应用内计费 : Service Intent must be explicit: Intent

转载 作者:太空狗 更新时间:2023-10-29 14:00:40 26 4
gpt4 key购买 nike

我一直在四处寻找,这似乎是一个与 Android L 相关的错误,显然已经使用我已有的代码解决了。

当我尝试调用 bindService 时,我得到:

Fatal Exception: java.lang.RuntimeException: Unable to resume activity {MyActivity}: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.android.vending.billing.InAppBillingService.BINL }

这是导致崩溃的代码 fragment :

    final Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
serviceIntent.setPackage("com.android.vending");
activity.bindService(serviceIntent, this, Context.BIND_AUTO_CREATE);

它只发生在 Android 6.0.1 和我在 23 中的 gradle 目标上,我似乎无法理解 API 23 上的问题...

最佳答案

如果您正在使用 IabHelper 类。转到IabHelper.java 中的startSetup 方法。添加以下代码

Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");
if (!mContext.getPackageManager().queryIntentServices(serviceIntent, 0).isEmpty()) {
// service available to handle that Intent
serviceIntent.setPackage("com.android.vending");

mContext.bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE);
}
else {
// no service available to handle that Intent
if (listener != null) {
listener.onIabSetupFinished(
new IabResult(BILLING_RESPONSE_RESULT_BILLING_UNAVAILABLE,
"Billing service unavailable on device."));
}
}

此方法将帮助您将隐式 Intent 转换为显式形式。灵感来自 SO 答案:https://stackoverflow.com/a/26318757/1446466bindServiceConn() 方法正在创建服务。

 * @param context
* @param implicitIntent - The original implicit intent
* @return Explicit Intent created from the implicit original intent
*/

public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) {
// Retrieve all services that can match the given intent
PackageManager pm = context.getPackageManager();
List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);

// Make sure only one match was found
if (resolveInfo == null || resolveInfo.size() != 1) {
return null;
}

// Get component info and create ComponentName
ResolveInfo serviceInfo = resolveInfo.get(0);
String packageName = serviceInfo.serviceInfo.packageName;
String className = serviceInfo.serviceInfo.name;
ComponentName component = new ComponentName(packageName, className);

// Create a new intent. Use the old one for extras and such reuse
Intent explicitIntent = new Intent(implicitIntent);

// Set the component to be explicit
explicitIntent.setComponent(component);

return explicitIntent;
}



protected void bindServiceConn() {
//call this method
Intent intent = createExplicitFromImplicitIntent(context.getApplicationContext(), new Intent("com.android.vending.billing.InAppBillingService.BIND"));
context.bindService(intent, mServiceConn, Context.BIND_AUTO_CREATE);
}

protected void unbindServiceConn() {
context.unbindService(mServiceConn);

context=null;
}

关于java - API 23 (Android 6.0.1) 应用内计费 : Service Intent must be explicit: Intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35863751/

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