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.
Related
How exactly is default activity defined?
As far as I can tell, adding "andriod.intent.action.MAIN" to AndroidManifest.xml like this:
<application
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN/>
That should define .MainActivity as the default activity, but it's not working. I've also tried adding the full package name com.package.MainActivity but that's also not working.
This is probably caused by my layout setup, I'm trying to define a layout dynamically in code... here's my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/mainlao">
</LinearLayout>
Then I define my TextureView layout in onCreate here:
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
LinearLayout layout = (LinearLayout) findViewById(R.id.mainlao);
mButton = new Button(this);
mButton.setText("pic");
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
mTextureView = new TextureView(this);
mTextureView.setSurfaceTextureListener(this);
layout.addView(mTextureView);
layout.addView(mButton, lp);
setContentView(layout);
}
I can't see how my layout would effect it, but this is the first time I've had this error, and the first time I tried doing a layout like this. No errors in code, and I tried invalidating caches.
Google hasn't turned up much yet, except for that android.intent.action.MAIN.. any help is appreciated.
Let me know if you need more information about the app.
The activity's category should be of type 'Launcher'
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN/>
<category android:name="android.intent.category.LAUNCHER" /> // This
</intent-filter>
</activity>
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.
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.
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'm in an early process of developping a game for Android and I'm trying to incorporate an ad banner using AdMob. I have downloaded the sample straight from the tutorial on the official site, so I guess that whatever I'm doing wrong here has to be basic since it crushes after a few seconds when debugging on my Galaxy S2 device. Pleae help.
package com.google.example.ads.fundamentals;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.LinearLayout;
/**
* A simple {#link Activity} that embeds an AdView.
*/
public class BannerSample extends Activity {
private AdView adView;
private final TelephonyManager tm =
(TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
private final String AD_MOB_ID = "my AdMob ID goes here";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create an ad.
adView = new AdView(this, AdSize.BANNER, AD_MOB_ID);
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
layout.addView(adView);
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(tm.getDeviceId());
// Start loading the ad in the background.
adView.loadAd(adRequest);
}
/** Called before the activity is destroyed. */
#Override
public void onDestroy() {
// Destroy the AdView.
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
}
The Logcat data in a screen shot is here
Edit: Also adding the Manifest.XML, which I suspect is what causeing the problems - amazingly the one that came with the example from the official site had an error in it (according to Eclipse), so I had to modify it a bit:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="3"
android:targetSdkVersion="13" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name">
<activity
android:name=".HelloAdMobActivity"
android:label="#string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:screenOrientation="landscape" >
</activity>
</application>
</manifest>
Logcat information would be super useful, but I'm betting on one of these two reasons:
You referenced the SDK as an external Jar, and didn't add it into your libs/ folder. You have two options here to fix this: add it to your libs/ folder, or go to Properties -> Java Build Path -> Order and Export and check the AdMob jar.
You don't have a LinearLayout in your XML with android:id="linearLayout". This is less likely as the sample project should include this.
Alright, so it turns out the problem is with tm.getDeviceId(), which for whatever reason caused the program to crush.
You need to add the following permission.
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>