0

I am developing an Android Application and I need to make sure that the user is connected to the internet somehow. I can already check for WiFi, however, not everyone will be near a WiFi zone all the time, so I am thinking that Mobile Data is a valid alternative (of course assuming that the device is capable of having a SIM and all). So far, I can check if the user has enabled his or her mobile data as follows:

if(checkForMobileNetworkActive()){
    //with mobile active
}
else{ // mobile not active
}

What I want to do is this: If the mobile network is NOT active, I will ask the user to turn it on. Very similar to what I have done in the past where I prompt the user to turn on their Bluetooth or the Location services. However, upon searching online, most answers pointed me to opening the WiFi settings via intent as such:

Intent i = new Intent(Settings.ACTION_WIFI_SETTINGS);
startActivity(i);

I have 2 issues with this solution:

  1. This opens a new activity and not an alert dialog-style prompt to turn the WiFi on.
  2. This turns on the WiFi and not the Mobile Data.

Has anyone tried to prompt the user to turn on their Mobile Data? As much as possible, I want it to look like the dialog box prompt and not an entire new activity (which is bad user experience in my opinion). I've looked on how to do it programmatically, however, what I stumbled upon no longer works for non-rooted device on Android Lollipop+ and I don't want to run the risk of the app no longer running when users upgrade the OS of their Android Devices.

Edit

I saw the link posted in the comment below, and I've tried this:

if(checkForMobileNetworkActive()){
}
else{
    Intent i = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);
    startActivity(i);
}

But it opens the Mobile Data/Data Roaming Settings as a new activity. While it works, it enables the user to check the checkbox for Mobile Data and pressing the back button goes back to my app, it is not a very smooth user experience.

halfer
  • 19,824
  • 17
  • 99
  • 186
Razgriz
  • 7,179
  • 17
  • 78
  • 150
  • Possible duplicate of [How can I launch mobile network settings screen from my code](http://stackoverflow.com/questions/6000452/how-can-i-launch-mobile-network-settings-screen-from-my-code) – n0idea Apr 27 '16 at 12:34
  • @bearzed as much as possible, I want a dialog style pop up and not a whole new activity. Also, the solution in the question you linked opens "Available Networks" and not the Mobile Data page. – Razgriz Apr 27 '16 at 12:56
  • Actually, I saw one of the answers that used `ACTION_DATA_ROAMING_SETTINGS` instead and it can help, but it opens a new activity instead of a small dialog type alert. – Razgriz Apr 27 '16 at 13:04

1 Answers1

0

First of all, you have to use this permission:

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

After that, with this code, you can know whether it connected to internet by mobile data or not:

public static boolean isConnectedMobile(Context context){
    NetworkInfo info = Connectivity.getNetworkInfo(context);
    return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
}

UPDATE 1: If you want to enable/disable the mobile network in your app, you can use this solution:

private void enableMobileData(Context context, boolean enabled) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
    final ConnectivityManager cm = (ConnectivityManager)  context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final Class conmanClass = Class.forName(cm.getClass().getName());
    final Field connectivityManagerField = conmanClass.getDeclaredField("mService");
    connectivityManagerField.setAccessible(true);
    final Object connectivityManager = connectivityManagerField.get(cm);
    final Class connectivityManagerClass =  Class.forName(connectivityManager.getClass().getName());
    final Method setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
    setMobileDataEnabledMethod.setAccessible(true);

    setMobileDataEnabledMethod.invoke(connectivityManager, enabled);
}

And don't forget to use this permission:

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
Mohammad Zarei
  • 1,773
  • 14
  • 33
  • Did you read the question? I want to prompt the user to turn on 3G/Mobile Data and NOT check if he or she is connected to the internet through mobile data or not (which I can already do as stated in my question) – Razgriz Apr 27 '16 at 12:59
  • sorry for misunderstanding, so you want to enable mobile data through your application? – Mohammad Zarei Apr 30 '16 at 06:48
  • Yes. I had to stick with the existing solution of opening the settings page for the mobile data as a new activity via intent – Razgriz Apr 30 '16 at 11:34
  • I just updated my answer, I think it will solve your problem. – Mohammad Zarei May 01 '16 at 07:27