psoulos
psoulos

Reputation: 830

Starting intent from xml

I'm trying to have my preferences have an option to go to the system wi-fi settings.

<PreferenceScreen
    android:title="Wifi">
    <intent android:action="android.settings.MAIN"/>
</PreferenceScreen>

This pops up a menu where I can select a bunch of activities, wi-fi settings being one of them. Using logcat, I was able to see

act=android.intent.action.MAIN 
cmp=com.android.settings/.wifi.WifiSettings

How can I call this? The android documentation isn't very clear. I found ACTION_WIFI_SETTINGS in the reference, but I can't figure out how to call it directly from the intent.

Thanks

Edit: I tried android:component, but apparently that doesn't exist

Upvotes: 1

Views: 4710

Answers (2)

inazaruk
inazaruk

Reputation: 74780

Try using ACTION_PICK_WIFI_NETWORK to directly access Wi-Fi settings:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >    
    <Preference 
        android:key="key"   
        android:title="WiFi" 
        android:summary="Calls WiFi">           
            <intent android:action="android.net.wifi.PICK_WIFI_NETWORK"/>           
    </Preference>
</PreferenceScreen>

P.S.

The component is composed of two parts: android:targetPackage and android:targetClass. If you want to know what other xml attributes Intent accepts, then go here: attrs_manifext.xml

    <!-- The package part of the ComponentName to assign to the Intent, as per
        {@link android.content.Intent#setComponent Intent.setComponent()}. -->
    <attr name="targetPackage" />

    <!-- The class part of the ComponentName to assign to the Intent, as per
        {@link android.content.Intent#setComponent Intent.setComponent()}. -->
    <attr name="targetClass" format="string" />

Upvotes: 7

mah
mah

Reputation: 39807

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.wifi.WifiSettings"));
startActivity(intent);

Upvotes: 2

Related Questions