[英]Adding Admob Ads to Java file instead of Xml
我在添加Admob廣告時遇到問題。 首先,我正在開發一個不使用xml布局的游戲。 我只是用Java。 在Google的說明中,它會查找linearlayout ID。 任何幫助都會很棒。 這是清單中聲明的主要活動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>
這是我想成為主要內容視圖的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.jar和示例的Admob Android SDK。
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);
字符串deviceid = tm.getDeviceId();
我認為這個例子對你有幫助
我發現使用adMob
在文件中顯式定義布局可能會產生巨大的麻煩,因為有時使用某些參數和布局組合可能會變得難以預測。 似乎總是可行的是將AdView
動態添加到一些LinearLayout
。
這應該工作:
final LinearLayout yourLayout = (LinearLayout) findViewById(R.id.your_linearlayout_id_where_you_want_to_put_your_adview);
final AdView adView = new AdView(this);
adView.setAdUnitId("your-admob-id");
adView.setAdSize(AdSize.BANNER);
yourLayout.addView(adView);
final AdRequest.Builder adReq = new AdRequest.Builder();
// You should include a line like this for testing purposes,
// but only after you've tested whether your AdView works!
// This will prevent your ad being loaded each time you test
// your ad, so it will prevent you being blocked from AdMob.
// You'll find your device_id in the LogCat.
adReq.addTestDevice("your_device_id");
final AdRequest adRequest = adReq.build();
adView.loadAd(adRequest);
我將通過XML配置您的TitleView。 例如,將布局更改為:
<?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" >
<your.titleview.package.name.here.TitleView
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
並刪除在onCreate中構造它的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.