当前位置: 首页 > 知识库问答 >
问题:

为不同的活动实现admob

司马洲
2023-03-14

我总共有三个活动,我正在为每个活动实现admob,每个活动都有自己的横幅,当活动被改变时,另一个活动挂了一点,因为广告加载在后台,是否有任何方法,一个横幅出现在所有活动时,切换,以避免延迟。

共有1个答案

冯元徽
2023-03-14

您只需在应用程序类中加载ad并在任何活动中使用它就可以做到这一点。

您可以下载演示

当我这么做的时候,

应用程序类

import android.app.Application;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

public class App extends Application {

AdView adView;

@Override
public void onCreate() {
    // TODO Auto-generated method stub

    super.onCreate();

    adView = new AdView(this);
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId("ca-app-pub-1267746788642565/8418489933");
    // Request for Ads
    AdRequest adRequest = new AdRequest.Builder().build();

    // Load ads into Banner Ads
    adView.loadAd(adRequest);
}

public void loadAd(LinearLayout layAd) {

    // Locate the Banner Ad in activity xml
    if (adView.getParent() != null) {
        ViewGroup tempVg = (ViewGroup) adView.getParent();
        tempVg.removeView(adView);
    }

    layAd.addView(adView);

}
}

主要活动

public class MainActivity extends Activity {

App app;
LinearLayout layAd;

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

    layAd = (LinearLayout) findViewById(R.id.layad);

    app = (App) getApplication();
    app.loadAd(layAd);

    Button btnNext = (Button) findViewById(R.id.next);
    btnNext.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent iNext = new Intent(MainActivity.this,
                    SecondActivity.class);
            startActivity(iNext);
        }
    });
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    app.loadAd(layAd);
    super.onResume();
}
}

第二项活动

public class SecondActivity extends Activity {

App app;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_second);

    LinearLayout layAd = (LinearLayout) findViewById(R.id.layad);

    app = (App) getApplication();
    app.loadAd(layAd);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admobdemo"
android:versionCode="1"
android:versionName="1.0" >

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

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

<application
    android:name="com.example.admobdemo.App"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.admobdemo.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.admobdemo.SecondActivity"
        android:label="@string/app_name" >
    </activity>

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
</application>
</manifest>

主活动布局xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >

<LinearLayout
    android:id="@+id/layad"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
</LinearLayout>

<Button
    android:id="@+id/next"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

和第二个活动布局xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >

<LinearLayout
    android:id="@+id/layad"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
 类似资料:
  • 我在同一个活动中使用了两个时间选择器,让用户为特定任务选择开始时间和停止时间,我使用了支持库中可用的DialogFragment类,以向后兼容旧的Android版本。

  • 问题内容: 我有4个选项: 邮件/邮件/ TW / FB 。我想为每个选项发送不同的活动! 对于例如:在表,我需要把:,,和。在邮件中,我需要将字符串放置在“主题”字段中,然后将“字符串”放置在主体中,并且也要放置。在 我想要放置图像的地方,例如社交文章,也有一些String和。 您是否知道在iOS8中使用Swift可以做到这一点? 我搜索了很多代码,但没有找到最适合我的代码。 问题答案: 您应该

  • 我是使用谓词的新手,不确定我是否正确理解了它。我有一个抽象的类,其中每小时和薪水员工是分开创建的。我的问题依赖于我的类,我不确定如何检查它是否是每小时员工并返回真或假。 我需要为以下所有条件创建不同的谓词: 所有员工,仅每小时一次,仅限工资和全职。 到目前为止,我只是想先让“仅每小时”谓词正常工作,然后再找出剩下的谓词。我不确定在“p”后面加什么来检查它是哪种类型的员工。我目前拥有的是: 我还有一

  • 我在处理活动和碎片时遇到了一些麻烦。我正处于应用程序开发的早期阶段,最终希望设计一种最有效的方法来整合两个独立的屏幕。 每种方法的好处是什么?你会选择哪种设计?

  • 在本活动中,我希望拥有导航抽屉,因此我扩展了'NavigationDrawer',在其他一些活动中,我希望使用相同的导航抽屉

  • 我使用了著名的Dagger ViewModelFactory模式,以便能够为所有活动中的所有视图模型注入工厂。 我遇到的问题是,当我将工厂注入到匕首时失败了,因为我不打算使用的对象的提供者并不总是可访问的。他们不是因为包含提供者的模块没有添加。 例如,我有一个LogIn活动和一个SignUp活动,这是我为它们添加子组件的方式: 请注意,当我为SignUpActivity创建子组件时,我没有添加模块