默认的Android的Browser中无法设置User Agent,但是在Browser的源码中可以看到,android提供了4中User Agent 具体代码如下:
在Browser的 src/com/android/browser/BrowserSettings.java private static final String DESKTOP_USERAGENT = "Mozilla/5.0 (Macintosh; " + "U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, " + "like Gecko) Version/5.0 Safari/533.16"; private static final String IPHONE_USERAGENT = "Mozilla/5.0 (iPhone; U; " + "CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 " + "(KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7"; private static final String IPAD_USERAGENT = "Mozilla/5.0 (iPad; U; " + "CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 " + "(KHTML, like Gecko) Version/4.0.4 Mobile/7B367 Safari/531.21.10"; private static final String FROYO_USERAGENT = "Mozilla/5.0 (Linux; U; " + "Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 " + "(KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"; 提供了4中不同的User Agent,但是在浏览器中无法进行设置。 在Browser的setting中添加设置User Agent的选项: 1.在 res/xml/browser_preferences.xml中添加: <ListPreference android:key="user_agent" android:title="@string/pref_user_agent" android:defaultValue="0" android:entries="@array/pref_development_ua_choices" android:entryValues="@array/pref_development_ua_values" android:dialogTitle="@string/pref_user_agent_dialogtitle" /> 2.在res/value/string.xml中添加对应的字符串,多语言的具体到不同的文件夹(自己添加) <string name="pref_user_agent">User agent</string> <string name="pref_user_agent_dialogtitle" translatable="false">User Agent</string> <string-array name="pref_development_ua_choices" translatable="false"> <item>Android</item> <item>Desktop</item> <item>iPhone</item> <item>iPad</item> <item>Froyo-N1</item> <item>NexusOne</item> <item>Samsung i9000</item> </string-array> <!-- Do not tranlsate. Development option --> <string-array name="pref_development_ua_values" translatable="false"> <item>0</item> <item>1</item> <item>2</item> <item>3</item> <item>4</item> <item>5</item> <item>6</item> </string-array>3. 在 src/com/android/BrowserSettings.java 中添加 view plainprint? public final static String PREF_USER_AGENT = "user_agent"; 4.在 src/com/android/BroserPreferencesPage.java 中添加 private CharSequence getVisualUserAgent(String enumName) { CharSequence result = ""; CharSequence[] visualNames = getResources().getTextArray( R.array.pref_development_ua_choices); CharSequence[] enumNames = getResources().getTextArray(R.array.pref_development_ua_values); if (visualNames.length == enumNames.length) { for (int i = 0; i < enumNames.length; i++) { if (enumNames[i].equals(enumName)) { result = visualNames[i]; } } } return result; } 5. 在BroserPreferencesPage.java的OnCreate方法中添加 e = findPreference(BrowserSettings.PREF_USER_AGENT); e.setOnPreferenceChangeListener(this); e.setSummary(getVisualUserAgent( getPreferenceScreen().getSharedPreferences() .getString(BrowserSettings.PREF_USER_AGENT, null))); 6.在BroserPreferencesPage.java的onPreferenceChange 方法中添加 else if (pref.getKey().equals(BrowserSettings.PREF_USER_AGENT)){ pref.setSummary(getVisualUserAgent((String)objValue)); return true; }
在Browser的 src/com/android/browser/BrowserSettings.java
提供了4中不同的User Agent,但是在浏览器中无法进行设置。 在Browser的setting中添加设置User Agent的选项: 1.在 res/xml/browser_preferences.xml中添加: <ListPreference android:key="user_agent" android:title="@string/pref_user_agent" android:defaultValue="0" android:entries="@array/pref_development_ua_choices" android:entryValues="@array/pref_development_ua_values" android:dialogTitle="@string/pref_user_agent_dialogtitle" /> 2.在res/value/string.xml中添加对应的字符串,多语言的具体到不同的文件夹(自己添加)
提供了4中不同的User Agent,但是在浏览器中无法进行设置。
在Browser的setting中添加设置User Agent的选项:
1.在 res/xml/browser_preferences.xml中添加:
<string name="pref_user_agent">User agent</string> <string name="pref_user_agent_dialogtitle" translatable="false">User Agent</string> <string-array name="pref_development_ua_choices" translatable="false"> <item>Android</item> <item>Desktop</item> <item>iPhone</item> <item>iPad</item> <item>Froyo-N1</item> <item>NexusOne</item> <item>Samsung i9000</item> </string-array> <!-- Do not tranlsate. Development option --> <string-array name="pref_development_ua_values" translatable="false"> <item>0</item> <item>1</item> <item>2</item> <item>3</item> <item>4</item> <item>5</item> <item>6</item> </string-array>3. 在 src/com/android/BrowserSettings.java 中添加 view plainprint? public final static String PREF_USER_AGENT = "user_agent"; 4.在 src/com/android/BroserPreferencesPage.java 中添加 private CharSequence getVisualUserAgent(String enumName) { CharSequence result = ""; CharSequence[] visualNames = getResources().getTextArray( R.array.pref_development_ua_choices); CharSequence[] enumNames = getResources().getTextArray(R.array.pref_development_ua_values); if (visualNames.length == enumNames.length) { for (int i = 0; i < enumNames.length; i++) { if (enumNames[i].equals(enumName)) { result = visualNames[i]; } } } return result; } 5. 在BroserPreferencesPage.java的OnCreate方法中添加 e = findPreference(BrowserSettings.PREF_USER_AGENT); e.setOnPreferenceChangeListener(this); e.setSummary(getVisualUserAgent( getPreferenceScreen().getSharedPreferences() .getString(BrowserSettings.PREF_USER_AGENT, null))); 6.在BroserPreferencesPage.java的onPreferenceChange 方法中添加 else if (pref.getKey().equals(BrowserSettings.PREF_USER_AGENT)){ pref.setSummary(getVisualUserAgent((String)objValue)); return true; }
Powered by: BlogJava Copyright © 天宇恒星