【问题标题】:How to launch browser when a preference is selected选择首选项时如何启动浏览器
【发布时间】:2010-12-11 20:12:34
【问题描述】:

我正在创建首选项菜单,并希望在单击特定首选项时启动浏览器(带有特定 URL)。我知道这可以做到,但我现在似乎无法让它工作。

有什么想法吗?

谢谢

######解决方案

所以在我的脑屁消失后,我就是这样做的:

getPreferenceManager()
   .findPreference("my_preference_key")
   .setOnPreferenceClickListener(
      new Preferences.OnPreferenceClickListener() {
    @Override
    public boolean onPreferenceClick(Preference preference) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("http://some_url_here"));
        startActivity(intent);
        return true;
    }
});

【问题讨论】:

    标签: android preferences


    【解决方案1】:
    getPreferenceManager()
       .findPreference("my_preference_key")
       .setOnPreferenceClickListener(
          new Preferences.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("http://some_url_here"));
            startActivity(intent);
            return true;
        }
    });
    
    enter code here
    

    【讨论】:

      【解决方案2】:
      Intent intent = new Intent(Intent.ACTION_VIEW);
      intent.setData(Uri.parse("http://some_url_here"));
      startActivity(intent);
      

      可以简化为

      startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://some_url_here")));
      

      【讨论】:

        【解决方案3】:

        假设您已经有一个 PreferenceFragmentPreferenceActivity 以及加载屏幕的行:

        addPreferencesFromResource(R.xml.my_prefs);
        

        无需编写任何额外代码就可以创建网站链接(以及更多)!这是我无需编写任何 Java 代码即可实现的功能的演示(“链接" 部分):

        启动网站是最简单的。请注意,这些首选项都没有任何键,因此即使我想也无法从代码中访问它们。当然,缺少密钥完全是可选的。

        src/main/res/xml/my_prefs.xml

        <PreferenceScreen
            xmlns:android="http://schemas.android.com/apk/res/android"
            >
            <!-- whatever you had before -->
            <PreferenceCategory android:title="Links"><!-- optional header -->
                <Preference
                    android:title="App info in settings"
                    >
                    <intent
                        android:action="android.settings.APPLICATION_DETAILS_SETTINGS"
                        android:data="package:my.app.package.name"
                        />
                </Preference>
                <Preference
                    android:title="App details in Play Store"
                    >
                    <intent
                        android:action="android.intent.action.VIEW"
                        android:data="market://details?id=my.app.package.name"
                        />
                </Preference>
                <Preference
                    android:title="Privacy Policy on our website"
                    >
                    <intent
                        android:action="android.intent.action.VIEW"
                        android:data="http://www.myapp.com/foo#bar"
                        />
                </Preference>
                <Preference
                    android:title="Send feedback"
                    android:summary="via email">
                    <intent android:action="android.intent.action.VIEW"
                            android:data="mailto:your@email.address">
                        <extra android:name="android.intent.extra.TEXT"
                               android:value="Pre-filled email body." />
                        <extra android:name="android.intent.extra.SUBJECT"
                               android:value="Pre-filled email subject" />
                    </intent>
                </Preference>
                <Preference
                    android:title="@string/about_title"
                    android:summary="@string/app_name"
                    >
                    <!-- @strings are the same as used in AndroidManifest.xml;
                    about_title is from <activity> label,
                    app_name is from <application> label. -->
                    <intent
                        android:targetClass="my.app.AboutActivity"
                        android:targetPackage="my.app.package.name"
                    />
                </Preference>
            </PreferenceCategory>
        </PreferenceScreen>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多