我有一个壁纸项目,我打算检查用户是否有有效订阅,然后让他下载壁纸,否则显示购买菜单也购买新订阅。我决定在下载按钮的事件处理程序中实现 BillingClient 类。我现在知道如何组合 BillingClient
和 SkuDetails
以使其显示购买菜单。这是我到目前为止尝试过的。我只需要我将在按钮方法中实现的良好逻辑以显示 google play 结帐弹出窗口。
这是我的代码
//My class implements the purchasesupdatedlistener
public class HomeFragment extends Fragment implements PurchasesUpdatedListener{
//SUBSCRIPTION IS SET TO FALSE BY DEFAULT
public boolean hasSubscription=false;
//This method handles the onclick of the button so i implemented BillingClient in the download button which has id R.id.cv_download
@OnClick({R.id.edtSearch, R.id.cv_Share, R.id.cv_like, R.id.cv_download,R.id.ivRandom})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.cv_download:
//This is where the app's business logic goes
//create a new BillingClient class
final BillingClient myclient= BillingClient.newBuilder(getActivity()).enablePendingPurchases().setListener(this).build();
//Establish connection to google play
myclient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(BillingResult billingResult) {
//The building client is ready you can setup purchases here
switch(billingResult.getResponseCode()){
case OK:
//Check if user has made a purchase for a subscription
if(hasSubscription){
Toast.makeText(getActivity(),"User has active subscription",Toast.LENGTH_LONG).show();
anImage = ((BitmapDrawable) ivRandom.getDrawable()).getBitmap();
saveImageToExternalStorage(anImage);
Toast.makeText(getActivity(), "Download successfuly", Toast.LENGTH_SHORT).show();
}else{
//User has no active subscription so request him to buy a new one
//This is where i am completely messed up
Toast.makeText(getActivity(),"User has no active subscription",Toast.LENGTH_LONG).show();
//So launch billingresult flow
try{
final SkuDetails mydetails=new SkuDetails(BillingClient.SkuType.SUBS);
myclient.querySkuDetailsAsync( SkuDetailsParams.newBuilder().build(), new SkuDetailsResponseListener() {
@Override
public void onSkuDetailsResponse(BillingResult billingResult, List<SkuDetails> list) {
list.add(mydetails);
}
});
}catch(JSONException k){
Toast.makeText(getActivity(),k.getMessage(),Toast.LENGTH_LONG).show();
}
}
}
}
@Override
public void onBillingServiceDisconnected() {
//Inform user that its not working and he should try some other time
Toast.makeText(getActivity(),"Connection Problems, Try Again Later",Toast.LENGTH_LONG).show();
}
});
//Implement conditional structure to check if user has subscription and then let him download
break;
}
//The billingClient interface is here
@Override
public void onPurchasesUpdated(BillingResult billingResult, List<Purchase> list) {
//To be implemented in a later section
switch(billingResult.getResponseCode()){
case OK:
Toast.makeText(getActivity(),"Payment made with success",Toast.LENGTH_LONG).show();
break;
case ITEM_UNAVAILABLE:
Toast.makeText(getActivity(),"This item is not available",Toast.LENGTH_LONG).show();
break;
case ITEM_ALREADY_OWNED:
Toast.makeText(getActivity(),"You already own this item",Toast.LENGTH_LONG).show();
break;
case BILLING_UNAVAILABLE:
case DEVELOPER_ERROR:
case ERROR:
case FEATURE_NOT_SUPPORTED:
case ITEM_NOT_OWNED:
case SERVICE_DISCONNECTED:
case SERVICE_UNAVAILABLE:
default:
Toast.makeText(getActivity(),"Some other problem occurred",Toast.LENGTH_LONG).show();
break;
}
}
}
我不知道如何创建 SkuDetails 和显示购买弹出菜单,请帮助