我的应用有两个广告:
当我打开我的应用时,感兴趣的广告首先出现(数据打开时)。当(数据关闭)时,应用程序无法正常运行,例如按钮onclick等。只有当广告完成后,我的应用程序才能正常运行,否则它将无法在离线状态下运行。
我想在离线状态下运行我的应用,即使广告没有显示我的应用也应该有效。 希望你能帮助我,这个问题对其他人有帮助
我的项目
MainActivity.Java
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private InterstitialAd interstitial;
private AdView mAdView;
Button xxxx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//GOOGLE ADMOB FIREBASE ADS //
//Interstitial//
AdRequest adRequest2 = new AdRequest.Builder().build();
interstitial = new InterstitialAd(MainActivity.this);
interstitial.setAdUnitId("ca-app-pub-8736194125011489/4511020459");
interstitial.loadAd(adRequest2);
interstitial.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
displayInterstitial();
}
});
}
private void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
//Banner Ad//
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest1 = new AdRequest.Builder().build();
mAdView.loadAd(adRequest1);
//button//
xxxx = (Button)findViewById(R.id.xxxx);
xxxx.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent xxxx = new Intent(MainActivity.this, xxxx.class);
startActivity(xxxx);
}
});
清单
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jobyreuben.yyyyyyyyy">
<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:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SplashScreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"/>
<activity
android:name=".xxxx"
android:configChanges="orientation"
android:screenOrientation="portrait" />
</application>
</manifest>
答案 0 :(得分:0)
尝试推杆
xxxx = (Button)findViewById(R.id.xxxx);
在AdRequest adRequest2 = new AdRequest.Builder().build();
之前
答案 1 :(得分:0)
根据您的代码,您在displayInterstitial
方法中添加了按钮,该方法仅在通过onAdLoaded()
回调方法加载interstitialAd时调用,因此您首先在屏幕上显示广告,然后按钮在您关闭时工作在InterstitialAd。
在互联网关闭时,在柜台一侧,interstitialAd未加载onAdLoaded()
- &gt;未调用displayInterstitial()
,并且没有向您的按钮添加侦听器。
AndroidManifest.xml
如果要使用非页内广告
,请在内部应用程序代码中添加AdActivity<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"
android:theme="@android:style/Theme.Translucent" />
MainActivity
private InterstitialAd interstitial;
private AdView mAdView;
Button xxxx;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xxxx = (Button)findViewById(R.id.xxxx);
xxxx.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent xxxx = new Intent(MainActivity.this, xxxx.class);
startActivity(xxxx);
}
});
mAdView = (AdView) findViewById(R.id.adView);
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR); // for testing only
mAdView.loadAd(adRequestBuilder.build());
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("ca-app-pub-8736194125011489/4511020459");
interstitial.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {}
@Override
public void onAdClosed() {
loadIntersitialAd();
}
});
loadIntersitialAd();
}
private void loadIntersitialAd(){
AdRequest interstitialRequest = new AdRequest.Builder().build();
interstitial.loadAd(interstitialRequest);
}
public void displayInterstitial() {
if (interstitial.isLoaded()) interstitial.show();
}
public boolean isInterstitialLoaded(){
return interstitial.isLoaded();
}
@Override
protected void onResume() {
super.onResume();
mAdView.resume();
}
@Override
protected void onPause() {
super.onPause();
mAdView.pause();
}
@Override
protected void onDestroy() {
super.onDestroy();
mAdView.destroy();
}
}
每当您想要展示广告来电displayInterstitial()
。
修改强>
如果您希望在加载广告后立即显示插页式广告,请displayInterstitial()
来onAdLoaded()
,但这不是理想的方式,加载广告需要一些时间,因此您的活动来之间会有一些延迟在前台和非页内广告显示。
interstitial.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
displayInterstitial();
}
@Override
public void onAdClosed() {
loadIntersitialAd();
}
});
编辑2
当您从loadIntersitialAd()
回调方法调用onAdClosed()
时,请勿从displayInterstitial()
加载onAdLoaded()
,因为它会创建递归循环。
interstitial.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
displayInterstitial();
}
@Override
public void onAdClosed() {
}
});