Interstitial ad not being loaded? - java

I just created a Interstitial add in my test app. Went fairly easy, but now I want to add one to my main application and I cannot seem to load it.
This is what my android launcher looks like:
../
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new GameOfClaws(this), config);
//initialize ads...
interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId(getString(R.string.interstitial_ad));
//Load add
requestNewInterstitial();
interstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
super.onAdClosed();
requestNewInterstitial();
}
#Override
public void onAdLoaded()
{
Gdx.app.log(TAG, "Ad loaded!");
//interstitialAd.show();
}
});
}
private void requestNewInterstitial()
{
AdRequest adRequest = new AdRequest.Builder()
.build();
interstitialAd.loadAd(adRequest);
}
#Override
public void displayInterstitialAd() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if (interstitialAd.isLoaded()) {
interstitialAd.show();
}
else
{
Gdx.app.log("MainActivity", "Add not loaded!");
}
}
});
}
/..
I simply call a interface method in the show method of my menuScreen to run the same method in the AndroidLauncher.
resolver.displayInterstitialAd();
It's basically the same as my working test app but there I load the interstitial by pressing a button.
This is my manifest:
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/GdxTheme" >
<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
android:name="com.madmenyo.gameofclaws.android.AndroidLauncher"
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>
</application>
The only things that are different is that my test app is in portrait mode and it has been uploaded a while ago but remains as a testing app and not published. My current app has just been uploaded for testing these ads and the play store. The play store works fine. I also changed the ad ID to the correct ID I got on the Admob website.

Add the following in manifest file
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Translucent" >
</activity>

Related

How to enable back button in webview?

I am creating an application with webview (an application in which a website is displayed).
Currently, if I click the back button, the app closes the application
What I am looking for is not that a button with an arrow appears in the action bar and it takes me back, because I have eliminated that bar, what I want is to be able to use the buttons on the cell phone.
I hope you can help me, here the code of the MainActivity.java and the AndroidManifiest.xml
MainActivity.java
public class MainActivity extends AppCompatActivity {
private View decorView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = findViewById(R.id.mywebview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new myChrome());
webView.loadUrl("https://mywebview.com/");
decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
#Override
public void onSystemUiVisibilityChange(int visibility) {
if (visibility == 0)
decorView.setSystemUiVisibility(hideSystemBars());
}
});
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
decorView.setSystemUiVisibility(hideSystemBars());
}
}
AndroidManifiest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Dessert.decov">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.DESSERT">
<activity
android:name=".MainActivity"
android:exported="true"
android:configChanges="screenSize|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Android - can't get broadcastreceiver to work

I try to launch a service at boot, but it never starts the service. I added <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/> to the intent-filter of the receiver, but when I connect my android 8.0 phone to power, it also doesn't work.
manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.madmagic.oqrpc">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:usesCleartextTraffic="true"
android:allowBackup="true"
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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MainService" />
<receiver
android:name=".StartAtBoot">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
</intent-filter>
</receiver>
</application>
</manifest>
StartAtBoot:
public class StartAtBoot extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "received", Toast.LENGTH_LONG).show(); //this never shows up when connecting to power
Intent i = new Intent(context, MainService.class);
context.startService(i);
}
}
MainService:
public class MainService extends Service {
public static boolean isRunning = false;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
isRunning = true;
Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show();
ConnectionChecker.run(this); //when device has wifi connection, this will run connected() method
}
#Override
public void onDestroy() {
isRunning = false;
}
public void connected() {
//things to do when it has wifi connection
}
}
In my MainActivity class, I start this service using the same way as in my StartAtBoot class, and there it works fine when I open the application. So the service is working fine, its just that the StartAtBoot class doesn't run the code.
ACTION_POWER_CONNECTED is not a listed exception to the limits on implicit Intent broadcasts. Your app cannot register for it in the manifest.
If your goal is to do work periodically, but only if the device has power, use JobScheduler or WorkManager.

What forces android activity to be destroyed when launching another activity

