簡體   English   中英

Android IAB - 成功購買后銷毀活動

[英]Android IAB - Activity destroyed after successful purchase

我把頭發拉出來! 在上周的某個時刻,我有這個工作。

我有一個Android應用程序,我試圖添加in-ap計費。 我跟着示例TrivialDrive,我的代碼工作了幾次。 現在它沒有。

我正在創建一個簡單的瑣事游戲,其中包含許多免費問題,以及升級選項以獲得更多問題。 當用戶完成免費問題列表時,他們將被帶到“游戲結束”屏幕,在那里他們可以刪除他們的答案並重新開始或升級。

當我點擊“升級”按鈕時,我可以成功購買,但只要Google“付款成功”對話框消失,我的活動就會被銷毀,我會被發回我的主要活動。

當我嘗試返回並再次購買時,我的代碼會捕獲錯誤(“您已擁有此項目”)並正確處理。 我的代碼向用戶解釋他們已經擁有升級,並允許他們單擊按鈕繼續播放。 所以看起來OnIabPurchaseFinishedListener此時正在觸發。

我已使用最新文件更新了Google幫助程序代碼。

任何有關在何處尋找答案的幫助或建議都非常感謝。

謝謝。

這是我活動的相關代碼:

public class GameOverActivity extends BaseActivity
{

