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

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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

6 Comments

I entered that in the manifest, but it doesn't resolve "settings.wifi.WifiSettings"
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)
OK, which lines do I keep and which do I need to replace? I've never used that before
Almost got it, however it can't resolve "Settings" in startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.