在 android 專案中 加入 admob


在 Android 專案中加入 admob 的方法可以參考這篇,裡面的步驟1~4是一模一樣的,以我的版權聲名流程為例,程式碼修改為

   1:  package com.example.helloworld;
   2:   
   3:  import android.animation.AnimatorSet;
   4:  import android.animation.ObjectAnimator;
   5:  import android.animation.ValueAnimator;
   6:  import android.annotation.SuppressLint;
   7:  import android.app.Activity;
   8:  import android.app.AlertDialog;
   9:  import android.content.Context;
  10:  import android.content.DialogInterface;
  11:  import android.content.Intent;
  12:  import android.content.pm.ActivityInfo;
  13:  import android.graphics.Typeface;
  14:  import android.net.ConnectivityManager;
  15:  import android.net.NetworkInfo;
  16:  import android.net.Uri;
  17:  import android.os.Bundle;
  18:  import android.view.Gravity;
  19:  import android.view.View;
  20:  import android.view.View.OnClickListener;
  21:  import android.view.Window;
  22:  import android.view.WindowManager;
  23:  import android.widget.Button;
  24:  import android.widget.LinearLayout;
  25:  import android.widget.TextView;
  26:   
  27:  import com.google.ads.AdRequest;
  28:  import com.google.ads.AdSize;
  29:  import com.google.ads.AdView;
  30:   
  31:   
  32:  @SuppressLint("NewApi")
  33:  public class CopyRightFlow extends Activity{
  34:   
  35:      static final String tLog = "Trace Log";
  36:   
  37:      TextView tv; // init by xml use
  38:   
  39:      Button bYes; // enter button
  40:      Button bNo; // exit button
  41:      
  42:      AdView adView;
  43:      
  44:      @Override
  45:      public void onCreate(Bundle savedInstanceState) {
  46:   
  47:          super.onCreate(savedInstanceState);
  48:          
  49:  //        setWindowFeature();
  50:          
  51:          initByXml();
  52:   
  53:      }
  54:      
  55:      void setWindowFeature(){
  56:          //fullscreen
  57:          getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  58:          //no title
  59:          requestWindowFeature(Window.FEATURE_NO_TITLE);
  60:          //portrait
  61:          setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  62:      }
  63:      
  64:      void initByXml() {
  65:          setContentView(R.layout.copyright);
  66:          
  67:          addAdmob();
  68:          
  69:          // TextView init
  70:          tv = (TextView) findViewById(R.id.textView1);// why can't cast to
  71:                                                          // TestTextView
  72:          tv.setTextColor(0xff888888); // set color
  73:          tv.setTextSize(18.0f); // set size
  74:          tv.setTypeface(Typeface.SERIF);// set typeface
  75:          tv.setText(R.string.copy_right);// change text
  76:          tv.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
  77:   
  78:          // ButtonYes init
  79:          bYes = (Button) findViewById(R.id.buttonYes); // get ButtonYes
  80:          OnClickListener bYesOc = new OnClickListener() { // build ButtonYes Listener
  81:   
  82:              @Override
  83:              public void onClick(View v) {
  84:                  // TODO Auto-generated method stub
  85:   
  86:  //                CopyRightFlow.this.finish(); // exit program
  87:  //                
  88:  //                Intent goAct = new Intent();// new a Intent
  89:  //                goAct.setClass(CopyRightFlow.this, GetDisplaySize.class); // setclass
  90:  //                startActivity(goAct); // start another Activity
  91:  //                
  92:  //                System.exit(0);
  93:                  
  94:                  
  95:                  ValueAnimator rotationY = ObjectAnimator.ofFloat(bYes, "rotationY", 0f, 360f);
  96:                  ValueAnimator rotationX = ObjectAnimator.ofFloat(bYes, "rotationX", 0f, 360f);
  97:                  rotationY.setDuration(1000);                
  98:                  rotationX.setDuration(500);
  99:                 
 100:                  AnimatorSet as = new AnimatorSet();
 101:                  as.playTogether(rotationY,rotationX);
 102:                  as.start();
 103:              }
 104:          };
 105:          bYes.setOnClickListener(bYesOc);// set ButtonYes Listener
 106:   
 107:          // ButtonNo init
 108:          bNo = (Button) findViewById(R.id.buttonNo); // get ButtonNo
 109:          bNo.setOnClickListener(new OnClickListener() { // build ButtonNo Listener
 110:   
 111:              @Override
 112:              public void onClick(View v) {
 113:                  // TODO Auto-generated method stub
 114:                  
 115:                  showAlertDialog();
 116:                  
 117:              }
 118:          });
 119:      }
 120:   
 121:      void showAlertDialog(){
 122:   
 123:          AlertDialog ad = new AlertDialog.Builder(this).create();
 124:          
 125:          ad.setTitle("警告");//設定警告標題
 126:          ad.setMessage("確定離開??");//設定警告內容
 127:          ad.setButton("確定", new DialogInterface.OnClickListener() {//設定按鈕1
 128:              
 129:              @Override
 130:              public void onClick(DialogInterface dialog, int which) {
 131:                  
 132:                  //點選按鈕1後執行的動作
 133:                  //檢查網路狀態
 134:                  ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
 135:   
 136:                  NetworkInfo ni = cm.getActiveNetworkInfo();
 137:                  if (ni == null) {//沒有網路
 138:   
 139:  //                    CopyRightFlow.this.finish();//結束程式
 140:                      System.exit(0);
 141:                  }
 142:                  else if (ni != null) {//若有網路先連結到外部網頁
 143:   
 144:                      if( ni.isConnected()){
 145:                          
 146:                      
 147:                      Uri uri = Uri.parse("http://vulpesadn.blogspot.tw/");
 148:                      Intent intent = new Intent(Intent.ACTION_VIEW, uri);
 149:                      startActivity(intent);
 150:   
 151:  //                    CopyRightFlow.this.finish();//再結束程序
 152:                      System.exit(0);
 153:                      }
 154:                  }
 155:              }
 156:          });
 157:          ad.setButton2("取消", new DialogInterface.OnClickListener() {//設定按鈕2
 158:              
 159:              @Override
 160:              public void onClick(DialogInterface dialog, int which) {
 161:                  
 162:                  //點選按鈕2後執行的動作
 163:                  //無動作
 164:              }
 165:          });
 166:          
 167:          ad.setCanceledOnTouchOutside(false);//當警告提示出現後,點選提示以外範圍,是否會取消提示,預設是true
 168:          
 169:          ad.setCancelable(false);//當警告提示出現後,點選其他實體按鈕(backkey等等),是否會取消提示,預設是true
 170:          
 171:          ad.show();//顯示按鈕
 172:      }
 173:      
 174:      void addAdmob() {
 175:   
 176:           adView = new AdView(this, AdSize.BANNER, "xxxxxxxxxxxxxxx");//xxxxxxxxxxxxxxx is your admob id    
 177:           LinearLayout layout = (LinearLayout) findViewById(R.id.AdLayout);
 178:           layout.addView(adView);
 179:           adView.loadAd(new AdRequest());
 180:   
 181:      }
 182:  }

