应用程序重新启动后重置

时间:2015-03-02 04:56:07

标签: android in-app-purchase

我正在尝试开发一个简单的应用程序,当我购买活动时,它会启用按钮。

我的代码工作正常,但问题是当我退出应用程序并重新打开它时,之前在购买后启用的按钮会被禁用,但它不允许再次重新购买。

(我需要按时购买)

那么当我重新启动应用程序时如何保持启用的按钮保持启用状态?

这是我的代码:

public class MainScreen extends ActionBarActivity {

private static final String TAG = "com.aseng90_test.smiap2";
IabHelper mHelper;
static final String ITEM_SKU = "com.aseng90_test.smiap2_button5";
static final String ITEM_SKU2 = "com.aseng90_test.smiap2_buyact2";
static final String ITEM_SKU3 = "com.aseng90_test.smiap2_buyact3";
static final String ITEM_SKU4 = "com.aseng90_test.smiap2_buyall";

public Button Activity1;
public Button Activity2;
public Button Activity3;


public Button buyButton;
public Button buyAct2;
public Button buyAct3;
public Button buyAll;


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

    buyButton = (Button) findViewById(R.id.buyButton);
    buyAct2 = (Button) findViewById(R.id.buyact2);
    buyAct3 = (Button) findViewById(R.id.buyact3);
    buyAll = (Button) findViewById(R.id.buyall);


    Activity1 = (Button) findViewById(R.id.act1);
    Activity2 = (Button) findViewById(R.id.act2);
    Activity3 = (Button) findViewById(R.id.act3);

    Activity1.setEnabled(false);
    Activity2.setEnabled(false);
    Activity3.setEnabled(false);

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

public void  buyAct2 (View view){
    mHelper.launchPurchaseFlow(this, ITEM_SKU2, 10002, mPurchaseFinishedListener, "buyact2");
}

public void buyAct3 (View view){
    mHelper.launchPurchaseFlow(this,ITEM_SKU3, 10003, mPurchaseFinishedListener, "buyact3");
}

public void buyAll (View view){
    mHelper.launchPurchaseFlow(this, ITEM_SKU4,10004, mPurchaseFinishedListener, "buyall");
}

@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;
        }
         if (purchase.getSku().equals(ITEM_SKU)) {
            Activity1.setEnabled(true);
            buyButton.setEnabled(false);
        }

         if (purchase.getSku().equals(ITEM_SKU2)){
        Activity2.setEnabled(true);
        buyAct2.setEnabled(false);
    }

       if (purchase.getSku().equals(ITEM_SKU3)){
            Activity3.setEnabled(true);
            buyAct3.setEnabled(false);
        }

      if (purchase.getSku().equals(ITEM_SKU4)) {
            Activity1.setEnabled(true);
            Activity2.setEnabled(true);
            Activity3.setEnabled(true);

            buyAll.setEnabled(false);
            buyButton.setEnabled(false);
            buyAct2.setEnabled(false);
            buyAct3.setEnabled(false);
        }
    }


};

public void Activity1 (View view)
{
    startActivity(new Intent(MainScreen.this, Click1.class));

}

public void Activity2 (View view){
    startActivity(new Intent(MainScreen.this, Activity2.class));
}

public void Activity3 (View view){
    startActivity(new Intent(MainScreen.this, Activity3.class));
}










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

非常感谢

好吧所以我使用共享首选项来保存我的活动状态,但是当我尝试使用另一个免费按钮启用一个按钮时它才能正常工作,但它不适用于应用程序内购买按钮(只是让我这样做)购买但停用按钮永远不会在购买后启用)所以我不知道我的代码中的应用内购买和共享偏好之间是否存在冲突?

这是我编辑的代码:

  

包com.aseng90_test.smiap2;

     

import android.content.Intent;进口   android.content.SharedPreferences;进口   android.support.v7.app.ActionBarActivity; import android.os.Bundle;   import android.util.Log; import android.view.View;进口   android.widget.Button; import android.widget.Toast;

     

import com.aseng90_test.smiap2.util.IabHelper;进口   com.aseng90_test.smiap2.util.IabResult;进口   com.aseng90_test.smiap2.util.Purchase;

     

