【问题标题】:how to open configure portable Wi-Fi hotspot setting by onclick?如何通过 onclick 打开配置便携式 Wi-Fi 热点设置?
【发布时间】:2014-01-10 06:38:43
【问题描述】:

如何打开以下目录:设置/无线和网络/网络共享和便携式热点/便携式Wi-Fi热点设置/配置便携式Wi-Fi热点/点击按钮? 我想使用 onClick 方法而不是 id 方法来实现这一点。 下面是我的代码

<RadioButton
        android:onClick="togglewifi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:checked="true"
        android:text="Toggle Wifi" />


public void togglewifi(View view) { 
    Intent intent = new Intent(             );
    startActivity(intent);
}

【问题讨论】:

    标签: android settings onclicklistener buttonclick


    【解决方案1】:

    在 Java 中激活网络共享:

    private void activeTethering(){
        Intent tetherSettings = new Intent();
        tetherSettings.setClassName("com.android.settings", "com.android.settings.TetherSettings");
    
        startActivity(tetherSettings);
    }
    

    在 Kotlin 中:

    private fun activeTethering(){
        val tetherSettings = new Intent().apply {
            setClassName("com.android.settings", "com.android.settings.TetherSettings")
        }
    
        startActivity(tetherSettings);
    }
    

    【讨论】:

      【解决方案2】:

      如果有人像我一样登陆这里,在 Android 8 上,我用它来打开设置热点页面本身。

      public static final String SETTINGS_PACKAGE = "com.android.settings";
      public static final String HOTSPOT_SETTINGS_CLASS = "com.android.settings.Settings$TetherWifiSettingsActivity";
      
      private void launchHotspotSettings(){
          Intent intent = new Intent(Intent.ACTION_MAIN, null);
          ComponentName componentName = new ComponentName(SETTINGS_PACKAGE, HOTSPOT_SETTINGS_CLASS);
          intent.setComponent(componentName);
          intent.addCategory(Intent.CATEGORY_LAUNCHER);
          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          startActivity(intent);
      }
      

      在我的设备上工作。 让我知道它是否适用于你的。 经过一番研究,我意识到这个类会因设备品牌而异。 例如。对于三星,使用

      public static final String HOTSPOT_SETTINGS_CLASS = "com.android.settings.Settings$WifiApSettingsActivity";
      

      顺便说一句,@Drew 的回答仍然适用于 Android 8。

      【讨论】:

      • 嗨,dosky,您是如何获得这些信息的?我有一个在三星上工作的例子,但索尼、华为等也需要它……我如何在这些手机上找到这些自定义活动的“路径”?谢谢!
      • 晚安。我基本上检查了日志。当您检查日志时,每个新页面都会告诉您打开了哪些活动。我可以在信息日志下看到我的,大多数时候它带有标签 ActivityManager 或者你可以搜索 Settingscom.android.settings.Settings,然后尝试导航到您想要的页面。你应该看到这样的东西。 09-12 19:19:39.345 805-858/? I/ActivityManager: Displayed com.android.settings/.Settings$WifiSettingsActivity: +603ms
      • 如果您有问题,请告诉我。注意:设备必须连接到设备并处于调试模式。另外,如果您可以成功编译列表,请帮我提供一个 Gist url。我周围没有太多设备。
      • 嗨,Doski,我已经尝试过你所说的,但我无法完整地看到信息,在三星 S9 上尝试只能看到:I/ActivityManager: Displayed com.android.settings/.SubSettings : +430ms 有没有我必须激活的选项才能更明确地看到这个“子设置”?谢谢!
      • 您是否尝试导航到热点设置活动本身?您需要导航到所需的活动,然后检查记录的内容。
      【解决方案3】:

      此代码适用于 4.2.2

          final Intent intent = new Intent(Intent.ACTION_MAIN, null);
          intent.addCategory(Intent.CATEGORY_LAUNCHER);
          final ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.TetherSettings");
          intent.setComponent(cn);
          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          startActivity( intent);
      

      【讨论】:

      • 我需要进入热点设置。所以我可以更改热点密码
      • 在 7.1.1 上工作,救了我的命
      • 这将打开网络共享屏幕。有没有其他方法可以只打开 WiFi 热点屏幕?我知道这是可能的,因为 SHAREit 做到了。
      • 这对我也有用吗?是否可以打开热点而不是获取热点设置?
      【解决方案4】:

      使用“按钮”而不是“RadioButton”

      在布局中试试这个:

      <Button 
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/button_send"
          android:onClick="togglewifi" />
      

      在activity.java中试试这个:

      public class MainActivity extends Activity {
      
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      }
      
      
      public void togglewifi(View view){
      
          startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
      
      
      }
      }
      

      【讨论】:

      • jeetu,我不想打开wifi设置,我想打开“配置便携式Wi-Fi热点”。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 1970-01-01
      • 2023-01-29
      • 1970-01-01
      • 2014-04-23
      • 1970-01-01
      • 2019-05-26
      相关资源
      最近更新 更多