5

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);
}
Anonymous
  • 1,389
  • 3
  • 11
  • 16

6 Answers6

15

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);
Drew
  • 1,014
  • 2
  • 10
  • 21
6

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.

dozieogbo
  • 473
  • 1
  • 6
  • 13
  • Hi dosky, how did you get that information? I have an example working on Samsung but i need it also for Sony, Huawei, etc... How do i find the "path" of those custom activities on those phones? Thanks! – rul3s Sep 12 '18 at 15:54
  • 1
    Goodday. I basically checked the logs. When you inspect the logs, every new page tells you what activity was opened. I can see mine under Info logs and most times it comes with the tag **ActivityManager** or you could just search for **Settings** or **com.android.settings.Settings**, then try navigating to the page you want. You should see stuff like this. `09-12 19:19:39.345 805-858/? I/ActivityManager: Displayed com.android.settings/.Settings$WifiSettingsActivity: +603ms` – dozieogbo Sep 12 '18 at 18:23
  • Let me know if you have issues. NB: The device has to be connected to the device and in debug mode. Also, if you can compile the list successfully, please help me with a Gist url for it. I don't have much devices around me. – dozieogbo Sep 12 '18 at 18:34
  • Hi Doski, I've tried what you say but I cannot see the information completly, trying on a Samsung S9 y can only see this: I/ActivityManager: Displayed com.android.settings/.SubSettings: +430ms Is there any option that I have to activate to see this "Subsettings" more explicit? Thanks! – rul3s Sep 14 '18 at 06:48
  • Did you try navigating to the hotspot setup activity itself? You need to navigate to the activity you want, then check what was logged. – dozieogbo Sep 14 '18 at 08:19
  • Yes, exactly this is what I did. From homescreen, go to settings, etc.... all this connected via usb, usb debug enabled and logging via android studio. Am i missing something? – rul3s Sep 14 '18 at 13:45
  • Hello. Sorry for the silence. I will check another device and get back to you. – dozieogbo Sep 20 '18 at 16:25
  • This crashes for me. – Aura Lee Oct 12 '21 at 10:05
1

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);
}
Boken
  • 4,825
  • 10
  • 32
  • 42
WoC
  • 11
  • 1
0

This code will work for you all your devices including SAMSUNGS.

   Intent intent=new Intent(Intent.ACTION_MAIN,null);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            final ComponentName componentName;
            componentName    =new ComponentName("com.android.settings","com.android.settings.TetherSettings");
            intent.setComponent(componentName);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
Sam
  • 241
  • 3
  • 7
0

Java:

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

Kotlin:

    val intent = Intent(Intent.ACTION_MAIN, null)
    intent.addCategory(Intent.CATEGORY_LAUNCHER)
    val cn = ComponentName("com.android.settings", "com.android.settings.TetherSettings")
    intent.component = cn
    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    startActivity(intent)
Rehan Khan
  • 1,031
  • 13
  • 10
-2

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


}
}
Jitesh S
  • 57
  • 1
  • 3