After a hard time and a busy afternoon, I finally figured out how to embed advertisements on the Android software interface. Below I will take the embedded rice advertisement as an example to summarize:
Step 1 download the meter ad SDK and import the youmi-android.jar into the project where you want to embed the ad.
1. Right-click your project root directory and choose Properties"
2. Select "Java Build Path" on the left panel"
3. Select the "Libraries" tab.
4. Click "Add External JARs „"
5. Select the directory path for the youmi-android.jar.
6. Click "OK". The import is successful.
Step 2: Configure user permissions in the AndroidManifest. xml file.
Be sure to configure the following permissions. Otherwise, the advertisement may not be obtained.
1. android. permission. INTERNET, connected to the INTERNET permission, used to request Advertisement
2. android. permission. READ_PHONE_STATE, used to accurately collect the system information of users' mobile phones
3. android. ACCESS_NETWORK_STATE, used to precisely identify network access points and other information
4. android. permission. ACCESS_COARSE_LOCATION helps to accurately serve regional ads and help collect regional distribution statistics of users who use applications
5. android. permission. WRITE_EXTERNAL_STORAGE helps to cache image resources, save traffic, and provide a better user experience.
Copy the following permission configuration code to the AndroidManifest. xml file:
<! -- Permission that must be affirmed -->
<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 precise user positioning information -->
<Uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION"/>
<! -- Use WIFI to obtain precise user location information -->
<Uses-permission android: name = "android. permission. ACCESS_WIFI_STATE"/>
Step 3: Add AdActivity to AndroidManifest. xml.
AdActivity is the carrier of advertisement 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 4: Initialize account information.
Call AdManager. init () in onCreate of the main Activity to initialize parameters such as App ID, App Secret, request ad interval, and test mode (please call it once before any AdView initialization ).
// The first parameter is your application release Id.
// The second parameter is your application password.
// The third parameter is the request interval. The valid value ranges from 30 to 200, in seconds.
// The fourth parameter is to set the test mode. If it is set to true, the test advertisement can be obtained. For official release, set this parameter to false.
AdManager. init (Context context, String appid, String appsec, int intervalSecond, boolean isTestMode );
! Note: In version 3.04, the parameters of the AdManager. init method are changed to five, and the Context parameter is added! In the debugging phase, set the test mode to true. Set the test mode to false and upload it to the website for review. ! Only test advertisements can be obtained for applications that have not been uploaded to the application installation package, applications that have not passed the review, and simulators that have passed the review, A real machine can get a normal advertisement.
The Code is as follows:
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// Application Id application password ad request interval (s) Test Mode
AdManager. init (this, "537ef88653a2993c", "b9e10bcfe994a9fb", 30, true );
SetContentView (R. layout. main );
}
Step 5: use xml layout to embed advertisements
1. Add attrs. xml to the res/values folder. If this file is not added, you cannot set the AdView attribute in layout. The content of the attrs. xml file is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Declare-styleable name = "net. youmi. android. AdView">
<! -- Ad background color [only valid for text ads] (value range: #000000 ---- # ffffff) -->
<Attr name = "backgroundColor" format = "color"/>
<! -- Ad text color [only valid for text ads] (value range: #000000 ---- # ffffff) -->
<Attr name = "textColor" format = "color"/>
<! -- Advertising background transparency [valid only for text ads]. The default value is 255. The value range is 0-255. -->
<Attr name = "backgroundTransparent" format = "integer"/>
</Declare-styleable>
</Resources>
2. embed the rice advertisement view in the layout main. xml:
The following is an instance:
<? Xml version = "1.0" encoding = "UTF-8"?>
<! -- You 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 = "155" type = "regxph" text = "yourobjectname"/>
</LinearLayout>
Note: xmlns: umadsdk = "http://schemas.android.com/apk/res/your application package name" must be added, otherwise the editor will prompt 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 when the activity is first created .*/
// Application Id application password ad request interval (s) Test Mode
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// Application Id application password ad request interval (s) Test Mode
AdManager. init (this, "537ef88653a2993c", "b9e10bcfe994a9fb", 30, true );
SetContentView (R. layout. main );
}
}
My Sina Weibo (wind Swiss): http://weibo.com/tianrui1990
Original Author: wind Rui
The final running result is shown in: