Android - add ads to your app

The following is the advertising promotion implemented by using the SDK of Baidu Mobile Alliance.

   The effect diagram is as follows:

      

It can be seen that at the bottom of the screen is the self-implemented advertisement placement.

  It is very simple to implant ads. Each ad network will have its own corresponding SDK. Just follow the SDK instructions step by step.

  The following are the steps I have summarized for the placement of ads:

  1. Import the jar package of the SDK (put it in the libs directory)

  

 

2. Add the corresponding permissions

 

<span style="font-family:KaiTi_GB2312;font-size:18px;"><!-- Permissions that must be declared-->  
    <uses-permission android:name="android.permission.INTERNET" />  
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />  
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
    <!-- SDK 2.1 new permissions, it is recommended to use -->  
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />  
    <!-- SDK 3.0 new permissions, it is recommended to use (optional) -->  
    <uses-permission android:name="android.permission.RECORD_AUDIO" />  
    <uses-permission android:name="android.permission.VIBRATE" />  
    <uses-permission android:name="android.permission.CAMERA" />  
    <uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" /></span>  

  3. Add the APPSID and billing name obtained from the ad network

 

 

<!-- The APPSID you obtained from the Baidu Mobile Alliance website -->  
<meta-data android:name="BaiduMobAd_APP_ID" android:value="debug" />   
<!-- The billing name you obtained from the Baidu Mobile Alliance website-->  
<meta-data android:name="BaiduMobAd_APP_SEC" android:value="debug" /></span>  

 It is currently used for debugging. When actually publishing, replace "debug" with the corresponding value.

 

4. Add application details Activity

<activity android:name="com.baidu.mobads.AppActivity" android:configChanges="keyboard|keyboardHidden|orientation" />

 

5. Create the attrs.xml file in the values ​​directory

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="com.baidu.mobads.AdView">
        <attr name="adSize" format="integer" />
        <attr name="adId" format="string" />
    </declare-styleable>
</resources>  

 6. Add a custom View to the layout file

<com.baidu.mobads.AdView  
        android:id="@+id/adView"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:layout_alignParentBottom="true"  
        android:layout_alignParentLeft="true"  
        android:layout_below="@id/loadmore_btn"  
        baiduadsdk:adSize="0" />  

 A namespace is used here: baiduadsdk, a namespace needs to be introduced

xmlns:baiduadsdk=http://schemas.android.com/apk/res/com.xiaowu.news  

 Note: com.xiaowu.news is the package name in the AndroidMenifest.xml file

Regarding the layout of the advertisement, there are two forms:

1. Declarative layout (the above example is this)

2. Code layout

 

Method 2: Dynamically join through JAVA code:
@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();
}

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326688826&siteId=291194637