I'm trying to add in-app purchases and when I call OpenIabHelper::launchPurchaseFlow function:
I see Google Play purchase window
onStop and onDestroy are executed in my main activity without calls to onActivityResult
So in the end I left with GooglePlay window and my dead app
I tried to start another activity via startActivityForResult and it worked fine
(nothing was destroyed and onActivityResult was called), so it seems that problem is with OpenIAB library
Therefore there's my question:
What can possibly force my activity to be destroyed after calling OpenIabHelper::launchPurchaseFlow ?
It doesn't seem to be system because it has enough memory and as I said startActivityForResult works fine.
Also there's no exceptions being trown so I'm puzzled
Any help would be appreciated
MyActivity.java
public class MyActivity extends AppCompatActivity {
private OpenIabHelper m_helper;
private Boolean m_bOpenIabInitialized = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OpenIabHelper.Options.Builder builder = new OpenIabHelper.Options.Builder()
.addAvailableStores(new GooglePlay(this,null))
.addPreferredStoreName(OpenIabHelper.NAME_GOOGLE)
.setStoreSearchStrategy(OpenIabHelper.Options.SEARCH_STRATEGY_INSTALLER_THEN_BEST_FIT)
.setVerifyMode(OpenIabHelper.Options.VERIFY_SKIP)
.setCheckInventory(false);
m_helper = new OpenIabHelper(this, builder.build());
m_helper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
m_bOpenIabInitialized = result.isSuccess();
}
});
}
public void onListViewItemClicked(String sku, String payload) {
if (m_bOpenIabInitialized == null || !m_bOpenIabInitialized) return;
m_helper.launchPurchaseFlow(this, sku, 10001, null, payload);
}
#Override
public void startActivityForResult(Intent intent, int RequestCode) {
// never reaches here
Log.d(TAG, "startActivityForResult()");
}
#Override
protected void onActivityResult(int RequestCode, int ResultCode, Intent data) {
// never reaches here
Log.d(TAG, "onActivityResult()");
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example" >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="com.android.vending.BILLING" />
<application
android:allowBackup="true"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity
android:name=".MyActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"
android:configChanges="orientation|screenSize|keyboardHidden|screenLayout">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Have you tried changing the activity's launchMode? Maybe the singleTop launch mode will preserve your activity:
<activity
android:name=".MyActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"
android:configChanges="orientation|screenSize|keyboardHidden|screenLayout"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You can find more about launch modes here:
https://developer.android.com/guide/topics/manifest/activity-element.html#lmode

BroadCastReceiver doesn't work when return button

I have a BroadCastReceiver and since I don't want it to keep working when the application is uninstalled so I removed the declaration of the receiver from the manifest . By doing this the receiver will only work when the application is running . That's good so far but I have a little problem which is if the user clicks "home" button on his device to get back to his device's main screen the receiver works in the background , but when he clicks "back" button the receiver will stop working . I want it to work either the user clicked home button or back button. So how to solve this problem and thanks in advance.
**Note : I'm activating/deactivating the receiver using this code :
private BroadCastReceiver myBroadcastReceiver = new BroadCastReceiver();
#Override
public void onStart() {
super.onStart();
registerReceiver(myBroadcastReceiver, new IntentFilter(Telephony.Sms.Intents.SMS_RECEIVED_ACTION));
}
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(myBroadcastReceiver);
}
&& my manifest is :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.test"
android:versionCode="9"
android:versionName="8.1" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:allowBackup="true"
android:icon="#drawable/danger"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
<activity
android:name="com.aaf.nuclearalert.MainActivity"
android:label="#string/app_name"
android:screenOrientation="landscape"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
*Update 1 :
public void onBackPressed() {
registerReceiver(myBroadcastReceiver, new IntentFilter(Telephony.Sms.Intents.SMS_RECEIVED_ACTION));
}

Ad isn't loading

I followed the google tutorial on interstitial ads, but whenever I try to display my ad it never is loaded. I've tried to use AdListener instead of interstitial.isLoaded, but still the same result. I've also given the ad over 2 minutes to load a couple times, so something must be wrong.
I am using an AVD, could that be the problem? It can connect to the Internet just fine when I use the browser though.
removed an incorrect link
androidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.JrodManU.LaserJumper.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
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="#string/app_name"
android:theme="#style/GdxTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".AndroidLauncher"
android:clearTaskOnLaunch="true"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
AndroidLauncher
package com.JrodManU.LaserJumper.android;
import android.os.Bundle;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.android.gms.ads.*;
import com.JrodManU.LaserJumper.GameEventListener;
import com.JrodManU.LaserJumper.LaserJumper;
public class AndroidLauncher extends AndroidApplication implements GameEventListener {
private InterstitialAd interstitial;
boolean showed = false;
boolean loaded = false;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new LaserJumper(this), config);
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("******");
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
//.addTestDevice(testDeviceId)
.build();
interstitial.loadAd(adRequest);
interstitial.setAdListener(new AdListener(){
public void onAdLoaded(){
loaded = true;
}
});
}
public boolean displayInterstitial() {
this.runOnUiThread(new Runnable() {
#Override
public void run() {
if(loaded) {
interstitial.show();
showed = true;
} else {
System.out.println("failed"); //Always prints failed
showed = false;
}
}
});
return showed;
}
}
If you got this error:
could not find com.google.android.gms.ads.AdActivity, please make sure
it is declared in AndroidManifest.xml
Then adding this activity declaration within the <application> tag in your Manifest should fix it:
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

Categories