【问题标题】:Launching mobile network settings screen programmatically以编程方式启动移动网络设置屏幕
【发布时间】:2011-08-25 10:05:13
【问题描述】:

我想启动移动网络设置屏幕,以便用户可以启用/禁用 3g 或数据连接。谁能告诉我我需要使用哪个意图来启动活动。 我用过

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

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

但是这两个都没有用。

【问题讨论】:

  • 我自己试过了,但无法让它工作,我发现你可以做的最接近的事情就是使用这个 Intent:startActivity(new Intent(android.provider.Settings. ACTION_WIRELESS_SETTINGS));它会调出整体网络设置,用户可以从那里转到移动网络
  • 感谢@sandeep。你的代码对我有用

标签: android android-settings


【解决方案1】:

它们不会起作用,因为我认为在 2.3 中修复了一个错误。

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

您可以使用 (for NETWORK_OPERATOR_SETTINGS) 绕过这个

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

NetworkSetting 替换为Settings 以获得DATA_ROAMING_SETTINGS

Error opening mobile network settings menu中描述了另一个类似的解决方案

更新

我最近对此进行了测试,似乎直到 API 级别 15 仍需要此解决方法。由于 API 级别 16,问题中的意图似乎可以正常工作。

【讨论】:

  • 谢谢@Zharf。这就是我要找的。 intent.setClassName("com.android.phone", "com.android.phone.Settings");
  • @Zharif 其打开的可用网络页面,如何打开互联网数据启用页面..?
  • 'com.android.phone' & 'com.android.phone.NetworkSetting' 没有常量吗??
  • @ShirishHerwade 我不知道它们,但这里的问题是由于链接错误,常规意图操作常量在 2.2 及更早版本中不起作用。坦率地说,这不应该是人们现在应该担心的事情,因为没有人应该针对 ICS 之前的东西 :)
  • @ShirishHerwade 呵呵,好吧,我实际上只是在我的 API 15 设备上测试了这个,似乎 Settings.ACTION_ 常量在那里仍然不起作用......我想你应该使用 ACTION_MAIN 。从 API 16 开始,Settings.ACTION_ 常量似乎可以工作......
【解决方案2】:
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)
        {}
}
}

【讨论】:

    【解决方案3】:

    我会尽力回答,尽管这个问题是几年前提出的。 如果您要做的是启动“数据使用”屏幕。 试试这个 sn-p 的代码。它对我有用。

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

    【讨论】:

    • 部分解决方案,因为并非所有设备都有此 com.android.settings.Settings$DataUsageSummaryActivity 类。应该在触发意图之前检查是否存在。
    【解决方案4】:

    有两种可能:

    它会调出整体网络设置,用户可以从那里转到移动网络

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

    正如Zharf 建议的那样:

    它会调出移动网络设置,用户可以从那里启用网络

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

    【讨论】:

      【解决方案5】:

      在我的情况下(android 9),这个意图直接落在移动数据设置上:

      val intent = Intent(Settings.ACTION_DATA_ROAMING_SETTINGS)
      context.startActivity(intent)
      

      【讨论】:

        【解决方案6】:

        您可以将它用于 Android API 29,它显示了一个用于切换 Wifi/mobile-data 的小 UI:

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

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

        专注于移动数据设置:

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

        专注于 Wifi 设置:

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

        【讨论】:

          猜你喜欢
          • 2014-01-09
          • 2013-06-11
          • 2011-03-22
          • 2022-11-13
          • 2020-04-29
          • 1970-01-01
          • 2010-09-05
          • 2010-10-16
          • 1970-01-01
          相关资源
          最近更新 更多