-1

I am trying for solution for just on one click we can check tyhe network status with this code

ConnectivityManager ConnectionManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo networkInfo=ConnectionManager.getActiveNetworkInfo();
                if(networkInfo != null && networkInfo.isConnected()==true )
                {
                   // Toast.makeText(SplashScreen.this, "Network Available", Toast.LENGTH_LONG).show();

}

But if network is not available then can we write code (like permission granting on one button click on dialog )for enable mobile data or WiFi with one button click in application pro grammatically

Ashish
  • 6,791
  • 3
  • 26
  • 48
  • Possible duplicate of [Latest update on enabling and disabling mobile data programmatically](https://stackoverflow.com/questions/31120082/latest-update-on-enabling-and-disabling-mobile-data-programmatically) – MadLeo Jan 22 '19 at 11:48
  • 3
    Possible duplicate of [How to enable mobile data or wifi without going to wireless settings page](https://stackoverflow.com/questions/23532500/how-to-enable-mobile-data-or-wifi-without-going-to-wireless-settings-page) – Ashish Jan 22 '19 at 11:53

2 Answers2

0

To Enable/Disable Wifi:

WifiManager wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(status);

You need these permissions:

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

To Enable data see here and here.

Zaki
  • 5,540
  • 7
  • 54
  • 91
0
    public static boolean isNetworkAvailable(Context context) {

            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

            NetworkInfo activeNetwork = cm.getActiveNetworkInfo();

            return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
        }

In OnClick Method run this program

    if(NetworkUtils.isNetworkAvailable(getApplicationContext())
{
                  Toast.makeText(this, "Active Network",Toast.LENGHT_SHORT).show();
        }
       else{
                Toast.makeText(this, "No Active Network", Toast.LENGHT_SHORT).show();
       }
MarcosApps
  • 414
  • 2
  • 4
  • 15