How to launch browser when a preference is selected

随声附和 提交于 2020-01-02 18:07:32

问题


I am creating a preference menu and would like to launch a browser (with a specific url) when a particular preference is clicked on. I know this can be done, but I cant seem to get it to work right now.

Any ideas?

Thanks

######SOLUTION

So after my brain fart disappeared, this is what i did:

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;
    }
});

回答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);

Can be reduced to

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



回答3:


Assuming you already have a PreferenceFragment or PreferenceActivity in place with a line that loads the screen:

addPreferencesFromResource(R.xml.my_prefs);

It's possible to do website links (and many more) without writing any additional code! Here is a demonstration of the capabilities I was able to achieve without writing a single line of Java code ("Links" section):

Launching a website is the simplest one. Notice that none of these preferences have any keys, so they're not accessible from code even if I wanted to. Of course this lack of key is entirely optional.

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>


来源:https://stackoverflow.com/questions/4418704/how-to-launch-browser-when-a-preference-is-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!