首页 文章

如何在Android上添加admobs

提问于
浏览
0

请帮助一些东西,我试图在应用程序上添加admobs但它在我运行它时不会显示在应用程序上,但它出现在android工作室预览中 . 我该如何解决这个问题?

此代码位于主活动布局上

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/relatively"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#d15400"
    android:orientation="vertical"
    android:weightSum="1" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.90"
        android:orientation="vertical"
        android:weightSum="1" >

        <LinearLayout
            android:id="@+id/linearLayout11"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.20"
            android:orientation="vertical"
            android:weightSum="1" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/score"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_gravity="center"
                    android:layout_marginRight="10dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="Score : 1"
                    android:textColor="#ffffff"
                    android:textSize="25.0sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/timers"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_gravity="center"
                    android:layout_marginRight="10dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="00:00:49"
                    android:textColor="#ffffff"
                    android:textSize="25.0sp"
                    android:textStyle="bold" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout12"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.60"
            android:orientation="vertical"
            android:weightSum="1" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.40" >

                <TextView
                    android:id="@+id/txtQuestion"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_gravity="center"
                    android:layout_marginTop="5dp"
                    android:gravity="center"
                    android:text="15*2*1-1"
                    android:textColor="#ffffff"
                    android:textSize="35.0sp"
                    android:textStyle="bold" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.20"
                android:orientation="vertical"
                android:weightSum="1" >

                <Button
                    android:id="@+id/button1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="80dp"
                    android:layout_marginRight="80dp"
                    android:layout_marginTop="5dp"
                    android:background="#fff"
                    android:gravity="center"
                    android:text="30"
                    android:textColor="#000000"
                    android:textSize="25.0sp" />

                <Button
                    android:id="@+id/button2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="80dp"
                    android:layout_marginRight="80dp"
                    android:layout_marginTop="5dp"
                    android:background="#fff"
                    android:gravity="center"
                    android:text="29"
                    android:textColor="#000000"
                    android:textSize="25.0sp" />

                <Button
                    android:id="@+id/button3"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="80dp"
                    android:layout_marginRight="80dp"
                    android:layout_marginTop="5dp"
                    android:background="#fff"
                    android:gravity="center"
                    android:text="32"
                    android:textColor="#000000"
                    android:textSize="25.0sp" />





            </LinearLayout>


        </LinearLayout>



</LinearLayout>
    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>


</LinearLayout>

此代码在问题活动类上 -

setContentView(R.layout.activity_main);
        AdView adView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder()
                .setRequestAgent("android_studio:ad_template").build();
        adView.loadAd(adRequest);

这是清单上的cose

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="hitesh.asimplegame">

    <!-- Include required permissions for Google Mobile Ads to run. -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".QuestionActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity><![CDATA[
       android:name=".ResultActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait" />
        ]]>
        <activity android:name=".Finish" />
        <activity android:name=".SplashScrean" />
        <!-- This meta-data tag is required to use Google Play Services. -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <!-- Include the AdActivity configChanges and theme. -->
        <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" />
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" />

    </application>

</manifest>

这是资源

resources>
    <string name="app_name">A simple game</string>

    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="title_activity_admobs">admobs</string>
    <!-- -
        This is an ad unit ID for a banner test ad. Replace with your own banner ad unit id.
        For more information, see https://support.google.com/admob/answer/3052638
    <!- -->
    <string name="banner_ad_unit_id">4526809957646802/687614978</string>
    <string name="title_activity_main">MainActivity</string>6

    <string name="interstitial_ad_sample">Interstitial Ad Sample</string>
    <string name="start_level">Level 1</string>
    <string name="next_level">Next Level</string>
    <!-- -
        This is an ad unit ID for an interstitial test ad. Replace with your own interstitial ad unit id.
        For more information, see https://support.google.com/admob/answer/3052638
    <!- -->
    <string name="interstitial_ad_unit_id">4526809957646802/6876146978</string>
    <string name="title_activity_doa">doa</string>
</resources>

3 回答

  • 0

    您的广告ID应以ca-app-pub-your_key开头,其中包含ID, Banner ID以及插页式ID

    所以使用这些:

    <string name="banner_ad_unit_id">ca-app-pub-4526809957646802/687614978</string>
    <string name="interstitial_ad_unit_id">ca-app-pub-4526809957646802/6876146978</string>
    
  • 0

    代码没有问题 . 问题是,您可能会在几分钟前创建该广告单元ID . 因此,它需要更多时间才能出现在真实设备中 . 如果您真的想知道代码是否正常,请将 Headers ID设置为 ca-app-pub-3940256099942544/6300978111 . 这是一个预览ID,它将适合您 .

  • 1
    Hope this will help you:
            <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/relatively"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#d15400"
    android:orientation="vertical"
    android:weightSum="1" >
    
    <LinearLayout
        android:id="@+id/Linear_ad"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
         />
       <LinearLayout/>
    
    
    
     AdView adview;
    LinearLayout linearads;
    private AdBannerListener adBannerListener;
    oncreate:
     setAdvertise();
    
     private void setAdvertise()
    {
        try{
    
            try{
    
    
    
                layout=(LinearLayout)findViewById(R.id.Linear_ad);
    
    
                adview= new AdView(CAboutActivity.this);
                adview.setAdSize(AdSize.BANNER);
                adview.setAdUnitId("adUnit id");
                adview.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
                layout.addView(adview);
                adview.loadAd(new AdRequest.Builder().build());
                //linear_ad
            }catch (Exception e) {
                // TODO: handle exception
            }
            adview.setAdListener(new AdListener() {
                @Override
                public void onAdFailedToLoad(int errorCode) {
                    // TODO Auto-generated method stub
                    try {
                        Log.d("On Fail called", "Ad");
                        layout.setVisibility(View.GONE);
                        super.onAdFailedToLoad(errorCode);
                    }catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
                @Override
                public void onAdLoaded() {
                    // TODO Auto-generated method stub
                    Log.d("On Load called","Ad");
                    layout.setVisibility(View.VISIBLE);
                    super.onAdLoaded();
                }
            });
        }catch (Exception e) {
            // TODO: handle exception
        }
    }
    

相关问题