Launching mobile network settings screen programmatically
Asked Answered
D

6

37

I want to launch mobile network settings screen, so that user can enable/disable 3g or data connection. Can anybody tell me which intent I need to use for starting activity. I used

Intent in = new Intent(android.provider.Settings.ACTION_NETWORK_OPERATOR_SETTINGS ) 

and

Intent in = new Intent(android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS  ). 

but both of these didn't work.

Deepfreeze answered 14/5, 2011 at 7:34 Comment(2)
I've tried this myself and couldn't get it to work, the closest thing that I found that you can do is to use this Intent: startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS)); It brings up the overall network settings and from there the user can go to mobile networksHiggledypiggledy
thks @Deepfreeze . your code works for meKine
T
41

They won't work because there was a bug that was fixed I think in 2.3.

See https://review.source.android.com/#/c/22229/

You can bypass this using (for NETWORK_OPERATOR_SETTINGS)

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.android.phone", "com.android.phone.NetworkSetting");
startActivity(intent);

Replace NetworkSetting with Settings for DATA_ROAMING_SETTINGS

there's another similar solution described in Error opening mobile network settings menu

UPDATE

I recently tested this and it seems that this workaround is still necessary up to API level 15. Since API level 16 the intents in the question seem to work correctly.

Tautog answered 22/7, 2011 at 11:54 Comment(10)
Thanks @Zharf. this was what i was looking for. intent.setClassName("com.android.phone", "com.android.phone.Settings");Deepfreeze
@Zharif its open Available Network page, how to open data enable page for internet..?Femininity
isn't there a constatnt for 'com.android.phone' & 'com.android.phone.NetworkSetting' ??Lavonna
@ShirishHerwade I'm not aware of them, but the problem here was that the regular intent action constants didn't work in 2.2 and earlier because of the linked bug. Frankly this shouldn't be something that people should worry about these days because no one should be targeting pre-ICS stuff :)Tautog
so finally can you please tell me what will you recommend to use from 1. new Intent( android.provider.Settings. ACTION_NETWORK_OPERATOR_SETTINGS ) 2. new Intent(Settings. ACTION_DATA_ROAMING_SETTINGS) 3. new Intent(Intent.ACTION_MAIN) I'm totally getting confused by seeing different answers to different question. My min sdk 14Lavonna
@ShirishHerwade you should use number 1 or 2 depending on which view you want to show.Tautog
@ShirishHerwade huh, well I just tested this on my API 15 device actually and it seems that the Settings.ACTION_ constants still aren't working there... I guess you should use ACTION_MAIN then. Since API 16 the Settings.ACTION_ constants seem to work...Tautog
thanks very very much for your time and efforts, its very useful info. You deserve upvoteLavonna
@Tautog can u please specify what permissions do we need for this?Onions
@Onions you shouldn't need any permissions, it's just launching another (system) activityTautog
H
16
public class SettingsScreen
{

protected static void _showSettingScreen(String intentStr)
{
    try
    {
        Intent intent = new Intent(intentStr);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Reference.getAppContext().startActivity(intent);
    }
    catch (Exception e) {Reference.showToast(e.toString(), true);}
}

public static void showSettingScreen()
{
    _showSettingScreen("android.settings.SETTINGS");
}

public static void showAPNScreen()
{
    _showSettingScreen("android.settings.APN_SETTINGS");
}

public static void showLocationScreen()
{
    _showSettingScreen("android.settings.LOCATION_SOURCE_SETTINGS");
}

public static void showSecurityScreen()
{
    _showSettingScreen("android.settings.SECURITY_SETTINGS");
}

public static void showWifiScreen()
{
    _showSettingScreen("android.settings.WIFI_SETTINGS");
}

public static void showBluetoothScreen()
{
    _showSettingScreen("android.settings.BLUETOOTH_SETTINGS");
}

public static void showDateScreen()
{
    _showSettingScreen("android.settings.DATE_SETTINGS");
}

public static void showSoundScreen()
{
    _showSettingScreen("android.settings.SOUND_SETTINGS");
}

public static void showDisplayScreen()
{
    _showSettingScreen("android.settings.DISPLAY_SETTINGS");
}

public static void showApplicationScreen()
{
    _showSettingScreen("android.settings.APPLICATION_SETTINGS");
}

public static void showNetworkSettingScreen()
{
    showDataRoamingScreen();
}

public static void showNetworkOperatorScreen()
{
    if(Reference.getSystemOptions().VERSION_SDK_INT > 15)
    {
        _showSettingScreen("android.settings.NETWORK_OPERATOR_SETTINGS");
    }
    else
    {
        Intent intent=new Intent(android.provider.Settings.ACTION_SETTINGS);
        intent.setClassName("com.android.phone", "com.android.phone.NetworkSetting");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Reference.getAppContext().startActivity(intent);
    }
}

public static void showDataRoamingScreen()
{
    if(Reference.getSystemOptions().VERSION_SDK_INT > 15)
    {
        _showSettingScreen("android.settings.DATA_ROAMING_SETTINGS");
    }
    else
    {
        Intent intent=new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);
        ComponentName cName = new ComponentName("com.android.phone","com.android.phone.Settings");
        intent.setComponent(cName);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Reference.getAppContext().startActivity(intent);
    }
}

