2013-08-21 1 views
2

InAppBilling API의 v3에서 IAP를 사용하는 방법을 마침내 알아 냈습니다. 이제 사용자는 원하는만큼의 제품을 지속적으로 소비 할 수 있습니다.IAP 구매 후 GUI 업데이트, IAPv3 - Android

이제 구매가 완료되면 사용자 GUI를 업데이트하고 싶습니다. 토스트를 아래의 코드 전체에 넣고 GUI를 업데이트 할 위치를 찾으려고했지만 토스트가 아직 나타나지 않았습니다. 그러나 IAP 소비가 효과적이라는 것을 기억하십시오.

아래의 코드에서 사용자의 GUI를 업데이트하는 스 니펫을 확인했습니다. 코드 스 니펫은 성공적인 구매가 완료된 후 실행하려는 것입니다.

내 질문은 성공적인 구매 후 사용자를 위해 GUI가 업데이트되도록 코드 스 니펫을 넣는 위치입니다.

public class Levels extends SwarmActivity { 

    //static final String SKU_BUYLIVES = "buy5lives"; 
    static final String SKU_BUYLIVES = "android.test.purchased"; 

    IabHelper mHelper; 
    IInAppBillingService mService; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     moreLives = (Button)findViewById(R.id.moreLives); 

     moreLives.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       buyLives(); 
      } 
     }); 
    } 

    public void buyLives() { 

     final Dialog dialog = new Dialog(c); 
     dialog.setContentView(R.layout.buylives); 

     String base64EncodedPublicKey = a + b + d + e + f + g + h + i + j + k; 

     TextView title = (TextView)dialog.findViewById(R.id.question); 
     Button no = (Button)dialog.findViewById(R.id.no); 
     Button yes = (Button)dialog.findViewById(R.id.yes); 

     title.setText(c.getResources().getString(R.string.buyLivesQuestion)); 
     no.setText(c.getResources().getString(R.string.maybelater)); 
     yes.setText(c.getResources().getString(R.string.buy)); 

     // Create the helper, passing it our context and the public key to verify signatures with 
     mHelper = new IabHelper(Levels.this, base64EncodedPublicKey); 

     // start setup. this is asynchronous and the specified listener will be called once setup completes. 
     mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { 
      public void onIabSetupFinished(IabResult result) { 

       if (!result.isSuccess()) { 
        // there was a problem. 
        complain("An error has occurred. We apologize for the inconvenience. " + c.getResources().getString(R.string.problem1) + " " + result); 
        return; 
       } 

       // IAB is fully set up. Now, let's get an inventory of stuff we own. 
       mHelper.queryInventoryAsync(mGotInventoryListener);     
      } 
     }); 

     yes.setOnClickListener(new OnClickListener() { 
      public void onClick(View arg0) {     
       mHelper.launchPurchaseFlow(Levels.this, SKU_BUYLIVES, 10001, mPurchaseFinishedListener, "payload"); 
       dialog.dismiss(); 

// the below ~14 lines is the code that I want to call to update the GUI for the user. this block of code has been all over the place. this is just the last spot I tested it at. 
       SharedPreferences settings = getSharedPreferences("level_SP", 0); 
       livesCount = settings.getInt("livesTotal1", 0); 
       remainderTimeStamp = settings.getLong("remainderTimeStamp1", 0); 

       livesCount = 5; 
       remainderTimeStamp = 0; 

       SharedPreferences.Editor editor = settings.edit(); 
       editor.putInt("livesTotal1", livesCount); 
       editor.putLong("remainderTimeStamp1", remainderTimeStamp); 
       editor.commit(); 

       livesCountTV.setText(c.getResources().getString(R.string.livesCount) + " " + livesCount); 
       livesCounterTV.setText(c.getResources().getString(R.string.livesCounter) + " FULL!"); 
      } 
     }); 

     no.setOnClickListener(new OnClickListener() { 
      public void onClick(View arg0) { 
       dialog.dismiss(); 
      } 
     }); 
     dialog.show(); 
    } 

    // listener that's called when we finish querying the items and subscriptions we own. 
    IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() { 
     public void onQueryInventoryFinished(IabResult result, Inventory inventory) { 
      if(result.isFailure()) { 

       complain(c.getResources().getString(R.string.sorryerror) + c.getResources().getString(R.string.failedtoquery) + " " + result); 
       return; 
      } else if(inventory.hasPurchase(SKU_BUYLIVES)) { 
       mHelper.consumeAsync(inventory.getPurchase(SKU_BUYLIVES), null); 
      } 
     } 
    }; 

    // callback for when a purchase is finished 
    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() { 
     public void onIabPurchaseFinished(IabResult result, Purchase purchase) { 

      // this appears to the user immediately after purchasing. 
      if(result.isFailure()) { 

       complain(c.getResources().getString(R.string.sorryerror) + result); 

      } else if(purchase.getSku().equals(SKU_BUYLIVES)) { 

       alert(c.getResources().getString(R.string.livesbought)); 

       try { 
        Bundle ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null); 
        int response = ownedItems.getInt("RESPONSE_CODE"); 

        if (response == 0) { 
         // success 
         Toast.makeText(Levels.this, "SUCCESS", Toast.LENGTH_LONG).show(); 

         try { 
          mService.consumePurchase(3, getPackageName(), SKU_BUYLIVES); 

// this Toast is never seen. 
          Toast t = Toast.makeText(Levels.this, "PURCHASE CONSUMED", Toast.LENGTH_LONG); 
          t.setGravity(Gravity.CENTER, 0, 0); 
          t.show(); 
         } catch (RemoteException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         }      
        } else { 
         // error 
// this Toast is never seen. 
         Toast.makeText(Levels.this, "ERROR", Toast.LENGTH_LONG).show(); 
        } 

       } catch (RemoteException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
       }  
      } 
      return; 
     } 
    }; 

    void complain(String message) { 
     alert("Error: " + message); 
    } 

    void alert(String message) { 
     AlertDialog.Builder bld = new AlertDialog.Builder(this); 
     bld.setMessage(message); 
     bld.setNeutralButton("OK", null); 
     bld.create().show(); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     if(mHelper != null) mHelper.dispose(); 
     mHelper = null; 
    } 
} 

답변

1

Google Play 구매 대화 상자를 열려면 구매 의도와 함께 startIntentSenderForResult() 메소드를 사용해야합니다. 이 대화 상자로 사용자가 완료되면 활동에 onActivityResult()이 호출됩니다. 필요한 경우 구매를 확인하고 GUI를 업데이트해야하는 곳입니다.

구매 대화 상자를 여는 방법의 예입니다.

public void buyProduct() { 
    PendingIntent buyIntent = ... // create your intent here 
    IntentSender sender = buyIntent.getIntentSender(); 
    try { 
     startIntentSenderForResult(sender, REQ_BUY_PRODUCT, new Intent(), 0, 0, 0); 
    } catch (SendIntentException e) { 
     Log.e(TAG, "", e); 
    } 
} 

이 두 방법은 사용자의 작업에 속하는 구매 의도

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == REQ_BUY_PRODUCT && resultCode == Activity.RESULT_OK) { 
     // here you verify data intent and update your GUI 
     ... 
     return; 
    } 
} 

을 처리하는 방법의 예입니다.

+0

예! 'onActivityResult()'메소드는 필자가 필요로하는 것이다. 그리고 fyi, 나는 이것을 작동시키기 위해'startIntentSenderForResult()'를 구현할 필요가 없었다. – Matt

+1

확인. IabHelper를 사용했다면, 당신을 위해이 작업을 수행했을 것입니다. –

관련 문제