How can you get the ads to not crash your ap

by Solomon P Byer   Last Updated September 11, 2019 17:26 PM

I want to keep the app from crashing when the sdk doesn't load right.(which happens a lot) I tried this.

I tried putting all the code in a try catch and ignoring it.

 try{
     int result = ContextCompat.checkSelfPermission(this, 
         Manifest.permission.INTERNET);
     int result2 = ContextCompat.checkSelfPermission(this, 
         Manifest.permission.ACCESS_NETWORK_STATE);
     if(result==PackageManager.PERMISSION_GRANTED&&
        result2==PackageManager.PERMISSION_GRANTED){
            boolean connected = false;
            ConnectivityManager connectivityManager = 
      (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
             if (connectivityManager != null) {
                //we are connected to a network
                connected = 
      connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).
        getState() == NetworkInfo.State.CONNECTED ||    
  connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
  .getState() == NetworkInfo.State.CONNECTED;
            }
       if(connected){
                MobileAds.initialize(this, new 
         OnInitializationCompleteListener() {
                    @Override
                    public void 
         onInitializationComplete(InitializationStatus initializationStatus) {

                    }
                });
                AdView mAdView;
                mAdView=findViewById(R.id.adView);
                AdRequest adRequest=new AdRequest.Builder().build();
                mAdView.loadAd(adRequest);
            }
        }
   }catch (Exception e){
        System.out.println("it didn't work");
    }


Related Questions


Updated August 02, 2018 01:26 AM

Updated February 05, 2019 19:26 PM

Updated November 21, 2017 09:26 AM

Updated August 05, 2018 17:26 PM

Updated February 22, 2017 04:26 AM