重新启动后应用程序重置

Posted

技术标签:

【中文标题】重新启动后应用程序重置【英文标题】:app resets after restarting 【发布时间】:2015-03-02 04:56:07 【问题描述】:

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

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

(我需要按时购买)

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

这是我的代码:

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;

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

导入 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));

再次感谢

【问题讨论】:

为什么不将这些值存储在数据库中? 怎么做? @VVB 存储跟踪购买的值。 【参考方案1】:

当您的 Activity 重新启动时,它不会保存之前实例的视图状态。因此,您可能需要将状态数据保存在某处,以便在重新启动活动时,然后可以重置查看此保存状态数据的视图。您可以使用“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

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

【讨论】:

谢谢,我会试试的【参考方案2】:

使用CheckOut library它会为您做所有事情,您可以检查购买的物品并根据sku您可以启用或禁用购买按钮。

【讨论】:

以上是关于重新启动后应用程序重置的主要内容,如果未能解决你的问题,请参考以下文章

重新启动后的警报管理器在意图时显示错误 NullPointerException

当我们使用 appium 重新启动应用程序时,无法在 ios 11.2 中限制应用程序重置数据

重置游戏循环/重新启动游戏 java swing

如何在不重新加载页面的情况下重新启动 Angular 应用程序?

在单击按钮时重置/重新初始化/重新启动 UIViewController 视图的最简单(正确)方法是啥?

每次重新启动时都会重置 MAMP 配置设置