Android应用内结算:制作应用内广告时不会删除广告

时间:2016-09-11 09:56:45

标签: java android in-app-billing ads

我已在我的活动

中实施了应用内结算

这是我的onIabPurchaseFinished()方法:

@Override
public void onIabPurchaseFinished(IabResult result, Purchase info) {

    if (!verifyDeveloperPayload(info)) {
        Toast.makeText(this, R.string.error_purchasing, Toast.LENGTH_LONG).show();
    }

    Toast.makeText(this, R.string.premium_bought, Toast.LENGTH_LONG).show();

    if (info.getSku().equals("chords_premium")) {

        /** salva isPremium tra SharedPreferences */
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString("status", "purchased");
        editor.apply();
    }
}

正如您所看到的,我将字符串"status"保存到SharedPreferences,以便我可以从任何地方访问它,并在应用关闭后保存它。

然后在我实施广告的其他活动中,我写道:

final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    final String status = prefs.getString("status", "free");


    /** gestisce le pubblicita */
    if (status.equals("free")) {
        MobileAds.initialize(getApplicationContext(), "ca-app-pub-6723047396589178/2654753246");

        AdView listBanner = (AdView) findViewById(R.id.chords_list_banner);
        AdRequest adRequest = new AdRequest.Builder().build();
        listBanner.loadAd(adRequest);

        /** carica Ad a tutto schermo */
        chordsListAd = new InterstitialAd(this);
        chordsListAd.setAdUnitId("ca-app-pub-6723047396589178/7447672046");
        requestNewInterstitial();


        chordsListAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                requestNewInterstitial();
            }
        });
    }

正如您在此处所见,广告被if statement包围,检查"status"字符串是否设置为空闲。

问题是,当我购买Premium时,广告仍会显示。我该如何解决?

2 个答案:

答案 0 :(得分:1)

检查是否进行了inapp购买:

//*************************************checking in app purchase has been made********************************// 
    void testInApp()
    {
        if (!blnBind) return;
        if (mService == null) return;

        int result;
        try {
            result = mService.isBillingSupported(3, getPackageName(), "inapp");

            //Toast.makeText(context, "isBillingSupported() - success : return " + String.valueOf(result), Toast.LENGTH_SHORT).show();
            Log.i(tag, "isBillingSupported() - success : return " + String.valueOf(result));
        } catch (RemoteException e) {
            e.printStackTrace();

            //Toast.makeText(context, "isBillingSupported() - fail!", Toast.LENGTH_SHORT).show();
            Log.w(tag, "isBillingSupported() - fail!");
            return;
        } 
    }

    void checkInApp()
    {

        if (!blnBind) return;
        if (mService == null) return;

        Bundle ownedItems;
        try {
            ownedItems = mService.getPurchases(3, getPackageName(), "inapp", null);

            //Toast.makeText(context, "getPurchases() - success return Bundle", Toast.LENGTH_SHORT).show();
            Log.i(tag, "getPurchases() - success return Bundle");
        } catch (RemoteException e) {
            e.printStackTrace();

            //Toast.makeText(context, "getPurchases - fail!", Toast.LENGTH_SHORT).show();
            Log.w(tag, "getPurchases() - fail!");
            return;
        }

        int response = ownedItems.getInt("RESPONSE_CODE");
        //Toast.makeText(context, "getPurchases() - \"RESPONSE_CODE\" return " + String.valueOf(response), Toast.LENGTH_SHORT).show();
        Log.i(tag, "getPurchases() - \"RESPONSE_CODE\" return " + String.valueOf(response));

        if (response != 0) return;

        ArrayList<String> ownedSkus = ownedItems.getStringArrayList("INAPP_PURCHASE_ITEM_LIST");
        ArrayList<String> purchaseDataList = ownedItems.getStringArrayList("INAPP_PURCHASE_DATA_LIST");
        ArrayList<String> signatureList = ownedItems.getStringArrayList("INAPP_DATA_SIGNATURE");
        String continuationToken = ownedItems.getString("INAPP_CONTINUATION_TOKEN");

        Log.i(tag, "getPurchases() - \"INAPP_PURCHASE_ITEM_LIST\" return " + ownedSkus.toString());
        Log.i(tag, "getPurchases() - \"INAPP_PURCHASE_DATA_LIST\" return " + purchaseDataList.toString());
        Log.i(tag, "getPurchases() - \"INAPP_DATA_SIGNATURE\" return " + (signatureList != null ? signatureList.toString() : "null"));
        Log.i(tag, "getPurchases() - \"INAPP_CONTINUATION_TOKEN\" return " + (continuationToken != null ? continuationToken : "null"));

        // TODO: management owned purchase  


        try {


            if(purchaseDataList.size()>0){
                jinapp=new JSONArray(purchaseDataList.toString());
                JSONObject c =  jinapp.getJSONObject(0);

                String productid=c.getString("productId");

                if(productid!=null){
                    SharedPreferences.Editor editor = prefpurchase.edit();
                    editor.putBoolean(Constants.APP_IS_PURCHASED,true);
                    editor.commit();
                }
            }   
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // TODO: management owned purchase  

    }

在SplashScreen中编写代码

现在,在您展示广告的活动/片段中,请输入以下代码:

//*******************to check purchase has been made.If yes disable ads and no then show ads******************//
        prefpurchase = this.getSharedPreferences(Constants.GET_IN_APP_STATE, Context.MODE_PRIVATE);

        //Toast.makeText(context, "bindService - return " + String.valueOf(blnBind), Toast.LENGTH_SHORT).show();

        //In App Purchase

        ispurchased=prefpurchase.getBoolean(Constants.APP_IS_PURCHASED,false);
        System.out.println("ispurchased-->"+ispurchased);
        if(ispurchased)
        {
            setContentView(R.layout.activity_home_noads);

        }else{
            System.out.println("Getting ad");
            setContentView(R.layout.activity_home);
            //Locate the Banner Ad in activity_main.xml
            AdView adView = (AdView) this.findViewById(R.id.adView);
            AdRequest adRequest = new AdRequest.Builder()
            // Add a test device to show Test Ads
            //.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            //.addTestDevice("B2D63***************************")
            .build();


            // Load ads into Banner Ads
            adView.loadAd(adRequest);
        }
    //*******************************************************************************************************//

逻辑很简单,您正在创建两个版本的布局,一个是广告而另一个没有广告。

根据共享偏好的值加载正确的布局。

<强> MSERVICE:

在onCreate()之前在splashscreen中全局编写此代码:

private IInAppBillingService mService;
    private ServiceConnection mServiceConn = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = IInAppBillingService.Stub.asInterface(service);
        }
    };

