AdMod 廣告改版方式

現在Google不讓 APP 挾帶 GoogleAdMobAdsSdk.jar了。要從 Google Play Service 上要取得服務。因為忙碌的生活一直沒有去改善這一部份。從Google所限定的更改時間到現在,其實舊版還是可以使用的。但為了小心哪一天Google又大刀一揮,我們還是跟著改版吧!

整個流程就是安裝 Google Play Service 的過程。

第一步

到 Android SDK Manager,把Tools更新到最新版吧!

主要更新的有
Android SDK Tools
Android SDK Platform-tools
Android SDK Build-tools



PS. 如果這一步發生版本衝突無法安裝,請參考這一篇(),把衝突的版本移除。個人是在ADT上出現了問題,所以把舊ADT 2.0 移除。

移除後,從Help -> Install New Software,再次從  https://dl-ssl.google.com/android/eclipse/ 安裝即可。

第二步:
更新完,會在<android-sdk>/extras/google/底下長出一個 google_play_services,

如果沒有,請重開Eclipse -> Window -> Android SDK Manager 查看 Package 中的 extra 是否存在 google_play_services。如果存在,即可以安裝。

如果還是不存在,請將<android-sdk>/extras/google底下的資料都刪除,再一次重開Eclipse -> Window -> Android SDK Manager 此時extra就會出現google_play_services了。

PS. 我們可能忘了<android-sdk>的位置。沒關係,Eclipse -> Window -> Preferences -> Android 就會看到了。

第2步之1:
安裝完,在Eclipse 中 import -> Existing Android Code Into Workspace,位置在<android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib,記得複制一份到workspace下,再從workspace下的google-play-services_lib import進來Eclipse。google-play-service_lib完成import如下


第2步之2:
在想要使用google services的project上點右鍵 -> Properties -> android -> add... 將google_play_service_lib加入,如下

再選Java Build Path,將Android Private Libraries打勾



第三步:
修改 AndroidMainfest.xml 檔,紅色字為必需加的入的程式碼。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.google.android.gms.example.bannerexample" >
    <!-- Include required permissions for Google Mobile Ads to run  這是網路權限 -->  
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
    <!--This meta-data tag is required to use Google Play Services. 這是meta-data的標記 -->
    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
    <activity
        android:name=".MyActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <!--Include the AdActivity configChanges 這是ads的activity -->
    <activity android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
    </application>
</manifest>

第四步:

主程式中的使用法。其實與原本的程式碼是大同小異。可以參考

Google Play 服務遷移

以個人為例,本來的寫法為
adView= new AdView(this,AdSize.SMART_BANNER,"YOUR_AD_UNIT_ID"); //記得填ID
adsLL=(LinearLayout)findViewById(R.id.ads);                     //取得放廣告的layout          
adsLL.addView(adView);                                          //layout加入廣告
adView.loadAd(new AdRequest());                                 //請求載入廣告 

原本生命周期

@Override
public void onDestroy() {
  adView.destroy();
  super.onDestroy();
}

改成如下

adView = new AdView(this);
adView.setAdUnitId("YOUR_AD_UNIT_ID");                         //記得填ID
adView.setAdSize(AdSize.BANNER);
adsLL=(LinearLayout)findViewById(R.id.ads);                    //取得放廣告的layout
adsLL.addView(adView);                                         //layout加入廣加
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);                                      //請求載入廣告
 
改變後之生命周期
@Override
public void onPause() {
  adView.pause();
  super.onPause();
}

@Override
public void onResume() {
  super.resume();
  adView.resume();
}

@Override
public void onDestroy() {
  adView.destroy();
  super.onDestroy();
}

到此即大功告成。

留言

這個網誌中的熱門文章

python 找圖自動點擊

Python pyserial 抓取系統內的 COM PORT

VBA EXCEL 工作表變化 馬上執行 的作法 Worksheet_Change