How do I put an admob adview in the settings screen for a live wallpaper?

后端 未结 6 1837
孤街浪徒
孤街浪徒 2020-12-12 23:21

I\'ve seen the Mario live wallpaper uses admob ads in the settings screen, but I haven\'t been able to do it myself. If I put the adview into the settings.xml like you would

6条回答
  •  醉话见心
    2020-12-12 23:57

    For those wondering how to implement this with a PreferenceFragment, here is a solution, there is no need to create a custom Preference:

    First create a new layout (here named settings.xml in the layout resources folder) that will be inflated in the PreferenceFragment:

    
    
    
        
    
        
    
    
    

    Then, use this layout in the PreferenceFragment, load it in the onCreateView method:

    private AdView adView;
    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.settings, null);
    
            adView = (AdView)v.findViewById(R.id.adview);
    
            //** Adview */
            if(Tools.isNetworkConnected(getActivity())){
                adView.setVisibility(View.VISIBLE);
            }
            else{
                adView.setVisibility(View.GONE);
            }
    
            //Create ad banner request
            AdRequest adBannerRequest = new AdRequest.Builder()
    //      .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    //      .addTestDevice("XXXXX")
            .build();
    
            // Begin loading banner
            adView.loadAd(adBannerRequest);
    
            return v;
        }
    

    Don't forget to add the following code for the new version of Admob using Google Play services:

    @Override
        public void onResume() {
            super.onResume();
            if(adView != null){
                adView.resume();
            }
        }
    
        @Override
        public void onPause() {
            if(adView != null){
                adView.pause();
            }
            super.onPause();
        }
    
        @Override
        public void onDestroy() {
            if(adView != null){
                adView.destroy();
            }
            super.onDestroy();  
        }
    

提交回复
热议问题