第 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 修改如下

   1:  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   2:      xmlns:tools="http://schemas.android.com/tools"
   3:      android:layout_width="match_parent"
   4:      android:layout_height="match_parent" >
   5:   
   6:      <TextView
   7:          android:id="@+id/textView1"
   8:          android:layout_width="wrap_content"
   9:          android:layout_height="wrap_content"
  10:          android:layout_centerHorizontal="true"
  11:          android:layout_centerVertical="true"
  12:          android:text="@string/hello_world" />
  13:   
  14:      <Button
  15:          android:id="@+id/buttonYes"
  16:          android:layout_width="wrap_content"
  17:          android:layout_height="wrap_content"
  18:          android:layout_alignParentBottom="true"
  19:          android:layout_alignParentLeft="true"
  20:          android:text="繼續" />
  21:   
  22:      <Button
  23:          android:id="@+id/buttonNo"
  24:          android:layout_width="wrap_content"
  25:          android:layout_height="wrap_content"
  26:          android:layout_alignParentBottom="true"
  27:          android:layout_alignParentRight="true"
  28:          android:text="離開" />
  29:   
  30:      <LinearLayout
  31:      android:id="@+id/AdLayout"
  32:      android:layout_width="wrap_content"
  33:      android:layout_height="wrap_content"
  34:      ></LinearLayout>
  35:      
  36:  </RelativeLayout>

第 30 ~ 34 行就是新加入的佈局專門給 adview 使用,如果就這樣執行的話,廣告會顯示,不過你會看到1個奇怪的內容如下



















提示你必須在 AndroidManifest.xml 中建立 adactivity 以及使用 configChanges ,所以我們就跟著提是修改 AndroidManifest.xml 吧

在 <application>標籤中加入

   1:          <activity android:name="com.google.ads.AdActivity"
   2:          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

第 1 行建立 AdActivity
第 2 行使用指定的 configchanges

最後還要記得開網路,不然廣告也無法顯示
結果為




0 意見:

張貼留言

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Affiliate Network Reviews