4

I'm trying to open open the wifi setting programmatically in an andoird app. It works on most devices, but on an android tablet it crashes and gives me this error:

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.android.settings/com.android.settings.wifi.WifiSettings}; have you declared this activity in your AndroidManifest.xml?

Here is my code in the main activity:

Button wifisettings = (Button) findViewById(R.id.WiFiSettings);
    wifisettings.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            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.wifi.WifiSettings");
            intent.setComponent(cn);
            intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    });
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
A P
  • 171
  • 1
  • 7

2 Answers2

2

If you want to call WiFiSettings from your app use this:

startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));

Look into this https://developer.android.com/reference/android/provider/Settings for further sttings and how to take the user there

Jav T
  • 478
  • 3
  • 12
0

Try adding this in your AndroidManifest.xml:

<activity
    android:name="com.android.settings.wifi.WifiSettings"/>

Same issue reported by other users in comments.

Update: If it didn't work, use this line which is simpler to use:

Button wifisettings = (Button) findViewById(R.id.WiFiSettings);
wifisettings.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
        }
    });
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
  • I entered that in the manifest, but it doesn't resolve "settings.wifi.WifiSettings" – A P Oct 14 '18 at 18:29
  • If the issue persist, I prefer to use : `startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));` instead of those few lines of codes. (Check the other user answer) – ʍѳђઽ૯ท Oct 14 '18 at 18:44
  • OK, which lines do I keep and which do I need to replace? I've never used that before – A P Oct 14 '18 at 18:49
  • Almost got it, however it can't resolve "Settings" in startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS)); – A P Oct 14 '18 at 18:54
  • You'll need to import it. The import will be : `import android.provider.Settings;` Android Studio will suggest to import it. Do a `clean-project` from `Build` if it does not suggest. – ʍѳђઽ૯ท Oct 14 '18 at 18:57
  • Could you solve the issue? Did it worked with the import? – ʍѳђઽ૯ท Oct 15 '18 at 08:07