作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试集成 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
谢谢
最佳答案
看来唯一能做的就是等待......
今天早上成功了。
计算 2 天后,您的应用内付款即可在应用上使用。
关于java - Android Google Pay 结算 - 响应 4 : ITEM_UNAVAILABLE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62320895/
我正在尝试集成 Google Play 结算。 这是我的 onCreate : private final static String TAG = "MainActivity" ; priva
我是一名优秀的程序员,十分优秀!