我遇到了问题,我无法处理此页面上的Google代码段:https://developer.android.com/google/play/billing/billing_java_kotlin 我的目标是仅实施六个不同的一次性产品。 问题是,我无法处理此代码段:
@Override
void onPurchasesUpdated(@BillingResponse int responseCode, List purchases) {
if (responseCode == BillingResponse.OK
&& purchases != null) {
for (Purchase purchase : purchases) {
handlePurchase(purchase);
}
} else if (responseCode == BillingResponse.USER_CANCELED) {
// Handle an error caused by a user cancelling the purchase flow.
} else {
// Handle any other error codes.
}
}
我不知道如何处理新的Android计费系统。您能否解释一下新的实现方式,将Google代码放在哪里? 我没有方法“ handlePurchase(purchase);”。我当前的完整代码如下(请告诉我它是对还是错以及如何使其变得更好):
import android.app.Activity;
import android.app.TaskStackBuilder;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.billingclient.api.BillingClient;
import com.android.billingclient.api.BillingClientStateListener;
import com.android.billingclient.api.BillingFlowParams;
import com.android.billingclient.api.Purchase;
import com.android.billingclient.api.PurchasesUpdatedListener;
import com.android.billingclient.api.SkuDetails;
import com.android.billingclient.api.SkuDetailsParams;
import com.android.billingclient.api.SkuDetailsResponseListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import java.util.ArrayList;
import java.util.List;
public class PaymentActivity extends Activity implements PurchasesUpdatedListener{
private SharedPreferences prefs;
private Intent svc;
private MediaPlayer mediaPlayer;
private final AlphaAnimation buttonClick = new AlphaAnimation(1F, 0.3F);
private Button adfree;
private Button two;
private Button three;
private Button four;
private Button five;
private Button ten;
private BillingClient mBillingClient;
private String mAdFreePrice;
private String m2FailsLeftPrice;
private String m3FailsLeftPrice;
private String m4FailsLeftPrice;
private String m5FailsLeftPrice;
private String m10FailsLeftPrice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment);
adfree = findViewById(R.id.adfree);
two = findViewById(R.id.two);
three = findViewById(R.id.three);
four = findViewById(R.id.four);
five = findViewById(R.id.five);
ten = findViewById(R.id.ten);
payInApp();
setButtons();
svc = new Intent(this, BackgroundSoundService.class);
checkMusicState();
}
private void payInApp() {
mBillingClient = BillingClient.newBuilder(getApplicationContext())
.setListener(this)
.build();
mBillingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(int responseCode) {
if (responseCode == BillingClient.BillingResponse.OK) {
//The billing client is ready
//You can query purchase here
}
}
@Override
public void onBillingServiceDisconnected() {
}
});
List skuList = new ArrayList<>();
skuList.add("2_fails_left");
skuList.add("3_fails_left");
skuList.add("4_fails_left");
skuList.add("5_fails_left");
skuList.add("10_fails_left");
skuList.add("ad_free");
SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
params.setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
mBillingClient.querySkuDetailsAsync(params.build(), new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(int responseCode, List<SkuDetails> skuDetailsList) {
//Process the result
if (responseCode == BillingClient.BillingResponse.OK && skuDetailsList != null) {
//process response
for (SkuDetails skuDetails : skuDetailsList) {
String sku = skuDetails.getSku();
String price = skuDetails.getPrice();
if ("ad_free".equals(sku)) {
mAdFreePrice = price;
adfree.setText(mAdFreePrice);
}else if ("2_fails_left".equals(sku)) {
m2FailsLeftPrice = price;
two.setText(m2FailsLeftPrice);
}else if ("3_fails_left".equals(sku)) {
m3FailsLeftPrice = price;
three.setText(m3FailsLeftPrice);
}else if ("4_fails_left".equals(sku)) {
m4FailsLeftPrice = price;
four.setText(m4FailsLeftPrice);
}else if ("5_fails_left".equals(sku)) {
m5FailsLeftPrice = price;
five.setText(m5FailsLeftPrice);
}else if ("10_fails_left".equals(sku)) {
m10FailsLeftPrice = price;
ten.setText(m10FailsLeftPrice);
}
}
}
}
});
}
private void setButtons() {
adfree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(buttonClick);
setClickSound();
BillingFlowParams.Builder builder = BillingFlowParams.newBuilder()
.setSku("ad_free")
.setType(BillingClient.SkuType.INAPP);
int responseCode = mBillingClient.launchBillingFlow(PaymentActivity.this, builder.build());
}
});
two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(buttonClick);
setClickSound();
BillingFlowParams.Builder builder = BillingFlowParams.newBuilder()
.setSku("2_fails_left")
.setType(BillingClient.SkuType.INAPP);
int responseCode = mBillingClient.launchBillingFlow(PaymentActivity.this, builder.build());
}
});
three.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(buttonClick);
setClickSound();
BillingFlowParams.Builder builder = BillingFlowParams.newBuilder()
.setSku("3_fails_left")
.setType(BillingClient.SkuType.INAPP);
int responseCode = mBillingClient.launchBillingFlow(PaymentActivity.this, builder.build());
}
});
four.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(buttonClick);
setClickSound();
BillingFlowParams.Builder builder = BillingFlowParams.newBuilder()
.setSku("4_fails_left")
.setType(BillingClient.SkuType.INAPP);
int responseCode = mBillingClient.launchBillingFlow(PaymentActivity.this, builder.build());
}
});
five.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(buttonClick);
setClickSound();
BillingFlowParams.Builder builder = BillingFlowParams.newBuilder()
.setSku("5_fails_left")
.setType(BillingClient.SkuType.INAPP);
int responseCode = mBillingClient.launchBillingFlow(PaymentActivity.this, builder.build());
}
});
ten.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.startAnimation(buttonClick);
setClickSound();
BillingFlowParams.Builder builder = BillingFlowParams.newBuilder()
.setSku("10_fails_left")
.setType(BillingClient.SkuType.INAPP);
int responseCode = mBillingClient.launchBillingFlow(PaymentActivity.this, builder.build());
}
});
}
@Override
public void onBackPressed() {
Intent intent = new Intent(PaymentActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this.startActivity(intent);
this.finish();
}
@Override
protected void onPause() {
super.onPause();
stopService(svc);
}
@Override
protected void onResume() {
super.onResume();
checkMusicState();
}
private void checkMusicState(){
prefs = this.getSharedPreferences("PREF_MUSIC", MODE_PRIVATE);
int musicstate = prefs.getInt("MUSIC", 1);
if(musicstate == 1){
startService(svc);
}else{
stopService(svc);
}
}
@Override
public void onPurchasesUpdated(@BillingClient.BillingResponse int responseCode,
@Nullable List<Purchase> purchases) {
//Handle purchases result
if (responseCode == BillingClient.BillingResponse.OK && purchases != null) {
for (Purchase purchase : purchases) {
//process the result
}
}else if (responseCode == BillingClient.BillingResponse.USER_CANCELED) {
//Handle an error when the user cancel
}else{
//handle any other error codes
}
}
}