<强> blnBind

全局声明blnBind:

boolean blnBind;

在SplashActivity的onCreate()中写道:

// Bind Service
        blnBind = bindService(new Intent(
                "com.android.vending.billing.InAppBillingService.BIND"),
                mServiceConn, Context.BIND_AUTO_CREATE);

        //Toast.makeText(context, "bindService - return " + String.valueOf(blnBind), Toast.LENGTH_SHORT).show();
        Log.i(tag, "bindService - return " + String.valueOf(blnBind)); 
        //In App Purchase

为共享首选项创建GET_IN_APP_STATE或APP_IS_PURCHASED,它们充当首选项值的键。

//Preferences to check in app purchase
    final static public String GET_IN_APP_STATE = "prefinapp";
    public static final String APP_IS_PURCHASED ="AppIsPurchased";

每次购买时,请不要忘记将共享偏好设置为true。

答案 1 :(得分:1)

这是因为您要将数据保存在基本上下文中,并尝试使用(此)在当前的活动上下文中找到它。

final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

此外,查询应用内购买商品的更多推荐方法是查询应用内商品库存,而不是存储在sharedprefs中。

如Google文档中所述

查询已购买的商品

成功购买后,Google Play的应用内结算服务会在本地缓存用户的购买数据。最好经常查询应用内结算服务以查看用户的购买情况,例如应用启动或恢复时,以便用户当前的应用内商品所有权信息始终反映在您的应用中。

要从您的应用中检索用户的购买,请在IabHelper实例上调用queryInventoryAsync(QueryInventoryFinishedListener)。 QueryInventoryFinishedListener参数指定在查询操作完成时通知并处理查询响应的侦听器。将此调用作为主线程是安全的。

mHelper.queryInventoryAsync(mGotInventoryListener); //mHelper is IabHelper instance

如果查询成功,查询结果将存储在传递回侦听器的Inventory对象中。应用内结算服务仅返回当前登录到设备的用户帐户所做的购买。

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

      if (result.isFailure()) {
        // handle error here
      }
      else {
        // does the user have the premium upgrade?
        mIsPremium = inventory.hasPurchase(SKU_PREMIUM);
        // update UI accordingly
      }
   }
};