同一广告ID的多种奖励

时间:2019-05-19 15:34:22

标签: android admob admob-rewardedvideoad

我们目前正在构建一个包含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()吗?

1 个答案:

答案 0 :(得分:0)

应用此:

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