public static void showDataMobileScreen()
{
    if(Reference.getSystemOptions().VERSION_SDK_INT > 15)
    {
        Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);//android.provider.Settings.ACTION_SETTINGS //Intent.ACTION_MAIN
        intent.setClassName("com.android.settings", "com.android.settings.Settings$DataUsageSummaryActivity");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Reference.getAppContext().startActivity(intent);
    }
    else
    {
        showDataRoamingScreen();
    }
}

public static void showNotificationScreen()
{
    _showSettingScreen("android.settings.NOTIFICATION_SETTINGS");
}

public static void showBatterySaverScreen()
{
    _showSettingScreen("android.settings.BATTERY_SAVER_SETTINGS");
}

public static void showNfcScreen()
{
    _showSettingScreen("android.settings.NFC_SETTINGS");
}

public static void showInternalStorageScreen()
{
    _showSettingScreen("android.settings.INTERNAL_STORAGE_SETTINGS");
}

public static void showDictionarySettingScreen()
{
    _showSettingScreen("android.settings.USER_DICTIONARY_SETTINGS");
}

public static void showManageApplicationsScreen()
{
    _showSettingScreen("android.settings.MANAGE_APPLICATIONS_SETTINGS");
}

public static void showManageAllApplicationsScreen()
{
    _showSettingScreen("android.settings.MANAGE_ALL_APPLICATIONS_SETTINGS");
}

public static void showMemoryCardScreen()
{
    _showSettingScreen("android.settings.MEMORY_CARD_SETTINGS");
}

public static void showAirPlaneScreen()
{
    if(Reference.getSystemOptions().VERSION_SDK_INT > 16)
    {
        if(Reference.getSystemOptions().BRAND.equalsIgnoreCase("Lenovo"))
        {
            showSettingScreen();
        }
        else
        {
            _showSettingScreen("android.settings.WIRELESS_SETTINGS");
        }
    }
    else
    {
        _showSettingScreen("android.settings.AIRPLANE_MODE_SETTINGS");
    }
}

public static void showWirelessScreen()
{
    _showSettingScreen("android.settings.WIRELESS_SETTINGS");
}

public static void showWifiScreenSafe()
{
    try
    {
        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.wifi.WifiSettings");
        intent.setComponent(cn);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Reference.getAppContext().startActivity(intent);
    }
    catch (Exception e)
        {}
}
}
Hyperextension answered 28/11, 2016 at 12:19 Comment(0)
K
8

I'll try to answer, dispite of the fact this question was asked a few years ago. If what you're trying to do is to launch the "Data usage" screen. Try this snippet of code. It worked for me.

Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setComponent(new ComponentName("com.android.settings",
                "com.android.settings.Settings$DataUsageSummaryActivity"));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
Kidderminster answered 21/8, 2016 at 6:46 Comment(1)
A partial solution since not all devices have this com.android.settings.Settings$DataUsageSummaryActivity class. One should check if exist before firing the intent.Appose
N
8

You can use this for Android API 29, which shows a small UI for toggling Wifi/mobile-data :

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    startActivity(Intent(android.provider.Settings.Panel.ACTION_INTERNET_CONNECTIVITY))
}

enter image description here

https://developer.android.com/reference/android/provider/Settings.Panel#ACTION_INTERNET_CONNECTIVITY

Focusing on Mobile-data settings:

startActivity(Intent(android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS))

Focusing on Wifi settings:

startActivity( Intent(android.provider.Settings.ACTION_WIFI_SETTINGS))
Nordau answered 1/7, 2021 at 12:51 Comment(6)
I've faced problems with many devices specially samsung that doesn't show up these panels.Bitstock
@Bitstock Sadly as with all Intents, you will need to create your own fallbacks using try-catch...Nordau
the problem is that it doesn't throw any exception, it seems as if it processed the intent correctly but nothing is shown in the phone.Bitstock
@Bitstock So have a fallback for this too, using measurements of time since when you've started the Activity and to the time you've reached back to your appNordau
Do you mean like verifying that the screen didnt go to onPause in order to validate if a modal was shown and it wasn't shown open the other?Bitstock
@Bitstock No, but if you do succeed with this, it's also good. What I said is to measure the time and if it's too little, you could assume it's not the user that has exited the new UI, but the OS itself. Sadly it's not the best way of detection, but that's what you have...Nordau
A
5

There are two possibilities:

It brings up the overall network settings and from there the user can go to mobile networks

startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS)); 

As Zharf suggested:

It brings up the Mobile network settings and from there the user can enable network

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.android.phone","com.android.phone.NetworkSetting");
startActivity(intent);
Architect answered 21/1, 2016 at 7:16 Comment(0)
P
4

In my case (android 9) this intent lands directly on mobile data settings:

val intent = Intent(Settings.ACTION_DATA_ROAMING_SETTINGS)
context.startActivity(intent)
Potto answered 11/12, 2019 at 10:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.