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.
Related
I am new to Android Development and I am developing an application to scan barcode using google vision API. In the application, I have three screens Splash Screen, Main Screen with two fragments (Scan & History) and Scanned Result. From the scan fragment, I am opening an activity called "ScannedResult" as soon as application scanned the barcode to show the result.
Now the problem is when I run the application from the Android Studio to the device.
[Scenario# 0] (From the Main activity screen app stopping that is the correct case)
Run the app
Splash Screen -> Main Screen
Shows scan fragment and run successful flow
Now, STOP the application from the Android Studio.
Application stopped
[Scenario# 1] (But from ScannedResult activity app stops and start again why?)
Run the app
Splash Screen -> Main Screen
Shows scan fragment and run successful flow
Now, Scan a Bar or QR Code then open ScannedResult (activity).
Now, STOP the application from the Android Studio.
Application stopped and show Black Screen
Application STARTED again and shows the Main Screen (Why it is starting again? I want to stop the application in this case).
Here I am suspecting something is wrong. Ideally, it must be stopped. Please help to identify where I am making mistake in the application.
Here is the Manifest.xml file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapp.powertools.barcodescan">
<application
android:allowBackup="false"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".CreatePaypalUrl"
android:parentActivityName=".MainActivity"></activity>
<activity
android:name=".CreateVcardForm"
android:parentActivityName=".MainActivity" />
<activity
android:name=".CreateLocationForm"
android:parentActivityName=".MainActivity" />
<activity
android:name=".CreateWifiForm"
android:parentActivityName=".MainActivity" />
<activity
android:name=".CreateEventForm"
android:parentActivityName=".MainActivity" />
<activity
android:name=".CreateSmsForm"
android:parentActivityName=".MainActivity" />
<activity
android:name=".CreateEmailForm"
android:parentActivityName=".MainActivity" />
<activity
android:name=".CreateTelephoneForm"
android:parentActivityName=".MainActivity" />
<activity
android:name=".CreateWebsiteForm"
android:parentActivityName=".MainActivity" />
<activity android:name=".Main2Activity" />
<activity
android:name=".GenerateQrCode"
android:parentActivityName=".MainActivity" />
<activity
android:name=".CreateTextForm"
android:parentActivityName=".MainActivity" />
<activity
android:name=".ScannedResult"
android:parentActivityName=".MainActivity" />
<activity
android:name=".SplashScreen"
android:label="#string/app_name" android:theme="#style/AppTheme.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:theme="#style/AppTheme.NoActionBar" />
<meta-data
android:name="com.google.android.gms.vision.DEPENDENCIES"
android:value="barcode" />
</application>
<uses-feature android:name="android.hardware.camera.front" android:required="false" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
</manifest>
Scan.java file
barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
#Override
public void release() {
}
#Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> qrCodes = detections.getDetectedItems();
if (qrCodes.size() > 0) {
if (isScanned == false) {
barcodeDetector.release();
isScanned = true;
String dataType = getBarcodeType(qrCodes.valueAt(0));
String dataValue = qrCodes.valueAt(0).displayValue;
String rawValue = qrCodes.valueAt(0).rawValue;
Intent intentScannedResult = new Intent(getActivity(), ScannedResult.class);
intentScannedResult.putExtra("barcode_raw_value", rawValue);
intentScannedResult.putExtra("barcode_value", dataValue);
intentScannedResult.putExtra("barcode_type", dataType);
intentScannedResult.putExtra("is_scanner", "1");
startActivity(intentScannedResult);
}
}
}
});
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 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.
i am developing an android app using phonegap. i have multiple pages in the app and a single activity. whenever i change the orientation of the device, no matter which page i am on, the app loads the first page which is shown when the app is launched. i assumed it was because i had not overriden the onResume() method in the activity, but even that failed to solve the problem.
EDIT: This issue does not arise if the app is paused and the resumed.
My activity:
public class MainScreen extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/landing.html");
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
}
EDIT: Added Manifest file
My Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.me.code"
android:versionCode="5"
android:versionName="1.4" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.miniorange.authenticator.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.miniorange.authenticator.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.miniorange.authenticator.MainActivity"
android:label="#string/app_name" >
</activity>
<activity android:name="com.miniorange.authenticator.MainScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.plugin.gcm.PushHandlerActivity"/>
<receiver android:name="com.plugin.gcm.CordovaGCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.me.code" />
</intent-filter>
</receiver>
<service android:name="com.plugin.gcm.GCMIntentService" />
</application>
</manifest>
what may be the solution to this problem?
sorry ... ( answering it late :D )
I had this problem with my app :
When I change the orientation, activity reloads ...
And I found this solution :
add this attribute to your <activity> tags :
android:configChanges="orientation"
suggestion ... use this insted (better) :
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
==========================================
information about solution :
what is the reason .. (?)
It'll say to application that you want to handle this changes (orientation, keyboardHidden, keyboard, screenSize, locale, etc.) yourself.