使用空间时,广告未加载安卓 [英] using space when ad is not loaded android

查看:258
本文介绍了使用空间时,广告未加载安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中添加AdMob广告,但是当我没有连接互联网比得过空的空间是存在的......

我要添加到那时的广告空间,应该由其他元素被用来加载后出现

该怎么办?

我的下列活动给出文件 -

MainActivity.java
`

 包com.testapp.update1;
进口com.google.android.gms.ads.AdRequest;
进口com.google.android.gms.ads.AdSize;
进口com.google.android.gms.ads.AdView;
进口android.os.Bundle;
进口android.app.Activity;
进口android.app.AlertDialog;
进口android.content.DialogInterface;
进口android.content.Intent;
进口android.content.DialogInterface.OnClickListener;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.GridView;
进口android.widget.LinearLayout;
进口android.widget.Toast;公共类MainActivity延伸活动{
私人的AdView AdView的;
GridView的网格;
的String []图标= {历史的地方,铁路,城市客车,电影,
        目录 };INT [] = imageID {R.drawable.ic_historic,R.drawable.ic_train,
        R.drawable.ic_bus,R.drawable.ic_movie,R.drawable.ic_call};@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    //添加广告
    AD浏览=新的AdView(本);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId(CommanData.AD_UNIT_ID);    // AdView的加入视图层次结构。该视图将没有大小
    //直到在加载广告。
    的LinearLayout布局=(的LinearLayout)findViewById(R.id.LinearLayout1);
    layout.addView(AD浏览);    AdRequest中AdRequest中=新AdRequest.Builder()
                                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                                .addTestDevice(4963BE445DBBB277)
                                。建立();    //开始加载在后台的广告。    CustomGrid适配器=新CustomGrid(MainActivity.this,图标,imageID);
    格=(GridView控件)findViewById(R.id.icongrid);
    grid.setAdapter(适配器);
    grid.setOnItemClickListener(新AdapterView.OnItemClickListener(){        @覆盖
        公共无效onItemClick(适配器视图<>母公司,观景,
                INT位置,长的id){
            // TODO自动生成方法存根
            Toast.makeText(MainActivity.this,
                    你在点击+图标[位置] +位置,
                    Toast.LENGTH_SHORT).show();            意向网络=新的Intent();
            开关(位置){
            情况下0:
                web.setClass(MainActivity.this,Places.class);
                startActivity(网络);
                打破;
            情况1:
                web.setClass(MainActivity.this,Railway.class);
                startActivity(网络);
                打破;
            案例2:
                web.setClass(MainActivity.this,BusOptions.class);
                startActivity(网络);
                打破;
            案例3:
                ConnectionDetector CD =新ConnectionDetector(getApplicationContext());
                如果(cd.isConnected()){
                    web.setClass(MainActivity.this,Movies.class);
                    startActivity(网络);
                }其他{
                    showConnectivityDialog();
                }
                打破;
            情况4:
                意向意图=新意图(MainActivity.this,
                        CallDirctory.class);
                startActivity(意向);
            默认:
                打破;
            }
        }
    });
    //加载添加
    adView.loadAd(AdRequest中);    //要为在谷歌Play应用程序
    // AppRater.showRateDialog(mContext,编辑)
    AppRater.app_launched(MainActivity.this);
}  @覆盖
  公共无效onResume(){
    super.onResume();
    如果(AD浏览!= NULL){
      adView.resume();
    }
  }  @覆盖
  公共无效的onPause(){
    如果(AD浏览!= NULL){
      adView.pause();
    }
    super.onPause();
  }  / **叫活动被销毁。 * /
  @覆盖
  公共无效的onDestroy(){
    //销毁的AdView。
    如果(AD浏览!= NULL){
      adView.destroy();
    }
    super.onDestroy();
  }
公共无效showConnectivityDialog(){
    AlertDialog.Builder notfoundDialog =新AlertDialog.Builder(MainActivity.this);
    notfoundDialog.setTitle(未连接)
            .setMessage(看来你没有连接互联网)
            .setPositiveButton(OK,新OnClickListener(){                @覆盖
                公共无效的onClick(DialogInterface对话,诠释它){                }
            })
            .setIcon(R.drawable.ic_launcher)
            。显示();
}
}


解决方案

请的知名度

 的LinearLayout布局=(的LinearLayout)findViewById(R.id.LinearLayout1);

即。布局其中添加你的的AdView View.GONE 最初这样就OCCUP没有空格。

现在的 AdListener的添加到的AdView ,使上述布局查看。可见中的 AdListener的 onAdLoaded 方法。这样,直到广告加载布局将永远是可见的。

有关如。

  AdListener的googleAdListener =新AdListener的()
{    @覆盖
    公共无效onAdLoaded()
    {
        super.onAdLoaded();
        尝试
        {
            (LinearLayout中)findViewById(R.id.LinearLayout1).setVisibility(View.VISIBLE);
        }
        赶上(例外五)
        {
            Logger.LogException(E);
        }
    }
};的AdView.setAdListener(googleAdListener);

注意:您必须使用最新的的Google Play服务-LIB 不是 AdMobsSDK

I am trying to add admob ads in my application but when I am not connected to internet than too empty space is there...

I want add to appear after loading until then ad space should be utilized by the rest elements

what to do???

My Activity file is given below -

MainActivity.java `

package com.testapp.update1;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {
private AdView adView;
GridView grid;
String[] icons = { "Historic Places", "Railway", "City Bus", "Movie",
        "Directory" };

int[] imageID = { R.drawable.ic_historic, R.drawable.ic_train,
        R.drawable.ic_bus, R.drawable.ic_movie, R.drawable.ic_call };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //adding advertisement 
    adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId(CommanData.AD_UNIT_ID);

    // Add the AdView to the view hierarchy. The view will have no size
    // until the ad is loaded.
    LinearLayout layout = (LinearLayout) findViewById(R.id.LinearLayout1);
    layout.addView(adView);

    AdRequest adRequest = new AdRequest.Builder()
                                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                                .addTestDevice("4963BE445DBBB277")
                                .build();

    // Start loading the ad in the background.

    CustomGrid adapter = new CustomGrid(MainActivity.this, icons, imageID);
    grid = (GridView) findViewById(R.id.icongrid);
    grid.setAdapter(adapter);
    grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            Toast.makeText(MainActivity.this,
                    "You clicked at " + icons[position] + position,
                    Toast.LENGTH_SHORT).show();

            Intent web = new Intent();
            switch (position) {
            case 0:
                web.setClass(MainActivity.this, Places.class);
                startActivity(web);
                break;
            case 1:
                web.setClass(MainActivity.this, Railway.class);
                startActivity(web);
                break;
            case 2:
                web.setClass(MainActivity.this, BusOptions.class);
                startActivity(web);
                break;
            case 3:
                ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
                if(cd.isConnected()) {
                    web.setClass(MainActivity.this, Movies.class);
                    startActivity(web);
                } else {
                    showConnectivityDialog();
                }
                break;
            case 4:
                Intent intent = new Intent(MainActivity.this,
                        CallDirctory.class);
                startActivity(intent);
            default:
                break;
            }
        }
    });
    // to load add
    adView.loadAd(adRequest);

    // To rate application at google play
    // AppRater.showRateDialog(mContext, editor)
    AppRater.app_launched(MainActivity.this);
}

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

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

  /** Called before the activity is destroyed. */
  @Override
  public void onDestroy() {
    // Destroy the AdView.
    if (adView != null) {
      adView.destroy();
    }
    super.onDestroy();
  }


public void showConnectivityDialog() {
    AlertDialog.Builder notfoundDialog = new AlertDialog.Builder(MainActivity.this);
    notfoundDialog.setTitle("Not Connected")
            .setMessage("It seems you are not connected to internet")
            .setPositiveButton("Ok", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            })
            .setIcon(R.drawable.ic_launcher)
            .show();
}
}

解决方案

Keep the visibility of

LinearLayout layout = (LinearLayout) findViewById(R.id.LinearLayout1);

i.e. the layout to which you are adding your Adviewto View.GONE initially so it will occup no space.

Now add an AdListener to the AdView and make the above mentioned layout View.VISIBLE in the onAdLoadedmethod of the AdListener. That way the layout will never be visible until the Ad is loaded.

For eg.

AdListener googleAdListener = new AdListener()
{

    @Override
    public void onAdLoaded()
    {
        super.onAdLoaded();
        try
        {
            (LinearLayout) findViewById(R.id.LinearLayout1).setVisibility(View.VISIBLE);
        }
        catch (Exception e)
        {
            Logger.LogException(e);
        }
    }
};

adView.setAdListener(googleAdListener);

NOTE: You will have to use the latest google-play-services-lib not AdMobsSDK

这篇关于使用空间时,广告未加载安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