当我在外面点按时,我的In app billing对话框消失了

时间:2014-08-14 07:03:07

标签: android dialog in-app-purchase in-app-billing

我已经在Android应用程序购买中做了一个演示,我已经创建了一个测试产品并购买它用于测试目的,一切都工作得非常好,但唯一的问题是,当我点击应用程序内的对话框时,它会消失,我想做一些类似“setCancellable(false)”的内容,请建议我,我需要帮助,我的代码如下: in app dialog

package com.medicineqod.activity;

import inapUtils.IabHelper;
import inapUtils.IabHelper.OnConsumeFinishedListener;
import inapUtils.IabHelper.OnIabPurchaseFinishedListener;
import inapUtils.IabHelper.OnIabSetupFinishedListener;
import inapUtils.IabHelper.QueryInventoryFinishedListener;
import inapUtils.IabResult;
import inapUtils.Inventory;
import inapUtils.Purchase;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Locale;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.drm.DrmStore.ConstraintsColumns;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.medicineqod.FragmentAbstractActivity;
import com.medicineqod.R;
import com.medicineqod.DBAdapter.DataBaseHelper;
import com.medicineqod.model.Question;
import com.medicineqod.utility.CommonVariables;

/**
 * Created by basheer on 4/26/14.
 */
public class QuestionOFDayActivity extends FragmentAbstractActivity 

