2016-08-19 5 views
0

私はAndroid開発にかなり新しく、アプリの請求でGoogleを実装する方法を理解する上で大きな問題を抱えています。私は公式のGoogleドキュメントを読んだり、チュートリアルなどを読んだり、私には分かりやすいものは次のとおりです。それでも私はそれを働かせるのに苦労しています。 http://redappz.com/micro-transactions-tutorial-iap-for-android/Androidでのアプリ請求の実装が混乱していますか?

私がしたことは、すべてのヘルパークラスを単純なドライブからインポートしたことです。署名付きAPKのgoogle devアカウントを持っています。

購入はBボタンを表示するアクティビティA.活動に成功したとき、私は自分のアプリケーションのためにしたいロジックがあります。

public class WorkoutPlan1 extends AppCompatActivity { 

private Button WorkoutPlan1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_workoutplan1); 
     WorkoutPlan1 = (Button) findViewById(R.id.Workout1MoreInfo); 

任意のGoogle請求書を含む基本コード。基本的なボタンはGoogleの請求をオフにするだけです。

 public class WorkoutPlan1 extends AppCompatActivity { 

private Button WorkoutPlan1; 
//************************************************************************************************************************* 
// Debug tag, for logging 
static final String TAG = "test"; 
// SKUs for our products: the premium upgrade (non-consumable) 
static final String SKU_PREMIUM = "android.test.purchased"; 
// Does the user have the premium upgrade? 
boolean mIsPremium = false; 
// (arbitrary) request code for the purchase flow 
static final int RC_REQUEST = 1; 
// The helper object 
IabHelper mHelper; 
//************************************************************************************************************************* 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    //************************************************************************************************************************* 
    String base64EncodedPublicKey = "KEY GOES HERE FROM MY GOOGLE DEV"; 
    //It is recommended to add more security than just pasting it in your source code; 
    mHelper = new IabHelper(this, base64EncodedPublicKey); 
    Log.d(TAG, "Starting setup."); 
    mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { 
     public void onIabSetupFinished(IabResult result) { 
      Log.d(TAG, "Setup finished."); 

      if (!result.isSuccess()) { 
    // Oh noes, there was a problem. 
       Log.d(TAG, "Problem setting up In-app Billing: " + result); 
      } 
    // Hooray, IAB is fully set up! 
      // mHelper.queryInventoryAsync(mGotInventoryListener); 
     } 
    }); 
     //************************************************************************************************************************* 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_workoutplan1); 
     WorkoutPlan1 = (Button) findViewById(R.id.Workout1MoreInfo); 
} 
    //************************************************************************************************************************* 
IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() { 
    public void onQueryInventoryFinished(IabResult result, Inventory inventory) { 
     Log.d(TAG, "Query inventory finished."); 
     if (result.isFailure()) { 
      Log.d(TAG, "Failed to query inventory: " + result); 
      return; 
     } 
     else { 
      Log.d(TAG, "Query inventory was successful."); 
      // does the user have the premium upgrade? 
      mIsPremium = inventory.hasPurchase(SKU_PREMIUM); 

      // update UI accordingly 
      WorkoutPlan1.setVisibility(View.VISIBLE); 
      Log.d(TAG, "User is " + (mIsPremium ? "PREMIUM" : "NOT PREMIUM")); 
     } 
     Log.d(TAG, "Initial inventory query finished; enabling main UI."); 
    } 
}; 

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() { 
    public void onIabPurchaseFinished(IabResult result, Purchase purchase) { 
     if (result.isFailure()) { 
      Log.d(TAG, "Error purchasing: " + result); 
      return; 
     } 
     else if (purchase.getSku().equals(SKU_PREMIUM)) { 
      // give user access to premium content and update the UI 
      WorkoutPlan1.setVisibility(View.VISIBLE); 
     } 
    } 
}; 

@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."); 
    } 
} 

私はボタンにonclickリスナーを持っていません。しかし、私はどのように私は支払い/購入の流れを呼び出すか知りません。

この時点で私はちょうど失われた感じとして正しい方向に私を指すようにAT ALLすべてのヘルプはそう、いただければ幸いです。

ありがとうございました

+0

http://stackoverflow.com/questions/8735931/android-in-app-billing-tutorial?rq=1 –

答えて

0

購入フローを呼び出すには、IabHelperオブジェクトを使用します。例えば:

試み{ mHelper.launchPurchaseFlow(この、SKU_BUNDLE、RC_REQUEST、mPurchaseFinishedListener、 ペイロード)。 } catch(IabAsyncInProgressException e){ (「購入フローの起動中にエラーが発生しました。別の非同期操作が進行中です。 }

...だから私はあなたのボタンにonClickListener内でこのコードスニペットが含まれます。次に、OnIabPurchaseFinishedListenerを使用して購入の応答を処理します。

P.S.モバイルデバイスでこれに応答していますので、フォーマットが貧弱であると言い訳します

+0

ありがとうございます。更新されます – uwotm8

+0

問題ありません!私をつけておく。 – DaveNOTDavid

+0

残念ながら、貼り付けたときに多くのエラーが表示されています。私はおそらくやり直して、やり直して、もう一度Googleドキュメントに従うことをお勧めしますか?ありがとうございます – uwotm8

関連する問題