Как запустить браузер при выборе предпочтения - PullRequest
2 голосов
/ 11 декабря 2010

Я создаю меню настроек и хотел бы запустить браузер (с определенным 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;
    }
});

Ответы [ 3 ]

6 голосов
/ 12 декабря 2010
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
3 голосов
/ 11 декабря 2010
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")));
1 голос
/ 11 марта 2017

Если у вас уже есть PreferenceFragment или PreferenceActivity на месте со строкой, которая загружает экран:

addPreferencesFromResource(R.xml.my_prefs);

Можно делать ссылки на веб-сайты (и многие другие) без написания какого-либо дополнительного кода! Вот демонстрация возможностей, которых я смог достичь без написания ни одной строки кода Java («Ссылки») раздел):
enter image description here

Запуск сайта является самым простым. Обратите внимание, что ни одно из этих предпочтений не имеет ключей, поэтому они не доступны из кода, даже если бы я захотел. Конечно, отсутствие ключа совершенно необязательно.

SRC / основные / Рез / 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...