Monetization of the Android application using ads from AdMob with the option of paid disconnection. Part one

Hello! There was already an article on Habrahabr on how to make paid disconnection of ads in an Android application, which suggested using the open-source Android Billing Library . In this article I want to talk about how to implement such functionality using the internal market payment system Android Market In-app Billing, without using third-party libraries.

The article consists of two parts. In the first part, I will talk in detail about how to add ads from Google AdMob to my application (this part is mainly aimed at beginners), and in the second - how to make it turn off for a fee.

To get started, create a simple Android application project in the Eclipse environment. I won’t write about how this is done (you can read about it, for example, here ).

Installing prerequisites


To work with AdMob, we need to install the Google AdMob Ads SDK, as well as the SDK for the Android platform version 3.2 or higher (see here ). We also install the Google Market Billing package (we will talk about it in the second part of the article).

Open the “Android SDK Manager” using the main menu command Window > Android SDK Manager .



Select the necessary packages for installation and click the “Install packages ...” button.



After installing the packages, connect the AdMob Ads SDK to the project. For this:
  • Right-click on a project in Project Explorer and select Properties.
  • In the left part of the window, select "Java Build Path".
  • Click the "Add External JARs ..." button.
  • Select the GoogleAdMobAdsSdk-4.3.1.jar file, which is located in the "% ANDROID% \ extras \ google \ admob_ads_sdk \" folder.


As a result, the GoogleAdMobAdsSdk-4.3.1.jar file should appear in the project structure.



Application configuration settings


Next, open the application configuration file AndroidManifest.xml and make the following changes:
  • Optionally, you can add the android: installLocation = “preferExternal” parameter in the manifest tag . Using this option allows you to install the application on a memory card.
  • In the uses-sdk tag, add the android parameter : targetSdkVersion = "13" . Version 13 corresponds to the SDK for the Android platform version 3.2. If you installed a later SDK, insert the appropriate version number.
  • Next, add the permissions ACCESS_NETWORK_STATE and INTERNET .
  • And finally, add an activity tag with the parameters android: configChanges = "keyboard | keyboardHidden | orientation | screenLayout | uiMode | screenSize | smallestScreenSize" and android: name = "com.google.ads.AdActivity" in the body of the application tag .
Below is the contents of the AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"android:installLocation="preferExternal"package="com.sample.billing"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="13" /><uses-permissionandroid:name="android.permission.INTERNET" /><uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE" /><applicationandroid:debuggable="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name" ><activityandroid:label="@string/app_name"android:name=".BillingSampleActivity" ><intent-filter ><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"android:name="com.google.ads.AdActivity" /></application></manifest>

Now we need to call the project properties again. In the left part of the window, select "Android". Then, on the right side, select Android 3.2 in the "Project Build Target" panel. If you installed the Android SDK later, select it.



Obtaining AdMob Publisher ID


Next, go to the site www.admob.com (you can use your Google account for authorization). Here we need to get a "Publisher ID" to link ads to our application. We go to the "Sites & Apps" section and select the "Android App".


After entering the necessary fields, our application should appear in the list of "Sites & Apps". Now we hover over the name of the application and click the “Manage Settings” button that appears.



Then we get to the page that shows the "Publisher ID" for our application. Copy it, it will be useful to us further.



Adding an ad banner to the application


Back to the project again. Open res / values ​​/ strings.xml and create the following string value:
<stringname="admob_publisher_id">скопированный ранее Publisher ID</string>



Now open the res / layout / main.xml file.



Make the following changes to the file:
  • Add the xmlns parameter to the LinearLayout root tag : ads = " schemas.android.com/apk/lib/com.google.ads "
  • Add another LinearLayout and move the TextView into it .
  • Add the android: layout_weight = "1" parameter to the new LinearLayout . This is necessary so that it does not overlap the banner ad.
  • Add the com.google.ads.AdView component:
    <com.google.ads.AdViewandroid:id="@+id/adView"android:layout_width="wrap_content"android:layout_height="wrap_content"ads:adSize="BANNER"ads:adUnitId="@string/admob_publisher_id"ads:loadAdOnCreate="true" />
The contents of the main.xml file are shown below.

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><LinearLayoutandroid:id="@+id/linearLayout1"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"android:orientation="vertical" ><TextViewandroid:layout_width="fill_parent"android:layout_height="match_parent"android:text="@string/hello" /></LinearLayout><com.google.ads.AdViewandroid:id="@+id/adView"android:layout_width="wrap_content"android:layout_height="wrap_content"ads:adSize="BANNER"ads:adUnitId="@string/admob_publisher_id"ads:loadAdOnCreate="true" /></LinearLayout>

In the “Graphic Layout” mode, the layout looks as follows:



Now we connect our device (or emulator) and click Run . We look what happened.



Conclusion


As a result of our actions, we added AdMob ads to our application. As you can see, this is not difficult to do.
The next step will be the development of functionality for paid advertising disconnection using Android MArket In-app Billing, which I will discuss in the next part of the article.

Sources of the developed application can be downloaded here .

Thanks for attention. I hope that someone will find the material useful. I will be glad to your comments.

Used sources

  1. Google AdMob Ads Android Fundamentals
  2. Hello, World | Android Developers
  3. Stack overflow
To be continued ...

Also popular now: