skip to Main Content

what can I do to make this function not work and give a warning when the device is offline? I would appreciate your help.

Code:

if (adType == Sdkmanager.AD_REWARDED)
    Website2APK.showInterstitialAd();
{

2

Answers


  1. you can add a condition to check the network connectivity

    Login or Signup to reply.
  2. First, ensure your app has permission to access the network state by adding the following line to your AndroidManifest.xml:

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    

    Next, modify your code to check for internet connectivity before attempting to show the ad:

    import android.content.Context;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.widget.Toast;
    
    public class AdHandler {
    
        private Context context;
    
        public AdHandler(Context context) {
            this.context = context;
        }
    
        private boolean isOnline() {
            ConnectivityManager cm = 
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (cm != null) {
                NetworkInfo netInfo = cm.getActiveNetworkInfo();
                return (netInfo != null && netInfo.isConnected());
            }
            return false;
        }
    
        public void showAd(int adType) {
            if (adType == Sdkmanager.AD_REWARDED) {
                if (isOnline()) {
                    Website2APK.showInterstitialAd();
                } else {
                    // No internet connection
                    // ...
                }
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search