Question

How must I go about coding to get an app to query the state of a device, through internet protocol to see if its on or off.

    public void port0156 (View view) {
    webView = (WebView) findViewById(R.id.webView);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("http://192.168.2.66/index.html?o0=1");
    }
Was it helpful?

Solution

You can register a broadcastreceiver to monitor connectivity changes.

In manifest :

<receiver android:name="com.yourpackage.NetworkChangeReciever" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>

the class in com.yourpackage :

public class NetworkChangeReciever extends BroadcastReceiver {

private final static String TAG = "NetworkChangeReciever";

public interface NetworkChangeRecieverListener {

    public void OnNetworkChangeReciever(boolean wifiConnected);
}


@Override
public void onReceive(final Context context, final Intent intent) {

    final ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);        
    final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

    if (wifi.isAvailable()) || mobile.isAvailable()) {
          Log.d(TAG, "Network Available.");


    }
    else {          
        Log.d(TAG, "Network Unavailable");

    }        
  }
}

OTHER TIPS

You can see if the app is online (or reachable) by seeing if it returns a ping you can easily do this by using OS and parsing the output.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top