公共类MainScreen扩展了ActionBarActivity {

private static final String TAG = "com.aseng90_test.smiap2";
IabHelper mHelper;
static final String ITEM_SKU = "com.aseng90_test.smiap2_button55";
static final String ITEM_SKU2 = "com.aseng90_test.smiap2_buyact22";
static final String ITEM_SKU3 = "com.aseng90_test.smiap2_buyact33";
static final String ITEM_SKU4 = "com.aseng90_test.smiap2_buyall_1";






private Button Activity1;
private Button Activity2;
private Button Activity3;

private Button buyButton;
private Button buyAct2;
private Button buyAct3;
private Button buyAll;
     

私人按钮EAct4;私人按钮Act4;

private SharedPreferences prefs;
private String prefName = "MyPref";


 boolean Activity1_isEnabled;
 boolean Activity2_isEnabled;
 boolean Activity3_isEnabled;
 boolean Act4_isEnabled;

 boolean buyButton_isEnabled;
 boolean buyAct2_isEnabled;
 boolean buyAct3_isEnabled;
 boolean buyAll_isEnabled;
 boolean EAct4_isEnabled;

private static final String Activity1_state = "Activity1_state";
private static final String Activity2_State = "Activity2_state";
private static final String Activity3_State = "Activity3_state";
private static final String buyButton_State = "buyButton_state";
private static final String buyAct2_State = "buyAct2_state";
private static final String buyAct3_State = "buyAct3_state";
private static final String buyAll_State = "buyAll_state";
private static final String Act4_State = "Act4_state";
private static final String EAct4_State = "EAct4_state";



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

public void  buyAct2 (View view){
    mHelper.launchPurchaseFlow(this, ITEM_SKU2, 10002, mPurchaseFinishedListener, "buyact2");
}

public void buyAct3 (View view){
    mHelper.launchPurchaseFlow(this,ITEM_SKU3, 10003, mPurchaseFinishedListener, "buyact3");
}

public void buyAll (View view){
    mHelper.launchPurchaseFlow(this, ITEM_SKU4,10004, mPurchaseFinishedListener, "buyall");
}


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

    buyButton = (Button) findViewById(R.id.buyButton);
    buyAct2 = (Button) findViewById(R.id.buyact2);
    buyAct3 = (Button) findViewById(R.id.buyact3);
    buyAll = (Button) findViewById(R.id.buyall);


    Activity1 = (Button) findViewById(R.id.act1);
    Activity2 = (Button) findViewById(R.id.act2);
    Activity3 = (Button) findViewById(R.id.act3);




    EAct4 = (Button) findViewById(R.id.eact4);
    Act4 = (Button) findViewById(R.id.act4);





    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 EACT4 (View view) {
    EAct4.setEnabled(false);
    Act4.setEnabled(true);


}

public void ACT4 (View view){
    Toast.makeText(MainScreen.this,
            "ACt4 Clicked", Toast.LENGTH_LONG).show();
}

@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;
        }
         if (purchase.getSku().equals(ITEM_SKU)) {
            Activity1.setEnabled(true);
            buyButton.setEnabled(false);

        }

         if (purchase.getSku().equals(ITEM_SKU2)){
        Activity2.setEnabled(true);
        buyAct2.setEnabled(false);
    }

       if (purchase.getSku().equals(ITEM_SKU3)){
            Activity3.setEnabled(true);
            buyAct3.setEnabled(false);
        }

      if (purchase.getSku().equals(ITEM_SKU4)) {
            Activity1.setEnabled(true);
            Activity2.setEnabled(true);
            Activity3.setEnabled(true);

            buyAll.setEnabled(false);
            buyButton.setEnabled(false);
            buyAct2.setEnabled(false);
            buyAct3.setEnabled(false);
        }
    }


};




public void Activity1 (View view)
{
    startActivity(new Intent(MainScreen.this, Click1.class));

}

public void Activity2 (View view){
    startActivity(new Intent(MainScreen.this, Activity2.class));
}

public void Activity3 (View view){
    startActivity(new Intent(MainScreen.this, Activity3.class));
}


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


