android 加入廣告的方法

1:註冊用戶,下載jar包: 去 http://www.youmi.net/ 註冊個賬戶,然後 添加一個應用
2:在manifest.xml文件中配置:
<uses-permission android:name="android.permission.INTERNET" />
3:在你的activity種加入:
//請務必使用靜態的方法初始化AdManager,設置AppId、密碼、廣告請求間隔和
//測試模式等參數。
static{
//第一個參數是AppId
//第二個參數是應用密碼
//第三個參數是請求廣告的間隔,有效的設置值爲30 至200,單位爲秒
//第四個參數是設置測試模式,設置爲true 時,廣告的展示和點擊記錄無
// 效,以測試模式運行。設置爲false 時,廣告的展示和點
// 擊記錄有效,以發佈模式運行。請設置爲false。

AdManager.init("df587cb8a2c2a013 ","ef967beb5489644e ", 30, false);

AppId 和 應用密碼是你在 http://www.youmi.net添加應用後得到的
4:在res/values 文件夾中添加attrs.xml。
attrs.xml 文件的內容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="net.youmi.android.AdView">
<attr name="backgroundColor" format="color" />
<!--廣告條的純色背景色-->
<attr name="startbackgroundColor" format="color" />
<!--廣告條的漸變背景色起始顏色-->
<attr name="endbackgroundColor" format="color" />
<!--廣告條的漸變背景色的結束顏色-->
<attr name="clickBackgroundColor" format="color" />
<!--廣告條被點擊時的背景色-->
<attr name="textColor" format="color" />
<!--字體顏色-->
<attr name="backgroundTransparent" format="integer"/>
<!--背景透明度-->
</declare-styleable>
</resources>

5:在main.xml 中佈局有米廣告視圖。
以下爲一個實例:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
xmlns:umadsdk="http://schemas.android.com/apk/res/net.y
oum i.HelloYouMi"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content
" android:textSize="20px"
android:text="@string/hello"
/>
<net.youmi.android.AdView
android:id="@+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
umadsdk:textColor="#0fff22"
umadsdk:backgroundTransparent="155"
umadsdk:startbackgroundColor="#2B2B2B"
umadsdk:endbackgroundColor="#63B8FF"
umadsdk:clickBackgroundColor="#1E90FF"
/>
</LinearLayout>
注意:
xmlns:umadsdk="http://schemas.android.com/apk/res/net.youmi.H
ell oYouMi"
這句一定要加上,不然會提示錯誤。其中net.youmi.HelloYouMi是你應用程序包

6:請務必在有廣告控件的Activity裏面重載onDestroy()
方法,並調用adView.OnDestroy().
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
try {
AdView adView= (AdView) findViewById(R.id.ad);
if(adView!=null)
{
adView.OnDestroy();
}
} catch (Exception e) {
// TODO: handle exception
}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章