简体   繁体   中英

Android - checking for certain value does not

I am working on Android billing and trying to set the onPurchaseStateChange method correct, but it does not seem to be working.

    @Override
    public void onPurchaseStateChange(PurchaseState purchaseState, String itemId,
            int quantity, long purchaseTime, String developerPayload) 
    {
        if (purchaseState == PurchaseState.PURCHASED) 
        {
            mOwnedItems.add(itemId);

            if ( itemId != null && itemId.trim().equals("3") )
            {
                  Intent myIntent = new Intent(ExtraHelpActivity.this, PsychologyActivity.class);
                  ExtraHelpActivity.this.startActivity(myIntent);
            }
            if ( itemId != null && itemId.trim().equals("4") )
            {
                  Intent myIntent = new Intent(ExtraHelpActivity.this, NumberOfBusinessesActivity.class);
                  ExtraHelpActivity.this.startActivity(myIntent);   
            }                
        }
        else 
        if (purchaseState == PurchaseState.CANCELED) 
        {  
            // purchase canceled
        } 
        else 
        if (purchaseState == PurchaseState.REFUNDED) 
        {
            // user ask for a refund
        }
        else
        {   
            if ( itemId != null && itemId.equals("3") )
            {
                  Intent myIntent = new Intent(ExtraHelpActivity.this, PsychologyActivity.class);
                  ExtraHelpActivity.this.startActivity(myIntent);
            }
            if ( itemId != null && itemId.equals("4") )
            {
                  Intent myIntent = new Intent(ExtraHelpActivity.this, NumberOfBusinessesActivity.class);
                  ExtraHelpActivity.this.startActivity(myIntent);   
            }                   
        }

So when the purchaseId is "4" or "3" and the purchaseState == PurchaseState.PURCHASED ....for some reason it does not seem to get into that if statement, and does not perform the intent to go to the next page?

Would anyone know why that happens? It seems very strange. Could it be a Java thing?

Thanks!

PurchaseState is not a primitive data type like int , long , float , etc so you should use:

purchaseState.equals(PurchaseState.PURCHASED)

instead of == , just like you use with Strings:

string1.equals(string2) // Results you expect

is not the same as

string1 == string2 // Don't do this...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM