Admob Banner Ad未在Android App上显示

时间:2014-05-29 10:45:03

标签: java android advertisement

我有一个Android应用程序。我需要整合admob。我已经包含以下代码,但横幅广告没有显示在主要布局中,请帮助您,我们将非常感谢您的回答,这将是一个很好的帮助。谢谢。

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_layout"
    android:background="@drawable/hanumanji1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   tools:context=".HanumanActivity" >

    <Button
        android:id="@+id/pause_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/circle_shape_drawable"
        android:text="@string/pau"
        android:textColor="#ffff00" />

    <Button
        android:id="@+id/play_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/circle_shape_drawable"
        android:gravity="center"
        android:text="@string/pl"
        android:textColor="#ffff00" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:background="@drawable/circle_shape_drawable"
    android:gravity="center"
    android:text="@string/de"
    android:textColor="#ffff00" />

<com.google.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="xxxxxxxxxxxxxxxxxxxxxxxxxxxx" 
        ads:loadAdOnCreate="true"/>


</RelativeLayout>

MainActivity.java

 package e.hanuman;
 import e.hanuman.Constants;
    import java.util.ArrayList;
    import java.util.Random;
    import java.util.Timer;
    import java.util.TimerTask;
    import com.google.ads.AdRequest;
    import com.google.ads.AdSize;
    import com.google.ads.AdView;

    import e.hanuman.R;
    import android.graphics.Rect;
    import android.graphics.drawable.Drawable;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.animation.ValueAnimator;
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.telephony.PhoneStateListener;
    import android.telephony.TelephonyManager;
    import android.util.DisplayMetrics;
    import android.util.Log;
    import android.view.Display;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.animation.AccelerateInterpolator;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.RelativeLayout;
    import android.widget.RelativeLayout.LayoutParams;

    public class HanumanActivity extends Activity implements OnClickListener{
        Button play_button, pause_button, button1 ;
        private AdView adView;

        MediaPlayer player;
        private int[] LEAVES = { 
                R.drawable.leaf_green, 
                R.drawable.leaf_red,
                R.drawable.leaf_yellow,
                R.drawable.leaf_other,
            };
    private Rect mDisplaySize = new Rect();


        private RelativeLayout mRootLayout;
        private ArrayList<View> mAllImageViews = new ArrayList<View>();
        private float mScale;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_hanuman);

            // Create the adView
            adView = new AdView(this, AdSize.BANNER, "xxxxxxxxxxxxxxxxxxxxxxxxxxxx");

            // Lookup your LinearLayout assuming it's been given
            // the attribute android:id="@+id/mainLayout"
           RelativeLayout layout = (RelativeLayout)findViewById(R.id.main_layout);

            // Add the adView to it
            layout.addView(adView);

            // Initiate a generic request to load it with an ad
            adView.loadAd(new AdRequest());
            AdView adView = (AdView)this.findViewById(R.id.adView); 
              AdRequest adRequest = new AdRequest();
              //adRequest.setTesting(true);              







            button1=(Button)findViewById(R.id.button1);
            button1.setOnClickListener(this);
            TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
            if(mgr != null) {
                mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
            }

            getInit();
            Display display = getWindowManager().getDefaultDisplay(); 
            display.getRectSize(mDisplaySize);

            DisplayMetrics metrics = new DisplayMetrics();
            display.getMetrics(metrics);
            mScale = metrics.density;

            mRootLayout = (RelativeLayout) findViewById(R.id.main_layout);

            new Timer().schedule(new ExeTimerTask(), 0, 5000);
        }
        PhoneStateListener phoneStateListener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                if (state == TelephonyManager.CALL_STATE_RINGING) {
                    player.pause();
                } else if(state == TelephonyManager.CALL_STATE_IDLE) {
                    //Not in call: Play music
                    player.start();

                } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
                    //A call is dialing, active or on hold
                    player.pause();
                }
                super.onCallStateChanged(state, incomingNumber);
            }
          };

        public void startAnimation(final ImageView aniView) {

            aniView.setPivotX(aniView.getWidth()/2);
            aniView.setPivotY(aniView.getHeight()/2);

            long delay = new Random().nextInt(Constants.MAX_DELAY);

            final ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
            animator.setDuration(Constants.ANIM_DURATION);
            animator.setInterpolator(new AccelerateInterpolator());
            animator.setStartDelay(delay);

            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                int angle = 50 + (int)(Math.random() * 101);
                int movex = new Random().nextInt(mDisplaySize.right);

                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    float value = ((Float) (animation.getAnimatedValue())).floatValue();

                    aniView.setRotation(angle*value);
                    aniView.setTranslationX((movex-40)*value);
                    aniView.setTranslationY((mDisplaySize.bottom + (150*mScale))*value);
                }
            });

            animator.start();
        }

        private Handler mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                int viewId = new Random().nextInt(LEAVES.length);
                Drawable d = getResources().getDrawable(LEAVES[viewId]);
                LayoutInflater inflate = LayoutInflater.from(HanumanActivity.this);
                ImageView imageView = (ImageView) inflate.inflate(R.layout.ani_image_view, null);
                imageView.setImageDrawable(d);
                mRootLayout.addView(imageView);

                mAllImageViews.add(imageView);          

                LayoutParams animationLayout = (LayoutParams) imageView.getLayoutParams();
                animationLayout.setMargins(0, (int)(-150*mScale), 0, 0);
                animationLayout.width = (int) (60*mScale);
                animationLayout.height = (int) (60*mScale);

                startAnimation(imageView);
            }
        };

        private class ExeTimerTask extends TimerTask {
            @Override
            public void run() {
                // we don't really use the message 'what' but we have to specify something.
                mHandler.sendEmptyMessage(Constants.EMPTY_MESSAGE_WHAT);
            }
        }
        public void getInit() {

            play_button = (Button) findViewById(R.id.play_button);
            pause_button = (Button) findViewById(R.id.pause_button);

            play_button.setOnClickListener(this);
            pause_button.setOnClickListener(this);
            player = MediaPlayer.create(this, R.raw.hanu);



        }





        @Override
        public void onClick(View view) {
            switch (view.getId()) {
            case R.id.play_button:

                player.start();
                break;
            case R.id.pause_button:
                player.pause();
                break;
            case R.id.button1:
                button1Click();
                break;


            }}

        private void button1Click() {
            // TODO Auto-generated method stub
            startActivity(new Intent("e.hanuman.details"));
        }

        @Override
        public void onBackPressed(){
            if(player != null && player.isPlaying())
                player.stop();
            finish();
        }
          @Override
          public void onDestroy() {
            if (adView != null) {
              adView.destroy();
            }
            super.onDestroy();
          }

    }

MANIFEST XML

    <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="e.hanuman"
    android:versionCode="4"
    android:versionName="4.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
   <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/hanuman"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >



            <activity android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 
            android:name="e.hanuman.Splash" android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


            <activity
            android:name="e.hanuman.HanumanActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:screenOrientation="portrait" > 
            </activity>
        <activity
            android:name="details"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="e.hanuman.details" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name="com.google.ads.AdActivity"
              android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

    </application>

</manifest>

1 个答案:

答案 0 :(得分:1)

com.google.android.ads更改为com.google.android.gms.ads在您的主要活动和您的清单中,因为您正在使用旧广告,请使用.gms也将您的xml更改为.gms广告视图。