java - Admob 横幅广告未显示在 Android 应用程序上

标签 java android ads

我有一个 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();
          }

    }

list 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>

最佳答案

在您的主要 Activity 和 list 中将 com.google.android.ads 更改为 com.google.android.gms.ads,因为您使用的是旧广告,也使用 .gms 将您的 xml 更改为 .gms adview。

关于java - Admob 横幅广告未显示在 Android 应用程序上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23931520/

相关文章:

Flutter firebase_admob 横幅仍然显示在每个屏幕上

java - 向 ProcessBuilder 添加参数 - Java

java - 使用 Java 打乱单词

android - pendingintent.getservice 无法正常工作

google-analytics - 网站每月访问量是(每日访问量 x 30)?

android - libGDX 的 Looper 错误

java - 如何在 Android 中创建像 facebook Messenger 一样的 float 按钮或窗口

java - 在数据库中搜索

android - 有人可以非常详细但易于理解地向我解释 SharedPreferences 在 Android 中的工作原理吗?

java - 动态添加和删除映射按钮到 TableLayout