how to open configure portable Wi-Fi hotspot setting by onclick?

喜欢而已 提交于 2019-12-18 06:57:40

问题


how to open the following directory:settings/wireless and networks/Tethering and portable hotspot/portable Wi-Fi hotspot settings/configure portable Wi-Fi hotspot/ on button click? i want to achieve this using onClick method not id method. below is my code

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

回答1:


This code works for 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);



回答2:


In case any one lands here like me, on Android 8, I used this to open up the setup Hotspot page itself.

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

Works on my device. Let me know if it works on yours. After a bit of research I realized that this class changes per device brand. Eg. For Samsung, use

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

By the way @Drew answer still works on Android 8.




回答3:


use "button" instead of "RadioButton"

in layout try this:

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

in activity.java try this :

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


}
}



回答4:


Activate Tethering in Java:

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

    startActivity(tetherSettings);
}

and in Kotlin:

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

    startActivity(tetherSettings);
}


来源:https://stackoverflow.com/questions/20719905/how-to-open-configure-portable-wi-fi-hotspot-setting-by-onclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!