从辅助类中对MainActivity进行UI更改

时间:2017-08-24 06:26:21

标签: java android in-app-purchase listener

我正在尝试在我的Android应用中加入应用结算。我目前仍然坚持如何回调我的MainActivity,一旦找到用户之前购买的东西,就从帮助计费类中执行UI更改。

我搜索过但搜索过但我找不到我要找的东西,我确定我需要实现回调或听众,或两者兼而有之?

我的代码如下:

MainActivity

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener  {


MyBilling bill;
private Menu nav_Menu;

@Override
protected void onCreate(Bundle savedInstanceState) {

bill = new MyBilling(this);
bill.onCreate();

}

private void displaySelectedScreen(int itemId) {

///calls fragment and inflates

}


public void UpdateUI(){

//Make some changes to UI
nav_Menu.findItem(R.id.remove_ad_button).setVisible(false);

//Recall fragment
displaySelectedScreen(R.id.distance_check);

}


}

结算助手类(Google In App Billing)

public class MyBilling {

Activity activity;
public MyBilling(Activity launcher) {
    this.activity = launcher;
}


IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
  public void onQueryInventoryFinished(IabResult result,
                                       Inventory inventory) {
      Log.d(TAG, "Query inventory finished.");

      // Have we been disposed of in the meantime? If so, quit.
      if (mHelper == null)
          return;

      // Is it a failure?
      if (result.isFailure()) {
          // complain("Failed to query inventory: " + result);
          return;
      }

      Log.d(TAG, "Query inventory was successful.");

      /*
       * Check for items we own. Notice that for each purchase, we check
       * the developer payload to see if it's correct! See
       * verifyDeveloperPayload().
       */

      // Do we have the premium upgrade?
      Purchase removeAdsPurchase = inventory.getPurchase(SKU_REMOVE_ADS);
      AdCheck = (removeAdsPurchase != null && verifyDeveloperPayload(removeAdsPurchase));


        //Yes there has been purchases! 
      if(AdCheck == true){
          removeAds(); // sets global flag

          //// Want to call UI change here....
          MainActivity main = new MainActivity();
          main.UpdateUI();

      }

  }
 };

 }

我从调试中理解的是IabHelper.QueryInventoryFinishedListener在计费类的oncreate期间被调用,它是异步的,所以我的主要活动继续编译。完成购买设置和阅读后,我需要返回到我的MainActivity并进行更改。

如果有人能指出我在正确的方向请 - 我已经尝试通过提供所需的片段来保持代码直接。

1 个答案:

答案 0 :(得分:0)

使用Interface。在UpdateUI()中添加Interface方法声明。然后在Interface类中创建MyBilling的实例并使用构造函数初始化。在UPdateUI()的实例中调用Interface方法。然后在Interface中实施MainActivity。如果您不清楚实施,我很乐意发布代码。

代码:

MyBilling课程中,在课程中添加以下内容

public interface Update {
        void UpdateUI();
    }

使用此方法从UpdateUI()类调用MyBilling方法。

myActivity.UpdateUI();

将此作为MyBilling class

中的构造函数
Update myActivity;
public MyBilling (Update activity) {
    myActivity = activity;
}

现在更改为MainActivity

中的以下内容
public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener, MyBilling.Update

如果在其下方显示红线,请按Alt +输入它以在Update中实施MainActivity界面。我猜是因为你UpdateUI()已经MainActivity,所以不会出现红线错误。希望它有所帮助!