hello friends i wnat ot integrate admob in my application so i set following code for that
Main.java
public class Main extends Activity{
AdView adView;
RelativeLayout mRelativeLayoutRoot;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.advs);
mRelativeLayoutRoot=(RelativeLayout)findViewById(R.id.root);
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(getResources().getString(R.string.admob_unit_id));
adView.setId(11);
LayoutParams lp;
lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); // You might want to tweak these to WRAP_CONTENT
lp.addRule(mRelativeLayoutRoot.ALIGN_PARENT_BOTTOM);
mRelativeLayoutRoot.addView(adView, lp);
AdRequest adRequest = new AdRequest.Builder()
.build();
adView.loadAd(adRequest);
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".Main"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
</application>
</manifest>
When i run above code advertisement show only Jellybean , kitkat mobile device in Lolliop device 5.1 it is not showing any idea how can i solve this problem ?
EDIT
When i run this cod ein Micomax android one phone (Lollipop 5.1) it is not show advertise on that
Suggest you start by creating your AdView in the XML layout for your Activity rather than in code. It will help eliminate a few potential errors.
Next, make sure that you have added the deviceIds for the 2 devices as test device ids.
Finally, look at your logcat for the 2 devices and see what the Admob log says in the case when the ad does not load.
Related
I am making a simple app, in this app I am add a navigation drawer & drawer item have contain website url link when I click navigation item it was open another brawser with url that I have given & I am put google admob ads in this app so when I click on this item page was open with admob interstitial ads nice but my problem is when ad was shown I am click back button ad was close & again my app is open but I want to shown that page (my url shown in browser) but it now happen again open my app when I close the admob interstitial ads. How to fix that pronlem, please any one help me.
watch video for better understand : https://drive.google.com/open?id=0B8YTgurIRbm5SS01YW1WRHptYlU
My AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="26" />
<supports-screens
android:resizeable="true"
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true"
/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<application
android:allowBackup="true"
android:icon="#mipmap/logo_icon"
android:label="#string/app_name"
android:roundIcon="#mipmap/logo_circle"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".SpleshScreen.SpleshScreen"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"
android:configChanges="orientation|screenSize">
</activity>
<service android:name=".Messaging.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent"
android:noHistory="true"/>
</application>
You need to implement the adListner() for the ads, put this in your code after you initialize your Interstitial Code
InterstitialAd mInterstitialAd = new InterstitialAd(getActivity());
mInterstitialAd.setAdUnitId(mContext.getString(R.string.admob_ad_unit_id_interstitial));
mInterstitialAd.loadAd(new AdRequest.Builder().
build());
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
super.onAdClosed();
// perform your task after the ad is closed
Intent intent = new Intent(CurrentAct.this, GoToAct.class);
startActivity(intent);
// load the ad again if you need to show it again
mInterstitialAd.loadAd(new AdRequest.Builder().
build());
}
#Override
public void onAdLoaded() {
super.onAdLoaded();
}
});
// open your ad
btnclick(){
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
// if your ad is not ready to be shown, perform your task here
Intent intent = new Intent(CurrentAct.this, GoToAct.class);
startActivity(intent);
}
}
I have a game on Google Play it was showing ads (Banner ads) now there is no ads are displayed
What are the possible causes ?
In build.gradle I have this:
com.google.android.gms:play-services-ads:9.2.0
the latest version is 9.4.0 could this be a possible cause ? But in this way if it is a cause I should update my app for each update of google-play-services
Another thing: I put ads in play screen so player see ads most of the time Is this a problem as I read something about accidental clicks within Admob policies
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mygdx.clockspuzzle"
android:versionCode="3"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="Clocks Puzzles"
android:theme="#style/GdxTheme" >
<activity
android:name="com.mygdx.clockspuzzle.AndroidLauncher"
android:label="Clocks Puzzles"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
AndroidLauncher class:
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout layout = new RelativeLayout(this);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
// gameView
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
View gameView = initializeForView(new GameMain(this), config);
// adView
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(AD_UNIT_ID);
AdRequest.Builder builder = new AdRequest.Builder();
adView.loadAd(builder.build());
// add gameView
layout.addView(gameView);
// add adView
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
layout.addView(adView, adParams);
setContentView(layout);
}
the point that I am talking about is ads were displayed correctly without a problem.
I'm making a 2D game in Unity3D. When a main character dies, a menu with "restart" ( which is Application.LoadLevel("thisLevel")) and "go to main menu" options appears. But I need interstitial ad to appear before menu (Character dies => ad appears). I did it. But here is the problem: Character dies, interstitial ad appears, I close it, press restart button in menu, I make character die again, and ad appear just after 1-2 minutes, which is strange. Then I press restart and make character die again, it appears instantly, as it should be. But if I don't wait for ad after second "death" ad does not appear after third "death" and so on. Here is my code:
using UnityEngine;
using System.Collections;
using UnityEngine.SocialPlatforms;
using GoogleMobileAds.Api;
public class AdBehaviourScript : MonoBehaviour {
private const string INTERSTITIAL_ID = "ca-app-pub-myNumers";
public InterstitialAd interstitial;
private bool showed;
void Start () {
DontDestroyOnLoad(transform.gameObject);
showed = false;
interstitial = new InterstitialAd(INTERSTITIAL_ID);
AdRequest request2 = new AdRequest.Builder().Build();
interstitial.LoadAd(request2);
}
void Update () {
}
public void showAd () {
if (interstitial.IsLoaded() && !showed) {
interstitial.Show();
showed = true;
}
}
public void destroyAd () {
interstitial.Destroy();
}
}
I call functions from this script when character dies. Here is my manifest file if you need:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="preferExternal" package="com.example.admobtest" android:versionName="1.0" android:versionCode="1">
<supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
<!-- Google Mobile Ads Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application android:icon="#drawable/app_icon" android:label="#string/app_name" android:debuggable="false">
<!-- meta-data tag for Google Play services -->
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version"/>
<activity android:name="com.unity3d.player.UnityPlayerProxyActivity" android:label="#string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" >
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="#string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" >
</activity>
<activity android:name="com.unity3d.player.UnityPlayerNativeActivity" android:label="#string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" >
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
<meta-data android:name="android.app.lib_name" android:value="unity" />
</activity>
<activity android:name="com.unity3d.player.VideoPlayer" android:label="#string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigaton|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" >
</activity>
<!-- Google Mobile Ads Activity -->
<activity android:name="com.google.android.gms.ads.AdActivity"
android:label="#string/app_name"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigati on|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
</activity>
</application>
<uses-feature android:glEsVersion="0x00020000" />
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="19" />
</manifest>
What is my problem?
Try hiding the ad instead of destroying it
You should check isInterstitialReady before displaying the ad.
Actually what happens is that when an ad is displayed then the admob plugin start downloading new ad. due to which when you call the ad the second time it is not yet downloaded and displays after a sec. You should check if your ad is ready then dislpay your ad.
I created a Android Game and the issue i am using both Banner ad and a Interstitial ad
1) Banner ad shows while playing the game and 2) Interstitial Ad shows when player ends up winning a level but the problem i am facing is admob interstitial ad is showing up but one cannot click it, it only display at the level end but user not able to click it i tried searching around but couldn't find the solution for it,
Please help with it
These are the source usage of the ad code in the game
The Activity file :
public class APP extends Activity
{
private InterstitialAd interstitial;
}
public static void showInterAd(){
((APP)APP.ctx).showAd();
}
// BANNER AD
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ctx = this;
setVolumeControlStream(AudioManager.STREAM_MUSIC);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
// Create ad request.
AdRequest adRequest = new AdRequest.Builder().build();
// Look up the AdView as a resource and load a request.
AdView adView = (AdView)this.findViewById(R.id.adView);
adView.loadAd(adRequest);
}// INTERSTITIAL AD
public void showAd(){
((Activity)ctx).runOnUiThread(new Runnable() {
#Override
public void run() {
// Create the interstitial.
interstitial = new InterstitialAd(APP.this);
interstitial.setAdUnitId("ca-app-pub-ID");
AdRequest adRequest = new AdRequest.Builder().build();
interstitial.loadAd(adRequest);
// Set the AdListener.
interstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
if (interstitial.isLoaded()) {
interstitial.show();
I am calling the Ad code in different file :
levelCompleted = true;
endOfGame = true;
soundManager.playSound(APP.SOUND_WON);
APP.showInterAd();
}
The Manifest File :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.XYZ"
android:installLocation="preferExternal"
android:versionCode="1"
android:versionName="1.0" >
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"
android:required="false" />
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"
android:required="false" />
<uses-permission
android:name="android.permission.ACCESS_MOCK_LOCATION"
android:required="false" />
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"
android:required="false" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar">
<activity
android:name="com.xyz.APP"
android:alwaysRetainTaskState="true"
android:configChanges="orientation|keyboard"
android:label="#string/app_name"
android:launchMode="singleInstance"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="com.XYZ.GAME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
</application>
</manifest>
Remove :
android:launchMode="singleInstance" from Manifest File.
OK you have many problems with your code. I suggest you start by following the examples on https://developers.google.com/mobile-ads-sdk/docs/admob/advanced.
Several things you really need to fix:
Get rid of static attributes and methods in your Activity. Eg showInterAd()
Create your interstitial in Activity.onCreate(), load it before your game, show it after your game.
I have succefully add and admob ad to my game. But the first time i load it, and only the first time, after i lock the device or go to home when i open again the game the textures are all white rectangles but the game is still running and i can hear the sounds and the actors are still working correctly but the ad is showing just fine.
PD. I just discovered that it only happens if i open the game just after installing it. If i finish the instalation, close the installation wizzard(?) and then open normaly the game the problem does not happen.
I donĀ“t know whats is the reason of this.
My code is as it follows:
MainActivity.java
public class MainActivity extends AndroidApplication {
private AdView adView;
RelativeLayout layout;
View gameView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = false;
cfg.useAccelerometer = false;
cfg.useCompass = false;
// Create the layout
layout = new RelativeLayout(this);
// Do the stuff that initialize() would do for you
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
// Create the libgdx View
gameView = initializeForView(new MyGame(), false);
// Create and setup the AdMob view
adView = new AdView(this);
adView.setAdUnitId("MY ID");
adView.setAdSize(AdSize.BANNER);
adView.setBackgroundColor(Color.TRANSPARENT);
adView.loadAd(new AdRequest.Builder()
.build());
// Add the libgdx view
layout.addView(gameView);
// Add the AdMob view
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
adParams.addRule(RelativeLayout.CENTER_IN_PARENT);
//adView.setVisibility(View.VISIBLE);
layout.addView(adView, adParams);
// Hook it all up
setContentView(layout);
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.me.Prototipo_Bombas"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="19" /><application android:icon="#drawable/ic_launcher" android:label="#string/app_name">
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version" />
<activity android:name=".MainActivity" android:label="#string/app_name" android:screenOrientation="landscape" android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|s mallestScreenSize" />
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>
If i lock the device or go to home the sounds are still working.
After closing the game the next time that i open it everything is fine. I don't know why it happens once.
I hope someone could help me with this problem.
LibGdx#initialiseForView sets the ContentView. You are then calling setContentView again later in onCreate. setContentView should only be called once. That is your problem.