为什么我无法在 android studio 中使用 andengine 展示 admob 奖励视频广告?
Why I can't show admob rewarded video ads with andengine in android studio?
我在 android 工作室用 andengine 玩了一个游戏。我想在我的应用中展示 admob 奖励视频广告。但我无法展示它。有没有人可以帮助我?
我运行我自己的设备(小米6)上的应用程序。该应用程序在我自己的设备上正确启动。但是当我点击广告显示按钮时应用程序关闭("anaMenuEngland.oSprite")。
MainActivity.java
public class MainActivity extends BaseGameActivity implements RewardedVideoAdListener {
private static final int CAMERA_WIDTH = 800;
private static final int CAMERA_HEIGHT = 480;
private Camera camera;
private Engine engine;
private Nesne anaMenuArka,anaMenuEngland,bayrakhover;
Scene sahneOyun;
@Override
public Engine onLoadEngine() {
camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
final EngineOptions engineOptions = new EngineOptions(true, EngineOptions.ScreenOrientation.LANDSCAPE, new FillResolutionPolicy(), camera);
engineOptions.getTouchOptions().setRunOnUpdateThread(true);
engine = new Engine(engineOptions);
return engine;
}
@Override
public void onLoadResources() {
anaMenuArka = new Nesne(1024, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA, this, "gfx/arkaplan.png", 0, 0,null);
anaMenuEngland=new Nesne(64,32,TextureOptions.BILINEAR_PREMULTIPLYALPHA,this,"gfx/england.png",0,0,null);
bayrakhover=new Nesne(64,32,TextureOptions.BILINEAR_PREMULTIPLYALPHA,this,"gfx/bayrakhover.png",0,0,null);
Texture[]textures = {anaMenuArka.oTexture,anaMenuEngland.oTexture,bayrakhover.oTexture};
this.engine.getTextureManager().loadTextures(textures);
}
@Override
public Scene onLoadScene() {
anaMenuArka.oSprite = new Sprite(0, 0, anaMenuArka.oTextureRegion);
bayrakhover.oSprite = new Sprite(0, 200, bayrakhover.oTextureRegion);
anaMenuEngland.oSprite = new Sprite(0, 248, anaMenuEngland.oTextureRegion)
{
@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
float pTouchAreaLocalX, float pTouchAreaLocalY)
{
if(pSceneTouchEvent.isActionDown())
{
}
if(pSceneTouchEvent.isActionUp())
{
//when this section is clicked to close the application (error)
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
}
}
return true;
}
};
this.sahneOyun = new Scene();
this.sahneOyun.attachChild(anaMenuArka.oSprite);
this.sahneOyun.attachChild(anaMenuEngland.oSprite);
this.sahneOyun.attachChild(bayrakhover.oSprite);
this.sahneOyun.registerTouchArea(anaMenuEngland.oSprite);
return sahneOyun;
}
private RewardedVideoAd mRewardedVideoAd;
@Override
public void onLoadComplete() {
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
mRewardedVideoAd.setRewardedVideoAdListener((RewardedVideoAdListener) this);
loadRewardedVideoAd();
}
private void loadRewardedVideoAd() {
mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().build());
}
@Override
public void onRewardedVideoAdLoaded() {
}
@Override
public void onRewardedVideoAdOpened() {
}
@Override
public void onRewardedVideoStarted() {
}
@Override
public void onRewardedVideoAdClosed() {
loadRewardedVideoAd();
}
@Override
public void onRewarded(RewardItem rewardItem) {
}
@Override
public void onRewardedVideoAdLeftApplication() {
}
@Override
public void onRewardedVideoAdFailedToLoad(int i) {
}
@Override
public void onRewardedVideoCompleted() {
}
}
Nesne.java
public class Nesne{
public int oTextureWidth, oTextureHeight;
public Texture oTexture;
public TextureRegion oTextureRegion;
public Sprite oSprite;
public TimerHandler timer;
public Nesne(int oTextureWidth, int oTextureHeight, TextureOptions oTextureOptions, Context oContext, String oAssetPath, int oTexturePositionX, int oTexturePositionY, ITextureSource oTextureSource)
{
this.oTextureWidth = oTextureWidth;
this.oTextureHeight = oTextureHeight;
oTexture = new Texture(oTextureWidth, oTextureHeight, oTextureOptions);
if (oAssetPath==null){
oTextureRegion=TextureRegionFactory.createFromSource(oTexture,oTextureSource,oTexturePositionX,oTexturePositionY);
}else{
oTextureRegion = TextureRegionFactory.createFromAsset(oTexture, oContext, oAssetPath, oTexturePositionX, oTexturePositionY);
}
}
public Nesne() {
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.deneme">
<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">
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
您必须从主线程调用广告相关方法。只需使用 runOnUiThread
方法包装您的代码:
runOnUiThread(new Runnable() {
@Override
public void run() {
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
}
}
});
我在 android 工作室用 andengine 玩了一个游戏。我想在我的应用中展示 admob 奖励视频广告。但我无法展示它。有没有人可以帮助我?
我运行我自己的设备(小米6)上的应用程序。该应用程序在我自己的设备上正确启动。但是当我点击广告显示按钮时应用程序关闭("anaMenuEngland.oSprite")。
MainActivity.java
public class MainActivity extends BaseGameActivity implements RewardedVideoAdListener {
private static final int CAMERA_WIDTH = 800;
private static final int CAMERA_HEIGHT = 480;
private Camera camera;
private Engine engine;
private Nesne anaMenuArka,anaMenuEngland,bayrakhover;
Scene sahneOyun;
@Override
public Engine onLoadEngine() {
camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
final EngineOptions engineOptions = new EngineOptions(true, EngineOptions.ScreenOrientation.LANDSCAPE, new FillResolutionPolicy(), camera);
engineOptions.getTouchOptions().setRunOnUpdateThread(true);
engine = new Engine(engineOptions);
return engine;
}
@Override
public void onLoadResources() {
anaMenuArka = new Nesne(1024, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA, this, "gfx/arkaplan.png", 0, 0,null);
anaMenuEngland=new Nesne(64,32,TextureOptions.BILINEAR_PREMULTIPLYALPHA,this,"gfx/england.png",0,0,null);
bayrakhover=new Nesne(64,32,TextureOptions.BILINEAR_PREMULTIPLYALPHA,this,"gfx/bayrakhover.png",0,0,null);
Texture[]textures = {anaMenuArka.oTexture,anaMenuEngland.oTexture,bayrakhover.oTexture};
this.engine.getTextureManager().loadTextures(textures);
}
@Override
public Scene onLoadScene() {
anaMenuArka.oSprite = new Sprite(0, 0, anaMenuArka.oTextureRegion);
bayrakhover.oSprite = new Sprite(0, 200, bayrakhover.oTextureRegion);
anaMenuEngland.oSprite = new Sprite(0, 248, anaMenuEngland.oTextureRegion)
{
@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
float pTouchAreaLocalX, float pTouchAreaLocalY)
{
if(pSceneTouchEvent.isActionDown())
{
}
if(pSceneTouchEvent.isActionUp())
{
//when this section is clicked to close the application (error)
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
}
}
return true;
}
};
this.sahneOyun = new Scene();
this.sahneOyun.attachChild(anaMenuArka.oSprite);
this.sahneOyun.attachChild(anaMenuEngland.oSprite);
this.sahneOyun.attachChild(bayrakhover.oSprite);
this.sahneOyun.registerTouchArea(anaMenuEngland.oSprite);
return sahneOyun;
}
private RewardedVideoAd mRewardedVideoAd;
@Override
public void onLoadComplete() {
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
mRewardedVideoAd.setRewardedVideoAdListener((RewardedVideoAdListener) this);
loadRewardedVideoAd();
}
private void loadRewardedVideoAd() {
mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().build());
}
@Override
public void onRewardedVideoAdLoaded() {
}
@Override
public void onRewardedVideoAdOpened() {
}
@Override
public void onRewardedVideoStarted() {
}
@Override
public void onRewardedVideoAdClosed() {
loadRewardedVideoAd();
}
@Override
public void onRewarded(RewardItem rewardItem) {
}
@Override
public void onRewardedVideoAdLeftApplication() {
}
@Override
public void onRewardedVideoAdFailedToLoad(int i) {
}
@Override
public void onRewardedVideoCompleted() {
}
}
Nesne.java
public class Nesne{
public int oTextureWidth, oTextureHeight;
public Texture oTexture;
public TextureRegion oTextureRegion;
public Sprite oSprite;
public TimerHandler timer;
public Nesne(int oTextureWidth, int oTextureHeight, TextureOptions oTextureOptions, Context oContext, String oAssetPath, int oTexturePositionX, int oTexturePositionY, ITextureSource oTextureSource)
{
this.oTextureWidth = oTextureWidth;
this.oTextureHeight = oTextureHeight;
oTexture = new Texture(oTextureWidth, oTextureHeight, oTextureOptions);
if (oAssetPath==null){
oTextureRegion=TextureRegionFactory.createFromSource(oTexture,oTextureSource,oTexturePositionX,oTexturePositionY);
}else{
oTextureRegion = TextureRegionFactory.createFromAsset(oTexture, oContext, oAssetPath, oTexturePositionX, oTexturePositionY);
}
}
public Nesne() {
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.deneme">
<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">
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
您必须从主线程调用广告相关方法。只需使用 runOnUiThread
方法包装您的代码:
runOnUiThread(new Runnable() {
@Override
public void run() {
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
}
}
});