gpt4 book ai didi

java - 将 Admob 广告添加到 Java 文件而不是 Xml

转载 作者:行者123 更新时间:2023-11-29 08:52:29 25 4
gpt4 key购买 nike

我在添加 Admob 广告时遇到问题。首先,我正在开发一款不使用 xml 布局的游戏。我只使用Java。在 Google 的说明中,它会查找线性布局 ID。任何帮助都会很棒。这是我在 list 中声明的​​主要 Activity java 文件的代码:

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import com.google.android.gms.ads.*;

public class CrazyEightsActivity extends Activity {
/** Called when the activity is first created. */
private AdView adView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TitleView tView = new TitleView(this);
tView.setKeepScreenOn(true);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
// Create the adView.
adView = new AdView(this);
adView.setAdUnitId("ca-app-pub-6705249916909813/9815575080");
adView.setAdSize(AdSize.BANNER);

// Lookup your LinearLayout assuming it's been given
// the attribute android:id="@+id/mainLayout".
LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);

// Add the adView to it.
layout.addView(adView);

// Initiate a generic request.
AdRequest adRequest = new AdRequest.Builder().build();

// Load the adView with the ad request.
adView.loadAd(adRequest);
}
}

这是不应该使用的我的 xml 布局的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

</LinearLayout>

这里是我想作为主要内容 View 的 java 文件的代码:

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.View;

public class TitleView extends View {

private Bitmap titleGraphic;
private Bitmap playButtonUp;
private Bitmap playButtonDown;
private int screenW;
private int screenH;
private boolean playButtonPressed;
private Context myContext;

public TitleView(Context context) {
super(context);
myContext = context;
titleGraphic = BitmapFactory.decodeResource(getResources(), R.drawable.title_graphic);
playButtonUp = BitmapFactory.decodeResource(getResources(), R.drawable.play_button_up);
playButtonDown = BitmapFactory.decodeResource(getResources(), R.drawable.play_button_down);
}

@Override
public void onSizeChanged (int w, int h, int oldw, int oldh){
super.onSizeChanged(w, h, oldw, oldh);
screenW = w;
screenH = h;
System.out.println("SCREEN W: " + screenW);
System.out.println("SCREEN H: " + screenH);
}

@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(titleGraphic, (screenW-titleGraphic.getWidth())/2, 0, null);
if (playButtonPressed) {
canvas.drawBitmap(playButtonDown, (screenW-playButtonUp.getWidth())/2, (int)(screenH*0.7), null);
} else {
canvas.drawBitmap(playButtonUp, (screenW-playButtonUp.getWidth())/2, (int)(screenH*0.7), null);
}
}



public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
int X = (int)event.getX();
int Y = (int)event.getY();

switch (eventaction ) {

case MotionEvent.ACTION_DOWN:
if (X > (screenW-playButtonUp.getWidth())/2 &&
X < ((screenW-playButtonUp.getWidth())/2) + playButtonUp.getWidth() &&
Y > (int)(screenH*0.7) &&
Y < (int)(screenH*0.7) + playButtonUp.getHeight()) {
playButtonPressed = true;
}
break;

case MotionEvent.ACTION_MOVE:
break;

case MotionEvent.ACTION_UP:
if (playButtonPressed) {
Intent gameIntent = new Intent(myContext, GameActivity.class);
myContext.startActivity(gameIntent);
}
playButtonPressed = false;
break;
}
invalidate();
return true;
}
}

最佳答案

首先将Google Mobile Ads SDK放入lib文件夹

Admob 上激活您的帐户后,您需要在您的帐户中配置一个应用程序。在该应用程序之后,Admob 将为您提供适用于 Admob 的 Android SDK,其中包含 admob.jar 和工作示例。

private void AdmobFooterInitialize(){
adView = new AdView(this, AdSize.SMART_BANNER, "Your_AdMobPUBLISHERID");
LinearLayout layout = (LinearLayout)findViewById(R.id.linear);
layout.addView(adView);
AdRequest request = new AdRequest();
request.addTestDevice("your device id"); // this for testing
request.setTesting(false);
adView.loadAd(request);
}

在你的 manifest.xml 中

 <meta-data android:value="true" android:name="ADMOB_ALLOW_LOCATION_FOR_ADS" />
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

如何获取deviceId:-

final TelephonyManager tm =(TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);

String deviceid = tm.getDeviceId();

我认为这个例子对你有帮助

关于java - 将 Admob 广告添加到 Java 文件而不是 Xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22028671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com