我几天来一直在努力解决这个问题。我知道在SO上存在很多关于同样问题的问题,但我无法让它发挥作用。
我做了什么
代码
的AndroidManifest.xml
<uses-permission android:name="com.android.vending.BILLING" />
MainActivity.java
public class MainActivity extends AppCompatActivity {
private IabHelper mHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ...
setupInAppBillings();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (!mHelper.handleActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
// [....]
private void setupInAppBillings() {
String base64EncodedPublicKey = "MY PUBLIC KEY";
mHelper = new IabHelper(this, base64EncodedPublicKey);
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
if (!result.isSuccess()) {
Toast.makeText(getContext(), "In-app Billing setup failed: " + result, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getContext(), "In-app Billing is set up OK", Toast.LENGTH_SHORT).show();
}
}
});
}
}
经过测试
我尝试了什么
我从这个列表中唯一没有做的就是设置许可证验证库(LVL)。但我找不到任何有关应用程序内购买所需的步骤的信息。如果不需要我想在没有这个库的情况下这样做,因为我并不像Google网站上所说的那样需要它。
Google Play许可服务主要面向希望验证当前用户确实为Google Play上的应用付费的付费应用。
有什么我错过的吗?
答案 0 :(得分:1)
Finally I got it to work! The problem was the following: Even though I put the IInAppBillingService.aidl
in the com.android.vending.billing
package, the generated class was in the wrong package as you can see in the code below.
/*
* This file is auto-generated. DO NOT MODIFY.
* Original file: C:\\path\\src\\main\\aidl\\com\\android\\vending\\billing\\IInAppBillingService.aidl
*/
package billing;
public interface IInAppBillingService extends android.os.IInterface { //... }
To solve this, I deleted and recreated the com.android.vending.billing
package with the IInAppBillingService.aidl
. So if you have the same problem, check twice where the IInAppBillingService.java
was generated.
答案 1 :(得分:0)
我最近遇到了这个问题。正如Bona Fide所写,IInAppBillingService.aidl中的包声明必须设置为“com.android.vending.billing”,并且应该使用资源管理器在相应的目录中找到aidl文件。此外(这是我的问题),在IabHelper.java中,serviceIntent的字符串参数必须与包含IInAppBillingService.aidl文件的包名称相同。
Intent serviceIntent = new Intent("com.android.vending.billing.InAppBillingService.BIND");// correct package name: com.android.vending.billing
serviceIntent.setPackage("com.android.vending");
List<ResolveInfo> intentServices = mContext.getPackageManager().queryIntentServices(serviceIntent, 0);
if (intentServices != null && !intentServices.isEmpty()) {
// service available to handle that Intent
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."));
}
}
}