如何从我的代码启动移动网络设置屏幕

时间:2022-02-27 20:46:24

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

我想启动移动网络设置屏幕,以便用户可以启用/禁用3g或数据连接。任何人都可以告诉我我需要用于启动活动的意图。我用了

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.

但这两个都行不通。

4 个解决方案

#1


34  

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

他们不会工作,因为我认为在2.3中有一个修复过的错误。

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

You can bypass this using (for NETWORK_OPERATOR_SETTINGS)

您可以使用(对于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

将NetworkSetting替换为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.

我最近对此进行了测试,似乎这个解决方法仍然需要API级别15.自API级别16以来,问题中的意图似乎正常工作。

#2


7  

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);

#3


7  

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)
        {}
}
}

#4


1  

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:

正如Zharf所说:

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);

#1


34  

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

他们不会工作,因为我认为在2.3中有一个修复过的错误。

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

You can bypass this using (for NETWORK_OPERATOR_SETTINGS)

您可以使用(对于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

将NetworkSetting替换为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.

我最近对此进行了测试,似乎这个解决方法仍然需要API级别15.自API级别16以来,问题中的意图似乎正常工作。

#2


7  

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);

#3


7  

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)
        {}
}
}

#4


1  

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:

正如Zharf所说:

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);