如何使用Billing库3.0创建订阅和购买?

问题描述

有人可以为我提供最简单的购买1个月订阅和购买1种产品的代码吗?

请只在一个文件中使用绝对最少的代码,而无需使用我自己的服务器支持来跟踪销售,并尽可能与Billing库3.0兼容

简而言之,我想执行以下代码


您点击[订阅]按钮并获得Google Play对话框菜单来支付订阅费用

您点击[购买燃料]按钮,并获得一个Google Play对话框菜单来为产品付款

您单击按钮[操作按钮]并代码检查订阅状态(有效,已过期,已取消,在宽限期内,处于等待状态,已暂停)

//是否有状态--->从不进行订阅...之类的事情?)

应该有一些事件...一些触发器可以让您了解请求购买产品时的状态...

重写乐趣OnSomeActivityDontKNowWhichOne(操作:操作,字符串:字符串)


从视觉上看,像这样:

package example.asdf.com.billy

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast

var CarFuel = 0

class MainActivity : AppCompatActivity() {

override fun OnSomeActivityDontKNowWhichOne(action: Action,product: String) 
{

    if (action == PURCHASE_PRODUCT_OK) 
    {
        if (product == "car_fuel")
        {
           CarFueL++
           Toast.makeText(applicationContext,"Thank you for your purchase. You have Now " + CarFuel.toString() 
           + " unit(s) of fuel",Toast.LENGTH_SHORT).show()
        }
    }

    if (action == PURCHASE_PRODUCT_NOT_OK
    {
           Toast.makeText(applicationContext,"Purchase Failed...",Toast.LENGTH_SHORT).show()
    }
}


override fun onCreate(savedInstanceState: Bundle?) 
{
   super.onCreate(savedInstanceState)
   setContentView(R.layout.activity_main)




   // Text on button: [Subscribe]
   val BuySub = findViewById<Button>(R.id.buttonBuyOneMonthSub)
   BuySub.setonClickListener(View.OnClickListener {

   // When you click button [Subscribe] it should appear
   // Google Play dialog for paying for 1 month subscription

   RequestBuySubscription()

   })



   // Text on button: [Buy fuel]
   val BuyProduct = findViewById<Button>(R.id.buttonBuyMyCoolProduct)
   BuyProduct.setonClickListener(View.OnClickListener {

   // When you click button [Buy fuel] it should appear
   // Google Play dialog for buying a product

   RequestPurchaseProduct(ProductId)

   })



   // Text on button: [Action button]
   val MyCoolAction = findViewById<Button>(R.id.buttonActionButton)
   MyCoolAction.setonClickListener(View.OnClickListener {

   // check if subscription is active...
   // like...
   val Status = GetSubscriptionStatus()

   // Status can be Active,Expired,Canceled,In grace period,On hold,Paused
   // Is there a status ---> NEVER BOUGHT SUBSCRIPTION... or something like that?

   if (Status == EXPIRED)
   {
      Toast.makeText(applicationContext,"Subscription expired. Please click [Subscribe]",Toast.LENGTH_SHORT).show()

      return@OnClickListener
   }

   if (Status == ACTIVE)
   {
      Toast.makeText(applicationContext,"Thank you for being our vip user :)",Toast.LENGTH_SHORT).show()
 
      FetchSubStartTime()
      FetchSubExpirationTime()

      // It would be good to fetch date of Subscription start and subscription end
      // Preferably if possible to get unix times :)
   }


 })


   }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)