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);
}
}
Related
Hi there I am trying to execute some code when I change to the next activity, but it does not seem to work. The Previous activity is a login page if the user is already logged in it goes straight to the new activity. But the onCreate does not seem to fire.
Main Activity
public class MainActivity extends AppCompatActivity {
private View mMainView;
private Meteor mMeteor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("ACTIVITY");
Log.d("SimpleActivity","OnCreate Started");
if(MeteorSingleton.getInstance().isConnected()){
Log.d("Connection", "Connected");
}else{
Log.d("Connection", "Not Connected");
}
}
........
The strange thing is the setTitle works but none of the logs.
Here is some code from the previous login in page.
#Override
public void onConnect(boolean signedInAutomatically) {
Log.i("Connection", "Connected to host server");
if (mMeteor.isLoggedIn()) {
openMainScreen(mLoginFormView);
}
}
public void openMainScreen(View view) {
Intent dashboard = new Intent(getApplicationContext(), MainActivity.class);
startActivity(dashboard);
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="system.carproject.adam.ams">
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".LoginActivity"
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=".MainActivity"
android:label="Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="#string/title_activity_maps"></activity>
</application>
</manifest>
If someone could educate me on this would be great. Can seem to figure it out.
Thanks
You have created two LAUNCHER Activities and that's created two App Icon in your Device so if you think its open directly MainActvity then its possible if you click on second app icon in your device for same application . check your device .
first remove LAUNCHER mode from MainActvity in Android Manifest and then you have to add manual check in your login Activity onCreate() for login status and then startActvity() MainActivty if login status is true.
try removing the intent filter tag from activity tag of MainActivity in Manifest:-
<activity android:name=".MainActivity"
android:label="Activity"> </activity>
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'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 an App which was using the old ADMOB SDK. I have started using the Google play services library for showing the ADMOB interestial ad. I have an Ad every time the Activity starts.
ISSUE:
In some Phones I could see the Activity restarts after every close of the AD. Because of this I could see only the Ads. Also I found that the activity reaches onDestroy(). Is there is any way to avod it ?
Kindly help me to resolve this
Android Manifest
<supports-screens
android:xlargeScreens="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true" />
<!-- Used to request banner and interstitial ads. -->
<uses-permission android:name="android.permission.INTERNET"/>
<!-- Used to avoid sending an ad request if there is no connectivity. -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="#drawable/memorygamefreelogo"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<meta-data android:name="com.google.android.gms.games.APP_ID"
android:value="#string/app_id" />
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version"/>
<!-- Activity required to show ad overlays. -->
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
<activity
android:name="com.example.Splash"
android:label="Memory Game"
android:noHistory="true"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.splash.MemoryHome"
android:label="memorygame"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
<intent-filter>
<action android:name="android.intent.action.memorygame.free.MEMORYHOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.splash.MemoryGame"
android:label="memorygame"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
<intent-filter>
<action android:name="android.intent.action.memorygame.free.MEMORYGAME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
When you show an interstitial ad a new (InterstitialAd) Activity is started. This means that your current Activity will no longer be visible and Activity#onStop will be called.
The Android framework may also decide to remove your Activity from memory altogether. If it does so, it may call Activity#onDestroy. This explains the differences you see between some of your phones. I'm not aware of any way to influence this behaviour.
You could modify your onCreate method:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
showAd();
}
}
Like this, the Ad shows only on fresh start.