在android的Browser中设置User Agent

默认的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="1"
        android:entries="@array/pref_user_agent_options"
        android:entryValues="@array/pref_user_agent_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_user_agent_options">
        <item>Desktop</item>
        <item>Mobile</item>
        <item>Ipad</item>
        <item>Froyo</item>

    </string-array>

    <string-array name="pref_user_agent_values" translatable="false">
        <item>1</item>
        <item>2</item>
        <item>3</item>
        <item>4</item>
    </string-array>

3. 在 src/com/android/BrowserSettings.java 中添加

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_user_agent_options);
                    CharSequence[] enumNames = getResources().getTextArray(
                            R.array.pref_user_agent_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;
    }


你可能感兴趣的:(android,String,user,OS,iPhone,browser)