选择首选项后如何启动浏览器

发布于 2024-10-07 07:59:27 字数 574 浏览 0 评论 0原文

我正在创建一个首选项菜单,并希望在单击特定首选项时启动浏览器(具有特定的 url)。我知道这是可以做到的,但我现在似乎无法让它发挥作用。

有什么想法吗?

谢谢

######SOLUTION

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

思念满溢 2024-10-14 07:59:27
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
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
百变从容 2024-10-14 07:59:27

假设您已经有一个 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:[email protected]">
                <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>

注意:与任何意图一样,它们可能无法解析,因此最好禁用/隐藏按钮,此类解决了这个问题:

public class ExternalIntentPreference extends Preference {

    public ExternalIntentPreference(Context context) {
        super(context);
    }
    public ExternalIntentPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public ExternalIntentPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public ExternalIntentPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override public void onAttached() {
        super.onAttached();
        PackageManager pm = getContext().getPackageManager();
        // Don't forget <queries> and ResolveInfoFlags.of deprecation.
        List<ResolveInfo> intents = pm.queryIntentActivities(getIntent(), 0);
        setEnabled(!intents.isEmpty());
    }
}

用法完全相同:

        <my.pack.age.ExternalIntentPreference
            android:title="App details in Play Store"
            >
            <intent
                android:action="android.intent.action.VIEW"
                android:data="market://details?id=my.app.package.name"
                />
        </my.pack.age.ExternalIntentPreference>

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):
enter image description here

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:[email protected]">
                <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>

Note: as with any intents, they may not resolve, so it's better to disable/hide the button, this class solves that:

public class ExternalIntentPreference extends Preference {

    public ExternalIntentPreference(Context context) {
        super(context);
    }
    public ExternalIntentPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public ExternalIntentPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public ExternalIntentPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override public void onAttached() {
        super.onAttached();
        PackageManager pm = getContext().getPackageManager();
        // Don't forget <queries> and ResolveInfoFlags.of deprecation.
        List<ResolveInfo> intents = pm.queryIntentActivities(getIntent(), 0);
        setEnabled(!intents.isEmpty());
    }
}

usage is exactly the same:

        <my.pack.age.ExternalIntentPreference
            android:title="App details in Play Store"
            >
            <intent
                android:action="android.intent.action.VIEW"
                android:data="market://details?id=my.app.package.name"
                />
        </my.pack.age.ExternalIntentPreference>
冷心人i 2024-10-14 07:59:27
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")));
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")));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文