2011-06-15 56 views
1

我想讓我的偏好有一個選項去系統的Wi - Fi設置。從xml開始的意圖

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

這會彈出一個菜單,我可以在其中選擇一組活動,其中之一就是wi-fi設置。使用logcat,我可以看到

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

我該怎麼稱呼它? Android文檔不是很清楚。我在參考中找到了ACTION_WIFI_SETTINGS,但我無法弄清楚如何直接從意圖調用它。

感謝

編輯:我試過安卓成分,但顯然不存在

回答

8

嘗試使用ACTION_PICK_WIFI_NETWORK直接訪問Wi-Fi設置:

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

PS

該組件由兩部分組成:android:targetPackageandroid:targetClass。如果您想了解其他的XML屬性Intent接受,然後去這裏: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" /> 
+0

你是怎麼知道應該做什麼的? android文檔並沒有一個明確的指導來解決它。例如,什麼是ACTION_WIFI_SETTINGS? – psoulos 2011-06-15 15:28:21

+1

是的,它有。請參閱:http://developer.android.com/reference/android/net/wifi/WifiManager.html#ACTION_PICK_WIFI_NETWORK,請注意** Constant value **字段。 – inazaruk 2011-06-15 15:30:19

+0

「ACTION_WIFI_SETTING」相同,請參閱http://developer.android.com/reference/android/provider/Settings.html#ACTION_WIFI_SETTINGS。值爲'android.settings.WIFI_SETTINGS'。順便說一句'ACTION_WIFI_SETTINGS'和'ACTION_PICK_WIFI_NETWORK'是等價的。這沒有很好的記錄,但你會看到你自己(這也可以從android源代碼中看到)。 – inazaruk 2011-06-15 15:33:00

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

這將是推出從代碼的意圖。我正在嘗試從XML中完成。 – psoulos 2011-06-15 14:54:44