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
}

}

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