    private IabHelper       mHelper;
    private String          m_base64EncodedPublicKey;
    private static String   THE_UPGRADE_SKU = "upgrade52";
    public static int BILLING_RESPONSE_RESULT_ITEM_ALREADY_OWNED = 7;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_over);

        setTitle("Game Over");

        Button butPlay = (Button) findViewById(R.id.buttonPlay);
        butPlay.setVisibility(View.INVISIBLE);

        PrepareIAB();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
        CURRENT_ACTIVITY = ACTIVITY_GAME_OVER;
        SetMainText();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        try
        {
            if (mHelper != null)
            {
                mHelper.dispose();
                mHelper = null;
            }
        }
        catch (Exception e)
        {
        }       
    }

    private void PrepareIAB()
    {
        m_base64EncodedPublicKey = "MyKey";

        // compute your public key and store it in base64EncodedPublicKey
        mHelper = new IabHelper(this, m_base64EncodedPublicKey);
        mHelper.enableDebugLogging( true, TAG);

        mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener()
        {
            public void onIabSetupFinished(IabResult result)
            {
                if (!result.isSuccess())
                {

                    ShowMessage("There was an error connecting to the Google Play Store.");
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {           
         try 
         {
            // Pass on the activity result to the helper for handling
                if (!mHelper.handleActivityResult(requestCode, resultCode, data))
                {
                    // not handled, so handle it ourselves (here's where you'd
                    // perform any handling of activity results not related to in-app
                    // billing...
                    super.onActivityResult(requestCode, resultCode, data);
                }
                else
                {
                    // Log.d(TAG, "onActivityResult handled by IABUtil.");
                }    
         }
         catch (Exception e)
         {
             super.onActivityResult(requestCode, resultCode, data); 
         }
    }



    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener   = 
            new IabHelper.OnIabPurchaseFinishedListener()
            {
                public void onIabPurchaseFinished(IabResult result, Purchase purchase)
                {
                    try 
                    {
                        if (result.isFailure())
                        {                       
                            if (result.mResponse==7)
                            {
                                UpgradeComplete();
                                ShowMessage("Thank you for upgrading.\r\n\r\nThis version has 400 more questions.");        
                            }
                            else
                            {

                                ShowMessage("Error purchasing: " + String.valueOf(result.mResponse));                           
                                UpgradeError();

                                return;
                            }

                        }
                        else if (purchase.getSku().equals(THE_UPGRADE_SKU))
                        {               
                            UpgradeComplete();
                            ShowMessage("Thank you for upgrading.\r\n\r\nThis version has 400 more questions.");                                        
                        }
                        else
                        {
                            ShowMessage("Something else happened. ");
                        }
                    }
                    catch (Exception e)
                    {
                        Log.e(TAG, e.getLocalizedMessage());
                    }

                }
            };

    private void HideUpgrade()
    {
        try 
        {
            Button btnUpgrade = (Button) findViewById(R.id.buttonUpgrade);
            if (btnUpgrade != null)
            {
                btnUpgrade.setVisibility(View.INVISIBLE);
            }           

            TextView txtMessage = (TextView) findViewById(R.id.txtUpgradeFromGameOver);
            if (txtMessage!=null)
            {
                txtMessage.setVisibility(View.INVISIBLE);   
            }   
        }
        catch (Exception e)
        {

        }        
    }

    public void onQuitButtonClick(View view)
    {
        finish();
    }

    public void onResetDBButtonClick(View view)
    {
        ConfirmResetDatabase();
    }

    private void ConfirmResetDatabase()
    {
        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                switch (which)
                {
                    case DialogInterface.BUTTON_POSITIVE:

                        ResetDatabase();

                        Intent gameActivity = new Intent(getApplicationContext(), GameActivity.class);

                        gameActivity.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                        // startActivityForResult(gameActivity, ACTIVITY_GAME);
                        startActivity(gameActivity);
                        break;

                    case DialogInterface.BUTTON_NEGATIVE:
                        // No button clicked
                        break;
                }
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Do you want to erase your score and start over?").setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener).show();
    }


    public void onUpgradeButtonClick(View view)
    {           
        try 
        {
            if (mHelper != null)
            {
                mHelper.launchPurchaseFlow(this, THE_UPGRADE_SKU, 10001, mPurchaseFinishedListener, m_TriviaAppInstance.AppInstallID());
            }
            else
            {
                ShowMessage("Unable to connect to Google Play Store.");         
            }   
        }
        catch (Exception e)
        {
            ShowMessage("Unable to connect to Google Play Store.");
            SendErrorMessage(e.getLocalizedMessage());          
        }
    }



    private void UpgradeComplete()
    {
        try
        {    
            HideUpgrade();

            Button butPlay = (Button) findViewById(R.id.buttonPlay);
            if (butPlay!=null)
            {
                butPlay.setVisibility(View.VISIBLE);    
            }

            TextView txtReset = (TextView) findViewById(R.id.txtGameOverRestDB);
            if (txtReset!=null)
            {
                txtReset.setVisibility(View.INVISIBLE); 
            }

            Button btnReset = (Button)findViewById(R.id.buttonResetDB);
            if (btnReset!=null)
            {
                btnReset.setVisibility(View.INVISIBLE);
            }

            m_TriviaAppInstance.SetUpgradedStatus(true);

        }
        catch (Exception e)
        {

        }

        //

    }

    private void UpgradeError()
    {
        try
        {
            Button butUpgrade;
            butUpgrade = (Button) findViewById(R.id.buttonUpgrade);
            butUpgrade.setVisibility(View.INVISIBLE);

            TextView txtMessage = (TextView) findViewById(R.id.txtUpgradeScreen);
            txtMessage.setText(R.string.upgradeScreenTextError);
        }
        catch (Exception e)
        {
        }
    }


    public void onPlayButtonClick(View view)
    {
        Intent myIntent = new Intent(view.getContext(), GameActivity.class);
        myIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        startActivityForResult(myIntent, ACTIVITY_GAME);
    }

    public void SetMainText()
    {
        TextView txt = (TextView) findViewById(R.id.txtScoreGlobal);
        txt.setText(Integer.toString(m_TriviaAppInstance.getGlobal()) + "%");
        SetPlayerScore(1);

        if (m_TriviaAppInstance.getUpgradedStatus() == true)
        {           
            HideUpgrade();          
        }       
    }

}

僅供參考:我想我已經想到了這一點 - 對於任何可能遇到它的人。

我用於啟動“In App Billing”的活動是使用“FLAG_ACTIVITY_NO_HISTORY”調用的。 我這樣做是因為我不希望用戶能夠點擊返回到“Game Over”活動。

但是,這會導致“In App Billing”的悲痛。 因此,請確保您不要嘗試從已設置“FLAG_ACTIVITY_NO_HISTORY”的活動啟動“In App Billing”。

我原來的代碼:

private void GameOver()
    {
        m_TriviaAppInstance.setGameOver(true);
        Intent gameOver = new Intent(getApplicationContext(), GameOverActivity.class);
        gameOver.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        startActivity(gameOver);
    }

更新的代碼:

private void GameOver()
    {
        m_TriviaAppInstance.setGameOver(true);
        Intent gameOver = new Intent(getApplicationContext(), GameOverActivity.class);       
        startActivity(gameOver);
    }

和平

我不高,不能評論,但祝福你。 我有

android:noHistory="true"

在AndroidManifest.xml中為我的活動設置並遇到了同樣的問題。

拿出來,IAB正在工作。 好極了!

不要忘記您的IabHelper.OnIabPurchaseFinishedListener是在另一個線程上調用的,並且在您的Activity上調用onResume()之前!

因此,您的UpgradeComplete()或UpgradeError()可能會導致舊設備崩潰(每次在我的Gingerbread Sony Xperia Mini Pro上崩潰,並且在三星Galaxy S4(Android 4.2.2)上沒有任何問題。

造成我的比賽延遲3天..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM