0

我有一个游戏,你和回答问题的朋友玩 1v1。目前有 5 组问题由 admob 货币化。我计划再增加 10 套和没收选项,这些都将通过应用内购买解锁(也删除广告)。

我有实现工作,但觉得这不是最好的方法,可以通过编辑来利用SharedPreferences

当用户通过应用内购买购买“高级版”时,我"has_premium"SharedPreferences. 在我的片段中,OnCreateView()我调用querySkuDetailsAsync()并相应地更新此偏好,以确保所有购买都得到确认和最新。

无论我在哪里放置广告,我都会检查此SharePreference值,然后显示/隐藏广告。同样,当显示RecyclerView可用的问题集和没收时,我再次检查这个SharePreference值。如果用户是高级用户,我会启用RecyclerView. 如果用户不是高级用户,那么我会禁用一些项目,而是BillingFlow在单击时启动。

有没有比存储价值更好的方法来检查用户是否购买了“高级”状态SharedPreferences

4

1 回答 1

0

By moving all billing logic to a BillingRepository, which caches purchases in a local database, then serving all fragments which display ads or premium features via a BillingViewModel, there is no need for any SharedPreferences

I can simply call:

billingViewModel = ViewModelProviders.of(this).get(BillingViewModel::class.java)
    billingViewModel.userPurchasedProLiveData.observe(this, Observer {
        it?.apply {
            showProStatus(entitled)
        }
    })

private fun showProStatus(entitled: Boolean) {
        if (entitled) {
            btnMainMenuPro.visibility = View.GONE
            disableAds()
        } else {
            btnMainMenuPro.visibility = View.VISIBLE
            enableAds()
        }
    }

As per the Google Docs:

Notice that the connection to [playStoreBillingClient] is created using the * applicationContext. This means the instance is not [Activity]-specific. And since it's also * not expensive, it can remain open for the life of the entire [Application]. So whether it is * (re)created for each [Activity] or [Fragment] or is kept open for the life of the application * is a matter of choice.

More info and an example can be found here: https://github.com/android/play-billing-samples/tree/master/TrivialDriveKotlin

于 2020-03-14T00:02:17.023 回答