在 Android 專案中加入 admob 的方法可以參考這篇,裡面的步驟1~4是一模一樣的,以我的版權聲名流程為例,程式碼修改為
package com.example.helloworld;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
@SuppressLint("NewApi")
public class CopyRightFlow extends Activity{
static final String tLog = "Trace Log";
TextView tv; // init by xml use
Button bYes; // enter button
Button bNo; // exit button
AdView adView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setWindowFeature();
initByXml();
}
void setWindowFeature(){
//fullscreen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//no title
requestWindowFeature(Window.FEATURE_NO_TITLE);
//portrait
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
void initByXml() {
setContentView(R.layout.copyright);
addAdmob();
// TextView init
tv = (TextView) findViewById(R.id.textView1);// why can't cast to
// TestTextView
tv.setTextColor(0xff888888); // set color
tv.setTextSize(18.0f); // set size
tv.setTypeface(Typeface.SERIF);// set typeface
tv.setText(R.string.copy_right);// change text
tv.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
// ButtonYes init
bYes = (Button) findViewById(R.id.buttonYes); // get ButtonYes
OnClickListener bYesOc = new OnClickListener() { // build ButtonYes Listener
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// CopyRightFlow.this.finish(); // exit program
//
// Intent goAct = new Intent();// new a Intent
// goAct.setClass(CopyRightFlow.this, GetDisplaySize.class); // setclass
// startActivity(goAct); // start another Activity
//
// System.exit(0);
ValueAnimator rotationY = ObjectAnimator.ofFloat(bYes, "rotationY", 0f, 360f);
ValueAnimator rotationX = ObjectAnimator.ofFloat(bYes, "rotationX", 0f, 360f);
rotationY.setDuration(1000);
rotationX.setDuration(500);
AnimatorSet as = new AnimatorSet();
as.playTogether(rotationY,rotationX);
as.start();
}
};
bYes.setOnClickListener(bYesOc);// set ButtonYes Listener
// ButtonNo init
bNo = (Button) findViewById(R.id.buttonNo); // get ButtonNo
bNo.setOnClickListener(new OnClickListener() { // build ButtonNo Listener
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showAlertDialog();
}
});
}
void showAlertDialog(){
AlertDialog ad = new AlertDialog.Builder(this).create();
ad.setTitle("警告");//設定警告標題
ad.setMessage("確定離開??");//設定警告內容
ad.setButton("確定", new DialogInterface.OnClickListener() {//設定按鈕1
@Override
public void onClick(DialogInterface dialog, int which) {
//點選按鈕1後執行的動作
//檢查網路狀態
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null) {//沒有網路
// CopyRightFlow.this.finish();//結束程式
System.exit(0);
}
else if (ni != null) {//若有網路先連結到外部網頁
if( ni.isConnected()){
Uri uri = Uri.parse("http://vulpesadn.blogspot.tw/");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
// CopyRightFlow.this.finish();//再結束程序
System.exit(0);
}
}
}
});
ad.setButton2("取消", new DialogInterface.OnClickListener() {//設定按鈕2
@Override
public void onClick(DialogInterface dialog, int which) {
//點選按鈕2後執行的動作
//無動作
}
});
ad.setCanceledOnTouchOutside(false);//當警告提示出現後,點選提示以外範圍,是否會取消提示,預設是true
ad.setCancelable(false);//當警告提示出現後,點選其他實體按鈕(backkey等等),是否會取消提示,預設是true
ad.show();//顯示按鈕
}
void addAdmob() {
adView = new AdView(this, AdSize.BANNER, "xxxxxxxxxxxxxxx");//xxxxxxxxxxxxxxx is your admob id
LinearLayout layout = (LinearLayout) findViewById(R.id.AdLayout);
layout.addView(adView);
adView.loadAd(new AdRequest());
}
}
第 42 行產生 AdView 的 reference, adview
第 67 行為建立 admob 廣告的方法,其內容定義在 174 ~ 181 行
第 176行產生 adview 物件,其中 xxxxxxxxxx 為你的發布商id
第 177 行使用 LinearLayout 佈局,這個佈局專門給 adview 使用
第 178 行加入 adview
第 179 行要求 顯示廣告
程式碼的部分是如此,接著請注意 addAdmob 方法是在 setContentView 後呼叫代表在 R.layout.copyright 必須加入給 adview 使用的 佈局,所以 R.layout.copyright 修改如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/buttonYes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="繼續" />
<Button
android:id="@+id/buttonNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="離開" />
<LinearLayout
android:id="@+id/AdLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></LinearLayout>
</RelativeLayout>
第 30 ~ 34 行就是新加入的佈局專門給 adview 使用,如果就這樣執行的話,廣告會顯示,不過你會看到1個奇怪的內容如下
提示你必須在 AndroidManifest.xml 中建立 adactivity 以及使用 configChanges ,所以我們就跟著提是修改 AndroidManifest.xml 吧
在 <application>標籤中加入
<activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
第 1 行建立 AdActivity
第 2 行使用指定的 configchanges
最後還要記得開網路,不然廣告也無法顯示
結果為

