开发者

How to launch browser when a preference is selected

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?

开发者_如何学C

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


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


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


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

How to launch browser when a preference is selected

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