多个活动的单个IabHelper实例

时间:2015-09-22 12:17:22

标签: android android-fragments in-app-billing

我正在尝试为我的应用实施应用内结算。我正在关注Google的Android In-app billing tutorial

在教程中,IabHelperActivity的实例变量。要实例化IabHelper,我们需要传递Context,在这种情况下是Activity本身。

我有几个Activty需要与Google Play商店进行通信。为不同的IabHelper s创建多个Activity是不是很糟糕?

如果不好,我如何为IabHelper个不同的Activity提供一个IabHelper?当我在Context之间切换时,如何告诉Activity import threading class App(Frame): def __init__(self, master=None): Frame.__init__(self, master) self.is_playing = False self.pack() self.widgets() def widgets(self): self.beep = Button(self, text = "Beep", command = self.play_beep) self.beep.pack() self.stop = Button(self, text = "Stop", command = self.stop_beep) self.stop.pack() def play_beep(self): self.is_running = True self.beep_th = threading.Thread(target=self.run) self.beep_th.start() def run(self): count = 10 while self.is_running == True and count != 0: winsound.Beep(randint(100, 2500), 200) count -= 1 def stop_beep(self): try: self.is_running = False self.beep_th.join(0) self.beep_th = None except (AttributeError, RuntimeError): # beep thread could be None pass def closeEvent(self, event): # This is a pyside method look for a TKinter equivalent. """When you close the App clean up the thread and close the thread properly.""" self.stop_beep() super().closeEvent(event) 已更改?

谢谢。

1 个答案:

答案 0 :(得分:0)

我找到了下一个解决方案: 我在Application>中进行初始化onCreate方法:

@Override
    public void onCreate() {
        final IabHelper iabHelper = IabHelper.createInstance(getApplicationContext(), AppHelper.getPublicKey());
}

然后,当您需要启动购买时,您应该这样做:

public static void launchPurchase():void {
    final AppCompatActivity foregAct = sFacade.getForegroundActivity();
    final String payload = AppHelper.generateDeveloperPayload();
    final IabHelper iabHelper = IabHelper.getInstance();
    if (iabHelper != null) {
        iabHelper.launchPurchaseFlow(foregAct, AppHelper.SKU_PRO, AppHelper.RC_REQUEST, mPurchaseFinishedListener, payload);
    }
}

其中getForegroundActivity可以以不同的方式实现,例如:

public AppCompatActivity getForegroundActivity() {
    if (MainActivity.isForeground) {
        return (AppCompatActivity) retrieveEntity(MainActivity.NAME);
    } else if (MiscActivity.isForeground) {
        return (AppCompatActivity) retrieveEntity(MiscActivity.NAME);
    } if (GameActivity.isForeground) {
        return (AppCompatActivity) retrieveEntity(GameActivity.NAME);
    }  else {
        return (AppCompatActivity) retrieveEntity(HelpActivity.NAME);
    }
}

或者您可以选择其他方法来跟踪前景活动:

How to check if activity is in foreground or in visible background?

Android: How can I get the current foreground activity (from a service)?

最后,不要在任何活动中处理IabHelper。您应该在每个可以启动购买的活动中包含处理代码:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        final IabHelper iabHelper = IabHelper.getInstance();
        if (iabHelper != null) {
            iabHelper.handleActivityResult(requestCode, resultCode, data);
        }
}