android - AsyncTask 中的应用内计费查询库存?

标签 android asynchronous android-asynctask in-app-billing

我关注了this tutorial在使用 AsyncTask 的 doInBackground() 函数加载数据时向我的应用程序的启动添加加载屏幕。

我的应用还具有应用内结算高级升级功能,我想查询库存以在启动时检查这一点。然而,IabHelper 函数已经是异步的。

如何将 IabHelper 检查集成到 doInBackground() 中,以便仅在一切成功完成时才加载主要 Activity ?

我的账单代码如下:

private void checkForPremiumPurchase()
{
    billingHelper = new IabHelper(this, Constants.BASE_64_KEY);
    //Start setup. This is asynchronous and the specified listener will be called once setup completes.
    billingHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
        public void onIabSetupFinished(IabResult result) {
            if(result.isSuccess()) {
                billingHelper.queryInventoryAsync(mGotInventoryListener);
            }
        }
    });
}

//Listener that's called when we finish querying the items and subscriptions we own
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener()
{
    @Override
    public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
        if(result.isSuccess()) {
            isPremium = inventory.hasPurchase(Constants.SKU_PREMIUM);
            Log.d(Constants.TAG, "App is " + (isPremium ? "PREMIUM" : "NOT PREMIUM"));
        }
    }
};

最佳答案

AsyncTask 非常有用,它可以帮助您将长时间运行的作业放到后台线程中,并为您提供一个非常干净的机制,用于在后台任务运行之前、期间和之后更新 UI。 .. 所有这些都不会直接干扰线程。

但是,其他一些 Android API 设置为让您在主 (UI) 线程上发起调用,然后在幕后,它们将在后台线程上完成工作(他们甚至可以使用 AsyncTask,尽管您不一定关心)。

在这种情况下,IabHelper methods you're using are asynchronous ,它们将允许您从主线程启动它们,而不会阻塞 UI。

因此,无需在用于其他工作的相同 AsyncTask#doInBackground() 方法中运行它们,只是为了关闭工作主线程。


我看到两个选项:

1) 并发加载/IAB 请求

理想情况下,如果您需要在启动时加载一些数据(并且正在为此使用 AsyncTask),您可以在同时。

您描述了一个主要 Activity ,所以我假设您的应用以某种启动 Activity 开始(?)。在启动 Activity 中,您可以使用:

public void onCreate(Bundle savedInstanceState) {
    new MyLoadingAsyncTask().execute();

    checkForPremiumPurchase();
}

然后您将在 IAB 检查完成时启动主要 Activity :

public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
    isPremium = false;
    if(result.isSuccess()) {
        isPremium = inventory.hasPurchase(Constants.SKU_PREMIUM);
        Log.d(Constants.TAG, "App is " + (isPremium ? "PREMIUM" : "NOT PREMIUM"));
    }
    Intent i = new Intent(self, MainActivity.class);
    i.putExtra("IsPremium", isPremium);
    startActivity(i);
}

这是假设网络 IAB 交易花费的时间比您应用的正常“加载”过程要长。 (如果该假设无效,请发表评论,我会处理这种情况)

2) 序列化加载,然后是IAB

如果您的应用设计有其他要求您“完成加载”然后然后启动 IAB 请求,那么您可以在 checkForPremiumPurchase() 时调用 AsyncTask 完成它的工作:

 protected void onPostExecute(Long result) {
     checkForPremiumPurchase();
 }

AsyncTask#onPostExecute() 在加载完成时在主线程上被调用,并且 checkForPremiumPurchase() 可以安全地在主线程上调用。

评论

一般来说,我建议不要延迟应用程序的启动来检查高级升级。理想情况下,您会找到一种聪明的方法来一次保存此状态(购买高级版),然后避免将来检查。您的用户会很感激。

但是,我不知道您的应用程序的免费/付费之间的区别是什么,以及这种区别是否会立即显示出来……所以,也许这是您无法避免的事情。

关于android - AsyncTask 中的应用内计费查询库存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18414077/

相关文章:

c# - 异步私有(private)和私有(private)异步有什么区别?

java - 为什么我的 AsyncTask 中会出现这个 ClassCastException?

android - 是否可以检查 Google 帐户是否属于 Google+ 中特定社区的一部分

java - 类似 GridView 的 View 或布局

ios - 通过 url 为 scrollview ios 异步请求图像

java - 使用 HttpAsyncClient 发送异步调用时出现问题

android - 异步任务后无法使菜单项可见

android - AsyncTask、RejectedExecutionException 和任务限制

android - android应用程序可以只有广播接收器和没有 Activity 的服务吗

java - ViewPager2 无法与 fragment 和点击事件一起正常工作