Android does not allow you to completely disable network system-wide without there being a way of manually enabling it through the system configuration.
You can, indeed turn internet connectivity on or off, but the user will be able to turn it back on through the system settings. To achieve this, you can take a look at this link.
To sum that up, you need to add permissions in the manifest file and use the WifiManager
class.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled())
{
wifiManager.setWifiEnabled(false);
} else {
wifiManager.setWifiEnabled(true);
}
However, if you want to disable internet access in your app, you don't need to even fiddle with the Android Network settings. I assume you are running your App in a WebView
. If that is so, you can wrap your WebView in an if
check.
package com.alfredo.example;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
public class MainActivity extends Activity {
private final WebView myWebView;
private final Button myFormButton;
private boolean hasUserCompletedForm = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webView1);
Button myFormButton = (Button) findViewById(R.id.button1);
myFormButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
hasUserCompletedForm = true;
}
}
if (hasUserCompletedForm) {
myFormButton.setVisibility(View.GONE);
myWebView.loadUrl("http://www.google.com");
myWebView.setVisibility(View.VISIBLE);
} else {
myWebView.setVisibility(View.GONE);
myFormButton.setVisibility(View.VISIBLE);
}
}
}