我正在尝试集成Google Play结算。
这是我的onCreate:
private final static String TAG = "MainActivity" ;
private final static String ITEM_SKU_SUBSCRIBE = "sub_example" ;
private final static String PREF_FILE = "shared_prefs" ;
private final static String SUBSCRIBE_KEY = "subscribe" ;
private BillingClient billingClient ;
private TextView premiumContent ;
private TextView subscriptionStatus ;
private Button purchaseButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
premiumContent = findViewById(R.id.premium_content) ;
subscriptionStatus = findViewById(R.id.subscription_status) ;
purchaseButton = findViewById(R.id.button) ;
billingClient = BillingClient.newBuilder(this)
.enablePendingPurchases()
.setListener(this)
.build() ;
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(BillingResult billingResult) {
Log.i(TAG, "onBillingSetupFinished ; billing response code == " + billingResult.getResponseCode());
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
Purchase.PurchasesResult queryPurchase = billingClient.queryPurchases(SUBS);
List<Purchase> queryPurchases = queryPurchase.getPurchasesList();
if (queryPurchases != null && queryPurchases.size() > 0) {
handlePurchases(queryPurchases);
} else {
saveSubscribeValueToPref(false);
}
} else {
Log.e(TAG, "ELSE : " + billingResult.getDebugMessage()) ;
}
}
@Override
public void onBillingServiceDisconnected() {
Toast.makeText(getApplicationContext(), "Service disconnected",
Toast.LENGTH_SHORT).show() ;
}
});
if (getSubscribeValueFromPref()) {
purchaseButton.setVisibility(View.GONE);
premiumContent.setVisibility(View.VISIBLE);
subscriptionStatus.setText("Subscription Status : Subscribed");
} else {
premiumContent.setVisibility(View.GONE);
purchaseButton.setVisibility(View.VISIBLE);
subscriptionStatus.setText("Subscription Status : Not Subscribed");
}
purchaseButton.setText("Load products");
purchaseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
subscribe();
}
});
}
当单击purchaseButton
时,我调用subscribe
功能。
[如果initiatePurchase
返回billingClient.isReady()
,则基本上只调用函数true
,在另一种情况下初始化billingClient
。
这是我的initiatePurchase
函数:
private void initiatePurchase() {
List<String> skuList = new ArrayList<>() ;
SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder() ;
skuList.add(ITEM_SKU_SUBSCRIBE);
params.setSkusList(skuList).setType(SUBS);
BillingResult billingResult = billingClient.isFeatureSupported(BillingClient.FeatureType.SUBSCRIPTIONS) ;
Log.i(TAG, "Billing response code == " + billingResult.getResponseCode()) ;
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
Log.i(TAG, "Billing response code is OK");
billingClient.querySkuDetailsAsync(params.build(),
new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> skuDetailsList) {
Log.i(TAG, "onSkuDetailsResponse ; billing result response code == " + billingResult.getResponseCode());
if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK) {
Log.i(TAG, "Billing response code OK") ;
if (skuDetailsList != null) {
for (int i = 0 ; i < skuDetailsList.size() ; i++) {
Log.i(TAG, "Loop INDEX " + i + " ; SkuDetails == " + skuDetailsList.get(i).getTitle()) ;
}
}
if (skuDetailsList != null && skuDetailsList.size() > 0) {
Log.i(TAG, "skuDetailsList is not null or empty");
BillingFlowParams flowParams = BillingFlowParams.newBuilder()
.setSkuDetails(skuDetailsList.get(0))
.build();
Log.i(TAG, "Flow Params = " + flowParams.getAccountId() + " | " + flowParams.getSku());
BillingResult billingFlowResult = billingClient
.launchBillingFlow(MainActivity.this, flowParams);
Log.i(TAG, "billingFlowResult == " + billingFlowResult.getResponseCode() + " | " + billingFlowResult.getDebugMessage()) ;
} else {
Toast.makeText(getApplicationContext(),
"Item not found", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(),
"Error - " + billingResult.getDebugMessage(),
Toast.LENGTH_SHORT).show();
}
}
});
} else {
Toast.makeText(getApplicationContext(),
"Sorry, subscription not supported. Please update Play Store",
Toast.LENGTH_SHORT).show();
}
}
似乎转到onSkuDetailsResponse
,并且帐单响应代码为OK。
我在skuDetailsList
中循环,然后得到sub_example
,这是我的ITEM_SKU_SUBSCRIBE
(和我的Play商店控制台上的那个);因此我的skuDetailsList
不为null或为空。
但是,这是我的日志输出:
I/MainActivity: Billing response code == 0
Billing response code is OK
I/MainActivity: onSkuDetailsResponse ; billing result response code == 0
Billing response code OK
Loop INDEX 0 ; SkuDetails == Monthly sub (Test Billing)
skuDetailsList is not null or empty
I/MainActivity: Flow Params = null | sub_example
I/MainActivity: billingFlowResult == 0 | null
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@cb159f0
当我单击“确定”按钮时,会出现有趣的日志:
W/ProxyBillingActivity: Activity finished with resultCode 0 and billing's responseCode: 4
W/BillingHelper: Couldn't find purchase lists, trying to find single data.
W/BillingHelper: Received a bad purchase data.
Couldn't find single purchase data as well.
我在BillingClient.class
中查找,然后在int ITEM_UNAVAILABLE = 4;
中找到了interface BillingResponseCode
谢谢
我正在尝试集成Google Play结算。这是我的onCreate:私有最终静态String TAG =“ MainActivity”;私有最终静态字符串ITEM_SKU_SUBSCRIBE =“ sub_example”; ...
似乎唯一要做的就是等待...今天早上工作了。请数两天,您的应用内付款才能在应用上使用。