0

I wrote some codes to check user if she/he bought the premium version, but after purchasing , again it check the user and this process use about 15 second(every time that the user open the app) and also after purchase when the user is offline and it can not check if the user purchased the app or not(but actually the user bought it before) it consider it as a demo version and the user can not use full content. what should i do? i want when the user buy the full version then can use if anytime offline.

public class MainActivity extends AppCompatActivity implements IabHelper.OnIabSetupFinishedListener 
    ,  IabHelper.QueryInventoryFinishedListener{

    private ProgressDialog progressDialog;
        private CoordinatorLayout coordinatorLayout;
        private IabHelper iabHelper;
        private static final String PRODUCT_PREMIUM_ACCOUNT="testapk";
        public boolean isPremiumAccount=false;
        private Button upgradeToPremiumButton;

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

    progressDialog =new ProgressDialog(this);
    progressDialog.setTitle("checking purches status");
    progressDialog.setMessage("please wiat...");
    progressDialog.setCancelable(false);
    progressDialog.show();
    coordinatorLayout=(CoordinatorLayout)findViewById(R.id.coor_main);
    upgradeToPremiumButton=(Button)findViewById(R.id.button_main_upgrade);
    upgradeToPremiumButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            purchasePremiumAccount();
        }  });
    checkAccount();}

public void btnTblcontentClick(View v){
if(isPremiumAccount){
    Intent i = new Intent(this,tblOfcontent.class);
    startActivity(i);
        }else{
            Toast.makeText(getApplicationContext(), "buy full version ",
            Toast.LENGTH_LONG).show();}}


public void btnSearchClick(View v){
    if(isPremiumAccount){
        Intent i = new Intent(this,search.class);
        startActivity(i);
    }else{
        Toast.makeText(getApplicationContext(), "buy full version ",}}
                Toast.LENGTH_LONG).show();


private void checkAccount(){

    iabHelper=new IabHelper(this,"my RSA key");
    iabHelper.startSetup(this);}

@Override
public void onIabSetupFinished(IabResult result) {
    if (result.isSuccess()){
        List<String> products = new ArrayList<>();
        products.add(PRODUCT_PREMIUM_ACCOUNT);
        iabHelper.queryInventoryAsync(true,products,this);
    }else {
        showError("sorry...process not complete");}}

@Override
public void onQueryInventoryFinished(IabResult result, Inventory inv) {

    if (result.isSuccess()){
        Purchase purchase= inv.getPurchase(PRODUCT_PREMIUM_ACCOUNT);
        if (purchase!=null){
            changeStateToPremium();
        }
    }else {
        showError(" you just can use demo version");}
    progressDialog.hide();}

private void showError(String message){
    Snackbar.make(coordinatorLayout,message,Snackbar.LENGTH_LONG).show();}


private void changeStateToPremium(){
    isPremiumAccount=true;
    upgradeToPremiumButton.setVisibility(View.GONE);}


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

private void purchasePremiumAccount(){

    iabHelper.launchPurchaseFlow(this, PRODUCT_PREMIUM_ACCOUNT, 1001, new IabHelper.OnIabPurchaseFinishedListener() {
        @Override
        public void onIabPurchaseFinished(IabResult result, Purchase info) {
            if (result.isSuccess()){
                if(info!=null){
                    changeStateToPremium();
                }
            }else {
                showError("purches not complete");}}});}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode==1001){
        iabHelper.handleActivityResult(requestCode,resultCode,data);
    }else {
        super.onActivityResult(requestCode,resultCode,data);}}}
zahra.s
  • 27
  • 1
  • 8

1 Answers1

0

When the transaction is successful then you can save Boolean flag to SharedPreference. If you find that Boolean flag true, then bypass checking methods.

Things to check.

  • If user clear cache then api would be called again.
  • First change SharedPreference flag when item is purchased then second when you know user has already purchased item.
  • If your item is subscription type then also save expire time with item id.
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • http://uupload.ir/files/519h_s1.png http://uupload.ir/files/ov3o_s2.png i tried to use but it does not work i don't know how can i use it properly – zahra.s Jul 23 '18 at 11:45