-1

I am creating an app, that provides internet access but passed 30 minutes the user should ask some questions to continue with the browsing, The user is connected to my wifi, so I want to stop the browsing or data transfer until the user ask the questions on the app, anyone has any idea how to achieve it?

I already inspected the connectivity in android but I cant see a way to disable data usage, please check the link

alan
  • 48
  • 1
  • 13

1 Answers1

0

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);
        }
    }
}
Marko Vejnovic
  • 45
  • 1
  • 12
  • Im not using web view, because I want the user uses his phone normally, according the information you provide, there is no way to disable the browing, so I think the proccess of stop sendind data must be on the server that has network access to control the data by the mac address of the phone – Alfredo uarez romero Jul 26 '19 at 15:33
  • @Alfredouarezromero, I'm a little confused by what you mean. Do you want the user to not be able to have internet access system-wide or just in your app? – Marko Vejnovic Jul 26 '19 at 16:12
  • System wide, but i want to control the data usage un a service from the app, with a timer – Alfredo uarez romero Jul 26 '19 at 21:30
  • You could set up a service which will continually check if the WiFi or Mobile Data is on and then turn it off. It's a hack, and I really advise against it as it creates an absolutely terrible user experience. – Marko Vejnovic Jul 27 '19 at 14:38