您的位置:首页 > 移动开发

如何在自己的Android APP中加入广告

2013-06-27 21:32 399 查看
最常用的就是使用google admob。

首先是需要在www.admob.com获得自己的发布者ID并且下载admob的JAR包作为开发使用。

获得ID和JAR包后具体做法如下:

注意:admob要求的最低版本为android1.5,目标编译版本最低为android3.2,即

android:minSdkVersion="3"

android:targetSdkVersion="13"

若targetSdkVersion无法满足,请在项目上点击右键->Properties->Android,选在Project Build Target,然后点击Apply,OK保存即可。

1.首先是将JAR包导入到eclipse

最新版的ADT和SDK在导入JAR包的时候,应该先将JAR包Copy到libs目录下再通过Build Path导入,否则会出现错误。

2.在AndroidManifest.xml中声明com.google.ads.AdActivity

<application

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name="com.example.adtest.MainActivity"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<activity android:name="com.google.ads.AdActivity"

android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />

</application>

注意

android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize",如果android:targetSdkVersion低于13这里会报错。

3.申请网络权限

<uses-permission android:name="android.permission.INTERNET"/>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

4.以上做好编译无错误后就可以具体写java和布局文件了

广告的显示有两种方式:一是直接在XML中布局显示,二是通过代码动态加入。

方法一:xml中布局显示:

<com.google.ads.AdView android:id="@+id/adView"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

ads:adUnitId=“MY_AD_UNIT_ID”

ads:adSize="BANNER"

ads:loadAdOnCreate="true"/>

方法二:通过JAVA代码动态加入:

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

adView = new AdView(this, AdSize.BANNER,"a151ca4d8cc2454");

LinearLayout layout = (LinearLayout)findViewById(R.id.layout1);

// Add the adView to it

layout.addView(adView);

// Initiate a generic request to load it with an ad

adView.loadAd(new AdRequest());

}

@Override

protected void onDestroy() {

// TODO Auto-generated method stub

if (adView != null) {

adView.destroy();

}

super.onDestroy();

}

OK,就是这么简单。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: