2017-07-21 1 views
2

Je cherche à étudier dans App Billing. J'ai créé une application de test. Je peux voir mon dans le produit app insered en jeu console, avec getSkuDetails, mais quand je commence launchPurchaseFlow, l'interface d'acheter dans le produit application ne démarre pas, et je ne peux voir que le journal (Log son ok)Dans la facturation de l'application no ui

public class MainActivity extends Activity implements View.OnClickListener { 

    Button compra,click,controllo; 
    private IabHelper.QueryInventoryFinishedListener mQueryFinishedListener; 

    IInAppBillingService mService; 

    ServiceConnection mServiceConn = new ServiceConnection() { 
     @Override 
     public void onServiceDisconnected(ComponentName name) { 
      mService = null; 
     } 

     @Override 
     public void onServiceConnected(ComponentName name, 
             IBinder service) { 
      mService = IInAppBillingService.Stub.asInterface(service); 
     } 
    }; 

    private static final String TAG="INappBILLING"; 
    IabHelper mHelper; 


    static final String ITEM_SKU="dsada"; 
    public monky.myapplication.util.IabHelper.OnIabPurchaseFinishedListener 
    mPurchaseFinishedListener; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Intent serviceIntent = 
       new Intent("com.android.vending.billing.InAppBillingService.BIND"); 
     serviceIntent.setPackage("com.android.vending"); 
     bindService(serviceIntent, mServiceConn, Context.BIND_AUTO_CREATE); 

     compra=(Button) findViewById(R.id.buybtn); 
     compra.setOnClickListener(this); 
     click=(Button)findViewById(R.id.clickbtn); 
     click.setOnClickListener(this); 
     controllo=(Button) findViewById(R.id.controllo); 
     controllo.setOnClickListener(this); 



     String base64EncodedPublicKey="mykey"; 

     mHelper=new IabHelper(this,base64EncodedPublicKey); 

     mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { 
      public void onIabSetupFinished(IabResult result) { 
       if (!result.isSuccess()) { 
        // Oh no, there was a problem. 
        Log.d(TAG, "Problem setting up In-app Billing: " + result); 
       } 
       // Hooray, IAB is fully set up! 
      } 
     }); 



     } 





    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     if (mService != null) { 
      unbindService(mServiceConn); 
     } 
    } 

    @Override 
    public void onClick(View view) { 
     switch (view.getId()){ 

      case R.id.clickbtn: 

       List additionalSkuList = new ArrayList(); 
       additionalSkuList.add(ITEM_SKU); 
       mHelper.queryInventoryAsync(true, additionalSkuList, 
         mQueryFinishedListener); 


       mQueryFinishedListener = new IabHelper.QueryInventoryFinishedListener() { 
        public void onQueryInventoryFinished(IabResult result, Inventory inventory) 
        { 
         if (result.isFailure()) { 
          Log.i(TAG, " - Prezzo Error"); 
          return; 
         } 


         String prezzo = inventory.getSkuDetails(ITEM_SKU).getPrice(); 

         String titolo = inventory.getSkuDetails(ITEM_SKU).getTitle(); 

         String desc = inventory.getSkuDetails(ITEM_SKU).getDescription(); 

         String type = inventory.getSkuDetails(ITEM_SKU).getType(); 

         String SkU = inventory.getSkuDetails(ITEM_SKU).getSku(); 

         Toast.makeText(getApplicationContext(),"Titolo: "+titolo+" Desc "+desc+" Prezzo "+prezzo+" Tipo "+type+"SKU"+SkU ,Toast.LENGTH_LONG).show(); 
         Log.i(TAG, " - Prezzo " + prezzo); 

         // update the UI 
        } 
       }; 

       break; 

      case R.id.buybtn: 
       if (!mHelper.isAsyncInProgress()){ 

        mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001, 
          mPurchaseFinishedListener,""); 
        Log.i(TAG, " sono passato ");} 
       else { 
        Log.i(TAG, " Non sono passato "); 
       } 



       break; 


      case R.id.controllo: 
       controlloaq(); 
       break; 

     } 
    } 

    private void controlloaq() { 

     IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener 
       = new IabHelper.OnIabPurchaseFinishedListener() { 
      public void onIabPurchaseFinished(IabResult result, Purchase purchase) 
      { 
       if (purchase.getSku().equals(ITEM_SKU)) { 
        Log.i(TAG, " passato e comprato"); 
       } 

       if (result.isFailure()) { 
        Log.i(TAG, "Error purchasing: " + result); 
        return; 
       } 



      } 
     }; 
    } 

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

     Log.d(TAG, "onActivityResult(" + requestCode + "," + resultCode + "," 
       + data); 

     // Pass on the activity result to the helper for handling 
     if (!mHelper.handleActivityResult(requestCode, resultCode, data)) { 
      super.onActivityResult(requestCode, resultCode, data); 
     } else { 
      Log.d(TAG, "onActivityResult handled by IABUtil."); 
     } 
    } 
} 

Répondre

0

Je pense que votre code est presque correct. Qu'est-ce qui se passe est votre mPurchaseFinishedListener ne se déclenche pas parce que vous l'avez placé dans une fonction, controlloaq() par conséquent, il ne sera exécuté que si vous appelez controlloaq().

En regardant le Google In App Billing sample officiel, vous n'êtes pas censé l'enfermer dans une fonction, sauf si vous savez ce que vous faites.

// Callback for when a purchase is finished 
    IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() { 
     public void onIabPurchaseFinished(IabResult result, Purchase purchase) { 
      Log.d(TAG, "Purchase finished: " + result + ", purchase: " + purchase); 

      // if we were disposed of in the meantime, quit. 
      if (mHelper == null) return; 

      if (result.isFailure()) { 
       complain("Error purchasing: " + result); 
       setWaitScreen(false); 
       return; 
      } 
      if (!verifyDeveloperPayload(purchase)) { 
       complain("Error purchasing. Authenticity verification failed."); 
       setWaitScreen(false); 
       return; 
      } 

      Log.d(TAG, "Purchase successful."); 

      if (purchase.getSku().equals(SKU_GAS)) { 
       // bought 1/4 tank of gas. So consume it. 
       Log.d(TAG, "Purchase is gas. Starting gas consumption."); 
       try { 
        mHelper.consumeAsync(purchase, mConsumeFinishedListener); 
       } catch (IabAsyncInProgressException e) { 
        complain("Error consuming gas. Another async operation in progress."); 
        setWaitScreen(false); 
        return; 
       } 
      } 
      else if (purchase.getSku().equals(SKU_PREMIUM)) { 
       // bought the premium upgrade! 
       Log.d(TAG, "Purchase is premium upgrade. Congratulating user."); 
       alert("Thank you for upgrading to premium!"); 
       mIsPremium = true; 
       updateUi(); 
       setWaitScreen(false); 
      } 
      else if (purchase.getSku().equals(SKU_INFINITE_GAS_MONTHLY) 
        || purchase.getSku().equals(SKU_INFINITE_GAS_YEARLY)) { 
       // bought the infinite gas subscription 
       Log.d(TAG, "Infinite gas subscription purchased."); 
       alert("Thank you for subscribing to infinite gas!"); 
       mSubscribedToInfiniteGas = true; 
       mAutoRenewEnabled = purchase.isAutoRenewing(); 
       mInfiniteGasSku = purchase.getSku(); 
       mTank = TANK_MAX; 
       updateUi(); 
       setWaitScreen(false); 
      } 
     } 
    };