@Override
protected void onPause(){
    super.onPause();

    if (Act4.isEnabled()){
        Act4_isEnabled = true;
    }
    else {
        Act4_isEnabled = false;
    }


    if (Activity1.isEnabled()){
        Activity1_isEnabled = true;
    }
    else {
        Activity1_isEnabled = false;
    }

    if (Activity2.isEnabled()){
        Activity2_isEnabled = true;
    }
    else {
        Activity2_isEnabled = false;
    }

    if (Activity3.isEnabled()){
        Activity3_isEnabled = true;
    }
    else {
        Activity3_isEnabled = false;
    }

    if (buyButton.isEnabled()){
        buyButton_isEnabled = true;
    }
    else {
        buyButton_isEnabled = false;
    }

    if (buyAct2.isEnabled()){
        buyAct2_isEnabled = true;
    }
    else {
        buyAct2_isEnabled = false;
    }

    if (buyAct3.isEnabled()){
        buyAct3_isEnabled = true;
    }
    else {
        buyAct3_isEnabled = false;
    }

    if (buyAll.isEnabled()){
        buyAll_isEnabled = true;
    }
    else {
        buyAll_isEnabled = false;
    }



    prefs = getSharedPreferences(prefName,MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean(Act4_State,Act4_isEnabled);
    editor.putBoolean(EAct4_State,EAct4_isEnabled);
    editor.putBoolean(Activity1_state,Activity1_isEnabled);
    editor.putBoolean(Activity2_State,Activity2_isEnabled);
    editor.putBoolean(Activity3_State,Activity3_isEnabled);
    editor.putBoolean(buyButton_State,buyButton_isEnabled);
    editor.putBoolean(buyAct2_State,buyAct2_isEnabled);
    editor.putBoolean(buyAct3_State,buyAct3_isEnabled);
    editor.putBoolean(buyAll_State,buyAll_isEnabled);
    editor.apply();

}

@Override
protected void onResume(){
    super.onResume();

    prefs = getSharedPreferences(prefName,MODE_PRIVATE);

    Act4.setEnabled(prefs.getBoolean(Act4_State,false));
    Activity1.setEnabled(prefs.getBoolean(Activity1_state,false));
    Activity2.setEnabled(prefs.getBoolean(Activity2_State,false));
    Activity3.setEnabled(prefs.getBoolean(Activity3_State,false));

    EAct4.setEnabled(prefs.getBoolean(EAct4_State,true));
    buyButton.setEnabled(prefs.getBoolean(buyButton_State,true));
    buyAct2.setEnabled(prefs.getBoolean(buyAct2_State,true));
    buyAct3.setEnabled(prefs.getBoolean(buyAct3_State,true));
    buyAll.setEnabled(prefs.getBoolean(buyAll_State,true));
}

再次感谢

2 个答案:

答案 0 :(得分:0)

重新启动活动时,它不会保存上一个实例的视图状态。因此,您可能需要在某处保存状态数据,以便在重新启动活动时,可以重置查看此已保存状态数据的视图。您可以使用“SharedPreferences”类。 SharedPreferences允许您将信息存储在键值对中。我想这符合你的目的。

    SharedPreferences pref = getApplicationContext().getSharedPreferences("ProductCache", 0); 
// 0 - for private mode
//First parameter is the name of your preference file.Could be "ProductCache" in your case.
// Default 0 for private access.
SharedPreferences.Editor editor = pref.edit(); // edit your preference file
editor.putString("KEY","VALUE");//save strings
editor.putBoolean("KEY",true);//save booleans or any other type of data.
editor.commit(); //commit your changes

您可以设置您的产品已经在mPurchaseFinishedListener中购买。下次打开您的应用程序时,您可以使用用于存储信息的相同“密钥”来检查相同的文件,以检查产品是否已经购买并且然后你可以相应地设置你的观点。

如果这不符合您的目的,那么您可能必须开始实施一个跟踪所有购买的SQLite数据库。请阅读Android开发者的这篇文章,因为它可能会对您有所帮助:

http://developer.android.com/training/basics/data-storage/index.html

希望这会有所帮助。干杯!

答案 1 :(得分:0)

使用CheckOut library它可以为您完成所有操作,您可以检查购买的商品,并根据sku启用或禁用购买按钮。