How to implement ads in Android software

Source: Internet
Author: User
After a bit of tossing and bustle, busy all afternoon, finally understand the Android software interface embedded Advertising method, the following I have to embed rice ads for example summary:
Step one, download the meter advertising SDK, will youmi-android.jar into the project to embed ads.
1. Right key to your project root directory, select "Properties"
2. Select "Java build Path" in the left panel
3. Then select the "Libraries" tab
4. Click "Add External JARs" "
5. Select the directory path for Youmi-android.jar.
6. Click "OK" to import success
Step two, configure user permissions in the Androidmanifest.xml file.
Be sure to configure the following permissions, or you may not get the advertisement.
1. Android.permission.INTERNET, connect network permission Internet, use to request advertisement
2. Android.permission.READ_PHONE_STATE, for accurate statistics of user's mobile phone system Information
3. Android. Access_network_state, for accurate identification of network access points and other information
4. Android.permission.ACCESS_COARSE_LOCATION, helping to pinpoint geographic advertising and help to count the geographical distribution of users who use applications
5. Android.permission.WRITE_EXTERNAL_STORAGE, help to realize the cache of picture resources, save traffic, and get a better user experience
Please copy the following permission configuration code into the Androidmanifest.xml file:
<!--must declare the permissions-->
<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"/>
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<!--The following are optional permissions-->
<!--use GPS to obtain accurate positioning information for users-->
<uses-permission android:name= "Android.permission.ACCESS_FINE_LOCATION"/>
<!--use WiFi to obtain accurate user location information-->
<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE"/>
Step three, add the adactivity to the Androidmanifest.xml.
Adactivity is the carrier of advertising display, please add adactivity in Androidmanifest.xml:
<activity android:name= "Net.youmi.android.AdActivity"
android:configchanges= "Keyboard|keyboardhidden|orientation"/>
<meta-data android:name= "Youmi_channel" android:value= "0"/>
Step four, initialize the account information.
Call Admanager.init () initialize the app ID, app Secret, request ad interval, and test mode in the OnCreate of the main activity (be sure to call once before any Adview initialization).
The first parameter is the publication ID for your application
The second parameter is your application password
The third parameter is the interval at which the advertisement is requested, with a valid setting value of 30 to 200, in seconds
The fourth parameter is to set the test mode, and when set to true, you can obtain the test advertisement, set this parameter to False
Admanager.init (Context context,string AppID, String appsec, int intervalsecond, Boolean istestmode);
! Note: The 3.04 version starts with the Admanager.init method's parameter to five, plus the context parameter! Set the test mode to true during the debug phase, set the test mode to false and upload to the Web site awaiting approval.! Not uploaded application installation package, not approved by the application, simulator operation, can only get test ads, after the audit passed, the simulator is still the test ads, the real machine will get the normal ads.
The code is as follows:
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Apply ID apply password ad request interval (s) test mode
Admanager.init (This, "537ef88653a2993c", "B9E10BCFE994A9FB", true);
Setcontentview (R.layout.main);
}
Step Five, embed an advertisement with an XML layout
1. Add Attrs.xml to the Res/values folder. If you do not add this file, you will not be able to set the Adview properties in layout. The contents of the Attrs.xml file are as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<declare-styleable name= "Net.youmi.android.AdView" >
<!--advertising background color [valid for text ads only] (value range is #000000----#ffffff)-->
<attr name= "backgroundcolor" format= "Color"/>
<!--advertising text color [valid for text ads only] (value range is #000000----#ffffff)-->
<attr name= "TextColor" format= "Color"/>
<!--advertising background transparency [for text ads only valid], the default is 255, set range 0-255-->
<attr name= "backgroundtransparent" format= "integer"/>
</declare-styleable>
</resources>
2. Embedded in the layout main.xml has a meter advertising view:
The following is an example:
<?xml version= "1.0" encoding= "Utf-8"?>
<!--need to set the namespace: Umadsdk-->
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
xmlns:umadsdk= "Http://schemas.android.com/apk/res/com.youmi"
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >
<net.youmi.android.adview
Android:id= "@+id/adview"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Umadsdk:textcolor= "#ffffff"
Umadsdk:backgroundcolor= "#4076AA"
Umadsdk:backgroundtransparent= "/>"
</LinearLayout>
Note: xmlns:umadsdk= "http://schemas.android.com/apk/res/your application package name" must be added, otherwise the editor will prompt for an error.
3.XML Layout Code Section
Import Net.youmi.android.AdManager;
Import android.app.Activity;
Import Android.os.Bundle;

public class Testadactivity extends activity{
/** called the activity is a. */
Apply ID apply password ad request interval (s) test mode
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Apply ID apply password ad request interval (s) test mode
Admanager.init (This, "537ef88653a2993c", "B9E10BCFE994A9FB", true);
Setcontentview (R.layout.main);
}

}
The results of the final run are shown in the following illustration:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.