Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening phone network settings dialog from phonegap app

When my phone (Android) is not connected to the internet and I open the browser app, it pops up a dialog that says:

This application requires network access.
Enable mobile network or Wi-Fi to download data.

It then has two buttons - one for Settings and one for Cancel.

Pressing settings takes me directly to the "Wireless & networks" settings dialog.

I want to display a similar dialog from a phoneGap application. Can this be done?

like image 556
epeleg Avatar asked Apr 22 '12 07:04

epeleg


1 Answers

Diagnostic plugin for PhoneGap may give your answer.

Download the plugin from the following link: Download Link

Than go through the following steps:

  1. Add the diagnostic.js file after the cordova.js in the html header part.

< script type="text/javascript" charset="utf-8" src="cordova-X.X.X.js">< /script >
< script type="text/javascript" charset="utf-8" src="diagnostic.js" > < /script >

  1. Create a directory within your project called src/net/avantic/diagnosticPlugin and move Diagnostic.java into it.

  2. In your res/xml/plugins.xml file add the following line:

< plugin name="Diagnostic" value="net.avantic.diagnosticPlugin.Diagnostic"/>

  1. And in the AndroidManifest.xml add:

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

Than for calling the WIFI settings dialog you need add the following code wherever you want :

 //Check whether Wifi is enable or not...
 window.plugins.diagnostic.isWifiEnabled(wifiEnabledSuccessCallback, wifiEnabledErrorCallback);

 //If function success callback result is 0 it will open the wifi settings dialog box...
 function wifiEnabledSuccessCallback(result) {
      if (!result){
           alert("You must enable the Wi-Fi in device settings.");
           window.plugins.diagnostic.switchToWifiSettings();
        }
      else{
            alert("WiFi is ON!");
          }
     }

  function wifiEnabledErrorCallback(error) {
    console.log(error);
  }

Hope the above solution will work for you..

like image 117
Nemo Avatar answered Jan 12 '23 15:01

Nemo