/* OnIabSetupFinishedListener, OnIabPurchaseFinishedListener */{
    // When requested, this adapter returns a WallpaperCollectionFragment,
    // representing an object in the collection.
    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;
    ImageView btnNext, btnPrev;
    static TextView tvError;
    TextView tvDate;
    TextView btnHome;
    TextView tvTitle;
    int mTotalCount;
    int mPosition = 0;
    int mActivity = 0;
    ArrayList<Question> qusList = new ArrayList<Question>();

    // edited by jigar
    private static final String TAG = "com.medicineqod.inappbilling";
    static IabHelper mHelper;

    static final String ITEM_SKU = "com.medicineqod.product.one";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quizoftheday);
        // ViewPager and its adapters use support library
        // fragments, so use getSupportFragmentManager.
        // Edited by jigar,....

        showCustomeAlertValidation(QuestionOFDayActivity.this);
        DataBaseHelper bBase = new DataBaseHelper(this);
        mActivity = getIntent().getIntExtra(CommonVariables.ACTIVITY, 0);
        if (mActivity == CommonVariables.FavQusAct) {
            qusList = bBase.getFavQuestions();
            mTotalCount = qusList.size();

        } else if (mActivity == CommonVariables.PastQusAct) {
            qusList = bBase.getPastQuestions();
            mTotalCount = qusList.size();

        } else if (mActivity == CommonVariables.QusOfTheDay) {
            qusList = bBase.getQuestions();
            mTotalCount = qusList.size();

        } else {
            qusList = bBase.getQuestions();
            mTotalCount = qusList.size();
        }
        if (qusList.size() == 0) {
            return;
        }
        mPosition = getIntent().getIntExtra("postion", 0);
        Collections.reverse(qusList);
        if (mPosition == -1) {
            mPosition = qusList.size() - 1;
        }

        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        mViewPager.setCurrentItem(mPosition);

        tvError = (TextView) findViewById(R.id.error_tv);
        tvDate = (TextView) findViewById(R.id.topbar_date_tv);
        tvTitle = (TextView) findViewById(R.id.topbar_title_tv);

        btnNext = (ImageView) findViewById(R.id.bottom_next_img);
        btnPrev = (ImageView) findViewById(R.id.bottom_prev_img);
        btnNext.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mTotalCount - 1 > mPosition) {
                    mPosition++;
                    mViewPager.setCurrentItem(mPosition);
                }

            }
        });

        btnPrev.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (0 < mPosition) {
                    mPosition--;
                    mViewPager.setCurrentItem(mPosition);
                }
            }
        });
        mViewPager
                .setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

                    @Override
                    public void onPageSelected(int position) {
                        mPosition = position;
                        btnNext.setVisibility(View.VISIBLE);
                        btnPrev.setVisibility(View.VISIBLE);
                        if (mPosition == 0) {
                            btnPrev.setVisibility(View.INVISIBLE);
                        } else if (mPosition == mTotalCount - 1) {
                            btnNext.setVisibility(View.INVISIBLE);
                        } else {
                            btnNext.setVisibility(View.VISIBLE);
                            btnPrev.setVisibility(View.VISIBLE);
                        }
                        tvDate.setText("" + qusList.get(position).getDate());
                        setTiltle(qusList.get(mPosition).getDate());

                    }

                    @Override
                    public void onPageScrolled(int arg0, float arg1, int arg2) {

                    }

                    @Override
                    public void onPageScrollStateChanged(int arg0) {

                    }
                });
        tvDate.setText("" + qusList.get(mPosition).getDate());
        setTiltle(qusList.get(mPosition).getDate());
        btnHome = (TextView) findViewById(R.id.topbar_left_tv);
        if (mActivity == CommonVariables.FavQusAct) {
            btnHome.setText("Back");
            btnHome.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_back,
                    0, 0, 0);
        } else if (mActivity == CommonVariables.PastQusAct) {
            btnHome.setText("Back");
            btnHome.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_back,
                    0, 0, 0);

        } else if (mActivity == CommonVariables.QusOfTheDay) {
            // btnHome.setText("Back");
            // btnHome.setCompoundDrawablesWithIntrinsicBounds(null, null, null,
            // null);
        }

        btnHome.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransition(R.anim.slide_in_right,
                        R.anim.slide_out_right);
            }
        });

        // System.out.println(mPosition + "::::" + (mTotalCount - 1));
        if (qusList.size() == 0 || qusList.size() == 1) {
            btnPrev.setVisibility(View.INVISIBLE);
            btnNext.setVisibility(View.INVISIBLE);
        } else if (mPosition == 0) {
            btnPrev.setVisibility(View.INVISIBLE);
        } else if (mPosition == mTotalCount - 1) {
            btnNext.setVisibility(View.INVISIBLE);
        } else {
            btnNext.setVisibility(View.VISIBLE);
            btnPrev.setVisibility(View.VISIBLE);
        }

    }

    private void setTiltle(String date) {
        if (mActivity == CommonVariables.FavQusAct) {
            tvTitle.setText(""
                    + getApplication().getResources().getString(
                            R.string.title_fav_qus));
        } else if (getCurrentDate().equals(date)) {
            tvTitle.setText(""
                    + getApplication().getResources().getString(R.string.qod));
        } else {
            tvTitle.setText(""
                    + getApplication().getResources().getString(
                            R.string.title_past_qus));
        }
    }

    public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            fragment = new QuizCollectionFragment();
            Bundle args = new Bundle();
            args.putSerializable(QuizCollectionFragment.ARG_OBJECT,
                    (Serializable) qusList.get(position));
            args.putInt(QuizCollectionFragment.QUZI_POSITION, position);
            fragment.setArguments(args);

            return fragment;
        }

        @Override
        public int getCount() {
            return mTotalCount;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            return "good".toUpperCase(l);
        }
    }

    public void setOnAnswer(String mPerformance, String mAswerView,
            String mAswerQue, String mFavourite, int mPosition,
            String mPercentage) {
        if (qusList.size() > 0 && mPosition < qusList.size()) {
            Question mQuizQuestion = qusList.get(mPosition);
            mQuizQuestion.setAnsque(mAswerQue);
            mQuizQuestion.setPerformance(mPerformance);
            mQuizQuestion.setAnsview(mAswerView);
            mQuizQuestion.setFavourite(mFavourite);
            mQuizQuestion.setPercentage(mPercentage);
            qusList.set(mPosition, mQuizQuestion);
        }

    }

    public void refreshTheViewPage(int position) {
        if (mActivity == CommonVariables.FavQusAct) {
            tvDate.setText("");
            qusList.remove(mPosition);
            mTotalCount = qusList.size();
            mSectionsPagerAdapter.notifyDataSetChanged();
            mViewPager.setAdapter(mSectionsPagerAdapter);
            if (mTotalCount - 1 < mPosition) {
                mPosition = mTotalCount - 1;
            }
            mViewPager.setCurrentItem(mPosition);
            btnNext.setVisibility(View.VISIBLE);
            btnPrev.setVisibility(View.VISIBLE);

            if (qusList.size() == 0 || qusList.size() == 1) {
                btnNext.setVisibility(View.INVISIBLE);
                btnPrev.setVisibility(View.INVISIBLE);
            } else if (mPosition == 0) {
                btnPrev.setVisibility(View.INVISIBLE);
            } else if (mPosition == mTotalCount - 1) {
                btnNext.setVisibility(View.INVISIBLE);
            } else {
                btnNext.setVisibility(View.VISIBLE);
                btnPrev.setVisibility(View.VISIBLE);
            }

        }
    }

    // Popup of subscribing app..
    public void showCustomeAlertValidation(final Context context) {
        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.popup);
        dialog.setCancelable(false);
        dialog.getWindow().setBackgroundDrawable(
                new ColorDrawable(android.graphics.Color.TRANSPARENT));

        final Button btn_subscribe = (Button) dialog
                .findViewById(R.id.btn_subscribe);

        btn_subscribe.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // Subscribing code here...!!!
                mHelper.launchPurchaseFlow(QuestionOFDayActivity.this,
                        ITEM_SKU, 10001, mPurchaseFinishedListener,
                        "mypurchasetoken");
                dialog.dismiss();

            }
        });

        dialog.show();
    }

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

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        String base64EncodedPublicKey = "MIIBIjANyOvuqXSF8qd4NAapQdRohAajUA3/N/L9XoD21B7lVqZnICXh1dFEnnVvNyhK+ws1Cr5YsPvIAprHvsQNJ2xtEGypNivk/AA6N91OY0r2dtZJZHGpSxDSsfHTfKk5/4CaW1itbdIo+JiGTieJyHIvMFdWSkLqaxR3JWy+0KwTHvzMrR15oClM9v8fKjNT6M8qxlhnyzLDd7G2/ifBIPTJjfdvWHoXaP/rHa2MbN2acPwvbbwIDAQAB";

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

    }

    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();
                System.out
                        .println(":::::::::::::::::::::Purchase consumed successfuly::::::::::::::::::::");
            }

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

            if (result.isFailure()) {
                // Handle failure
            } else {
                mHelper.consumeAsync(inventory.getPurchase(ITEM_SKU),
                        mConsumeFinishedListener);
            }
        }
    };
    IabHelper.OnConsumeFinishedListener mConsumeFinishedListener = new IabHelper.OnConsumeFinishedListener() {
        public void onConsumeFinished(Purchase purchase, IabResult result) {

            if (result.isSuccess()) {
                System.out
                        .println("::::::::::::::::::::::::Purchase consumehas been finished successfully:::::::::::::::::::::");
            } else {
                // handle error
                System.out
                        .println("::::::::::::::::::::::::Purchase consumehas failed:::::::::::::::::::::");
            }
        }
    };



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

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if (mHelper != null)
            mHelper.dispose();
        mHelper = null;
    }

}

1 个答案:

答案 0 :(得分:1)

这是你需要做的,以限制它。

dialog.setCanceledOnTouchOutside(false);

这将确保在对话框区域外触摸时不会关闭对话框。