首页 > 解决方案 > 同一广告 ID 的多种奖励

问题描述

我们目前正在构建一个包含 AdMob 奖励视频系统的应用程序。应用程序有一个 MainActivity,并且在其上构建了片段。每个片段都有一个按钮,每个片段都实现“RewardedVideoAdListener”。单击按钮时,将加载并显示奖励视频。现在的问题是,每个按钮都应该有不同类型的奖励。

两个片段看起来像这样;

    public class FragmentShop extends Fragment implements RewardedVideoAdListener
    {
    private RewardedVideoAd rewardedVideoAd;
    private Button rewardButton;

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    rewardButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(rewardedVideoAd.isLoaded()){
                    rewardedVideoAd.show();
                    rewardButton.setVisibility(View.INVISIBLE);
                }
                else {
                    Toast.makeText(view.getContext(),"Try again!",Toast.LENGTH_SHORT).show();
                    loadRewardedVideoAd();
                }
            }
        });
    }
private void loadRewardedVideoAd() {
        rewardedVideoAd.loadAd("---", //there is our key
                new AdRequest.Builder().build());
    }

 @Override
    public void onRewarded(RewardItem rewardItem) {
        //Some database actions.
        loadRewardedVideoAd();
    }
}

问题是,无论我们点击哪个按钮,只有 onRewarded() 函数在片段 2 中运行。所以我们如何构建一个结构,每个片段都运行自己的 onRewarded() 函数。我们应该首先使用 onRewardedVideoCompleted() 吗?

标签: androidadmobadmob-rewardedvideoad

解决方案


应用这个:

private RewardedVideoAd mRewardedVideoAd;

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_main, container, false);
        mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(getContext());
        mRewardedVideoAd.setRewardedVideoAdListener(rewardedVideoAdListener);

        rewardButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // todo
                loadRewardedVideoAd();
            }
        });
        return rootView;
    }

    RewardedVideoAdListener rewardedVideoAdListener = new RewardedVideoAdListener() {
        @Override
        public void onRewardedVideoAdLoaded() {
            Toast.makeText(getActivity(), "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onRewardedVideoAdOpened() {
            Toast.makeText(getActivity(), "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onRewardedVideoStarted() {
            Toast.makeText(getActivity(), "onRewardedVideoStarted", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onRewardedVideoAdClosed() {
            Toast.makeText(getActivity(), "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onRewarded(RewardItem reward) {
            Toast.makeText(getActivity(), getString(R.string.on_rewarded_video) + " " +  reward.getAmount() + " " + reward.getType(), Toast.LENGTH_LONG).show();
            // Reward the user.
        }

        @Override
        public void onRewardedVideoAdLeftApplication() {
            Toast.makeText(getActivity(), "onRewardedVideoAdLeftApplication", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onRewardedVideoAdFailedToLoad(int i) {
            Toast.makeText(getActivity(), "onRewardedVideoAdFailedToLoad", Toast.LENGTH_SHORT).show();
        }
    };

    private void loadRewardedVideoAd() {
        mRewardedVideoAd.loadAd("ca-app-pub-#################/##########",
                new AdRequest.Builder().build());
    }

推荐阅读