Store arraylist in sharedpreference SharedPreference sPrefs=PreferenceManager.getDefaultSharedPreferences(context); SharedPreference.Editor sEdit=sPrefs.edit();
This guide will show you how to use Eclipse to integrate the Google Mobile Ads SDK into a brand new app and display a simple banner ad. It should take about thirty minutes to complete and will give you a good sense of how the SDK functions within an app.
The ad unit and samples that we provide return test ads. Test ads are always available, even if your account is suspended or disabled. For more information, review the AdMob policies and learn more about invalid activity.
Using live ads during development is against AdMob policy; if you test on live ads, your AdMob account may be suspended.
The guide is intended for developers who are familiar with Eclipse and have already used it to create Android apps. If you're new to Android coding, consider getting started with Android Studio. It's the official IDE for Android development.
No two developers have the same level of experience, so we've added occasional notes like this one for those who would like a little extra info. If you're an expert Android developer, feel free to skip them.
Prerequisites
Running Eclipse 3.7.2 (Indigo) or later
Eclipse JDT plugin (included in most Eclipse IDE packages)
In this step, we'll create a brand new project in Eclipse to use for our example. Open eclipse with an empty workspace and choose File > New > Project... from the menu.
Select a Wizard
Select the Android folder, then choose Android Application Project and click Next.
Create your project
Enter "BannerExample" as the app and project names, and whatever package name you normally use. Next, give the project a minimum required SDK version of 9. That's the minimum version supported by the Google Mobile Ads SDK.
Configure your new project
We're keeping it simple here, so uncheck the Create custom launcher icon checkbox. Everything else can stay as it is.
Create an activity
The defaults for this screen are just fine. The Create Activity checkbox should be checked, and the Blank Activitychoice selected in the list.
Name your activity
Here again, the default options are what you need. Just click Finish and Eclipse will create your new project's source code.
Compile your new project
Once Eclipse is finished, try compiling and running your new app. You should see a "Hello world!" message on an otherwise empty gray screen. Don't worry, we'll add some more content in the next steps.
Download the Google Play services SDK
The Google Play services SDK contains classes and resources which your app can use to request and display ads. Make sure that you have the latest version by opening up your SDK Manager. You can do this by selectingWindow > Android SDK Manager.
The SDK Manager
In the Android SDK Manager window, select Google Play services under the Extras folder, then pressInstall Packages and accept the licenses to download. If the Install Packages button is disabled, don't worry. That just means you already have the latest version, so there's nothing else you need to do in the SDK Manager.
Include the Google Play services library
Now that the Google Play services SDK is downloaded, you can reference it in your project. Select File > New > Project... from the menu.
Create a project from existing code
Select the Android folder, then the Android Project from Existing Code choice within it.
Import project
Now you just need to tell Eclipse where it can find the library. Click the Browse button and navigate to<android-sdk>/extras/google/google_play_services/libproject/google-play-services_lib/, and click the OK button. When the browse dialog closes, you should see a single item in theProjects list. Make sure the checkbox next to it is checked, then click Finish to create a new project with the imported code.
Reference the imported project
With the new project created, you can reference it in your app's code. Right-click BannerExample in the Package Explorer and select Properties.
Properties for BannerExample
Select the Android choice in the list of property sections on the left, then click the Add... button.
Project selection
You'll see google-play-services_lib in the list view. Choose it, then click OK to close the selection dialog, and again to close the properties dialog. Your app now contains a reference to the Google Play services library project, which means it can use the classes and resources contained in the Google Play services SDK.
Try building and running your app again to make sure everything is correctly in place. You won't see any changes, but including Google Play Services is the first step toward getting ads into your app.
Modify the manifest file
Now that you have a working app that includes Google Play Services, it's time to modify the app manifest file to include the permissions, version number, and activity definition that the Mobile Ads SDK requires. Open the Android manifest for the BannerExample project, AndroidManifest.xml, for editing. It will be under theBannerExample/src/ folder, though the exact path depends on the package name you chose when setting up the project.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.google.android.gms.example.bannerexample"><uses-sdkandroid:minSdkVersion="9"android:targetSdkVersion="21"/><!-- Include required permissions for Google Mobile Ads to run--><uses-permissionandroid:name="android.permission.INTERNET"/><uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme"><!--This meta-data tag is required to use Google Play Services.--><meta-dataandroid:name="com.google.android.gms.version"android:value="@integer/google_play_services_version"/><activityandroid:name=".MainActivity"android:label="@string/app_name"><intent-filter><actionandroid:name="android.intent.action.MAIN"/><categoryandroid:name="android.intent.category.LAUNCHER"/></intent-filter></activity><!--Include the AdActivity configChanges and theme. --><activityandroid:name="com.google.android.gms.ads.AdActivity"android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"android:theme="@android:style/Theme.Translucent"/></application></manifest>
There are three changes you need to make:
Add two <uses-permission> tags for INTERNET and ACCESS_NETWORK_STATE. The tag forINTERNET is required and used to access the Internet to make ad requests. ACCESS_NETWORK_STATE is optional, and used to check if an internet connection is available prior to making an ad request.
Add a <meta-data> tag that references the Google Play Services version. This lets Android know which version of the service your app expects to use.
Add an <activity> element with configChanges and theme attributes. This activity is used by the SDK when banners are clicked or interstitials are presented, and like any other activity must be declared in the manifest before being presented.
Go ahead and rebuild the project to make sure everything has been done correctly. You should still see the same "Hello world!" message for now. By configuring the App Manifest correctly, though, you've given your app the ability to use Mobile Ads.
Give your app an Ad Unit ID
An Ad Unit ID is a unique identifier given to the places in your app where ads are displayed. If you had an app with two activities, for example, each displaying a banner, you'd have two ad units, each with its own ID. AdMob ad unit IDs have the form ca-app-pub-XXXXXXXXXXXXXXXX/NNNNNNNNNN.
In order for your new app to display an ad, it needs to include an Ad Unit ID. Open your app's string resource file, which is found at BannerExample/res/values/strings.xml.
Add a new <string> tag as shown. Note that the Ad Unit ID provided above is just for testing. It will allow you to retrieve a sample banner ad and make sure your implementation is correct.
See the addTestDevice method documentation for information on how to get test ads with your own Ad Unit IDs.
Place an AdView in your main activity layout
There are only two steps remaining before your app is ready to show an ad. First, you'll need to modify your main activity's layout to include an AdView. Open BannerExample/res/layout/activity_main.xml in the editor.
A new element for your AdView. You'll be asked to provide layout_width and layout_height. You can set these both to wrap_content. In the AdView tag, set the adSize to BANNER and theadUnitId to @string/banner_ad_unit_id.
Load the ad in the MainActivity class
The last change needed is for you to add to your app's main activity class some Java code that will load an ad into the AdView.
Open your MainActivity.java file. It will be in the BannerExample/src/ folder, though again the exact subdirectory path will vary based on the domain you used when creating your project above. Once it's open in the editor, look for the onCreate method in the MainActivity class:
Add the code that will find your AdView in the layout, create an AdRequest, and then load an ad into theAdView with it.
Once that's completed, you're finished. You now have a fully functional AdView in your app's main activity.
Enjoy a freshly loaded ad
Your app is now ready to display an ad using the Google Mobile Ads SDK. Run it again, and you should see a test banner displayed at the bottom of the device screen:
Congratulations! You've successfully integrated banner ads into an app with Eclipse.