2017-06-27 5 views
0

私はAndroidが初めてで、私のアプリのアプリ課金を実装しようとしています。私が持っているのは、あなたをInAppBillingActivityに連れて行くボタンです。アクティビティはそのことです。私がしたいのは、購入が成功したときに、そのアクティビティにあなたを連れてきたボタンが消えてしまうことです。私はそれを消すことができません、そして、私はそれを正しくやっていないかもしれません。私はそれが来た断片に戻って戻るボタンを押す必要があり、それは正しく行っていないようだ。だからここに私のコードです:私はボタンが消える取得しようとするすべてのさまざまな方法をしようとしているが、私はそれを実現することができませんAndroid - アプリ内課金が正常に完了した後にボタンが消えるようにする

private InAppBillingActivity activity; 
public Button btnPurchase; 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    btnPurchase = (Button) rootView.findViewById(R.id.btnPurchase); 
    activity = new InAppBillingActivity(); 
    setOnClick(); 
    //tried to do it this way 
    if (activity.isPurchased == true){ 
     btnPurchase.setVisibility(View.GONE); 
    } 
} 

public void setOnClick() { 
    btnPurchase.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(CheckListGatherFragment.this.getActivity(), InAppBillingActivity.class); 
      startActivity(intent); 
      //tried doing it this way 
      if (activity.isPurchased == true){ 
       btnPurchase.setVisibility(View.GONE); 
      } 
     } 
    }); 
} 

//tried doing it this way 
@Override 
public void onResume() { 
    super.onResume(); 

    if (activity.isPurchased == true){ 
     btnPurchase.setVisibility(View.GONE); 
    } 
} 

:ここ

public class InAppBillingActivity extends AppCompatActivity { 

    private static final String TAG = "com.android.inappbilling"; 
    public IabHelper mHelper; 
    private Button btnInAppBilling; 

    public boolean isPurchased; 
    private CheckListGatherFragment checkListGatherFragment; 
    static final String ITEM_SKU = "android.test.purchased"; 
    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_in_app_billing); 
     btnInAppBilling = (Button) findViewById(R.id.btnInAppBilling); 

     checkListGatherFragment = new CheckListGatherFragment(); 
     String base64EncodedPublicKey = ""; 

     mHelper = new IabHelper(this, base64EncodedPublicKey); 

     mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { 
      public void onIabSetupFinished(IabResult result) 
      { 
       if (!result.isSuccess()) { 
        Log.d(TAG, "In-app Billing setup failed: " + 
          result); 
       } else { 
        Log.d(TAG, "In-app Billing is set up OK"); 
       } 
      } 
     }); 
    } 

public void buyClick(View view){ 
    mHelper.launchPurchaseFlow(this, ITEM_SKU, 10001, 
      mPurchaseFinishedListener, "mypurchasetoken"); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, 
           Intent data) 
{ 
    if (!mHelper.handleActivityResult(requestCode, 
      resultCode, data)) { 
     super.onActivityResult(requestCode, resultCode, data); 
    } 
} 

IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener 
     = new IabHelper.OnIabPurchaseFinishedListener() { 
    public void onIabPurchaseFinished(IabResult result, 
             Purchase purchase) 
    { 
     if (result.isFailure()) { 
      // Handle error 
      return; 
     } 
     else if (purchase.getSku().equals(ITEM_SKU)) { 
      consumeItem(); 
      btnInAppBilling.setEnabled(false); 
     } 

    } 
}; 

public void consumeItem() { 
    mHelper.queryInventoryAsync(mReceivedInventoryListener); 
} 

IabHelper.QueryInventoryFinishedListener mReceivedInventoryListener 
     = new IabHelper.QueryInventoryFinishedListener() { 
    public void onQueryInventoryFinished(IabResult result, 
             Inventory inventory) { 

     if (result.isFailure()) { 
      // Handle failure 
      return; 
     } else { 
      mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU), 
        mConsumeFinishedListener); 
     } 
    } 
}; 

IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = 
     new IabHelper.OnConsumeFinishedListener() { 
      public void onConsumeFinished(Purchase purchase, 
              IabResult result) { 

       if (result.isSuccess()) { 

        //Here is where I am trying to get it to disappear 
       checkListGatherFragment.btnPurchase.setVisibility(View.GONE); 
       //Tried a boolean to get it to disappear 
        isPurchased = true; 
       } else { 
        // handle error 
        return; 
       } 
      } 
     }; 

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


} 

は私のフラグメントの一部であります。購入は成功しましたが、ボタンは消えません。どんな助けでも大歓迎です。ありがとう!

答えて

0

フラグメントに戻ると、フラグメントはすでに作成されているため、onCreateViewもonResumeも呼び出されません。

あなたができることは、呼び出し元のフラグメントにonActivityResultを実装し、startActivityForResult(intent、requestCode)を使用してInAppBillingActivityに結果を返すことです。

はあなたの呼び出し元の断片でこれらの変更を行います:

public void setOnClick() { 
    btnPurchase.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(CheckListGatherFragment.this.getActivity(), InAppBillingActivity.class); 
      startActivityForResult(intent, 0); 
     } 
    }); 
} 

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

    if (resultCode == InAppBillingActivity.RESULT_SUCCESS) { 
     btnPurchase.setVisibility(View.GONE); 
    } else { 
     //handle error here 
    } 
} 

そして、あなたのInAppBillingActivityでこれらの変更:

public static final int RESULT_SUCCESS = 1; 
public static final int RESULT_FAILED = 0; 

IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = 
     new IabHelper.OnConsumeFinishedListener() { 
      public void onConsumeFinished(Purchase purchase, 
              IabResult result) { 

       if (result.isSuccess()) { 
        InAppBillingActivity.this.setResult(RESULT_SUCCESS); 
       } else { 
        InAppBillingActivity.this.setResult(RESULT_FAILED); 
       } 

       InAppBillingActivity.this.finish(); 
      } 
     }; 

それが動作するかどうか、私に教えてください。ここには方法です。

+0

応答ありがとう。私はあなたが提案したものを試しても機能しませんでしたが、購入に間違いがあります:06-27 12:59:54.459 13159-13159/mk E/IABUtil/Security:購入確認に失敗しました:データがありません。 06-27 12:59:54.459 13159-13159/mk E/IabHelper:アプリ内課金エラー:SKUの購入署名検証に失敗しましたandroid.test.purchased購入が正しく行われていない可能性があります。それは購入されたが、検証は正しくないと言います。どう思いますか?今、テスト目的で使っています。 – Karna

+0

これについてはあまり詳しく説明できませんが、以前はアプリ内課金で働いたことがありません。しかし、そのエラーの説明から、おそらく間違った公開鍵を使用していると思います。 – Tharkius

+0

ありがとう!コードは機能しました!私はその問題を解決したstackoverflowのページを見つけました。 – Karna

-1

レイアウトにフラグメントを追加した場所はどこですか?

+0

あなたはその質問のメインポストにコメントを書くべきです – Tharkius

+0

私はそこにコメントを追加することができません。私は最低50の評判を持たなければならないようです。 @Tharkius –

関連する問題