为了显示目的,在LabeledIntent中包装了Intent [英] Wrapping Intent in LabeledIntent for display purposes

查看:136
本文介绍了为了显示目的,在LabeledIntent中包装了Intent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要求:

我的应用程序中有一个共享"按钮.我需要通过Facebook分享.我需要选择是否已安装本机Facebook应用程序.如果未安装该应用程序,我们的决定是将用户发送到facebook.com进行共享.

I have a "share" button in my app. I have a requirement to share via Facebook. I need to have the option whether or not the native Facebook app is installed. Our decision is to send the user to facebook.com to share if the app is not installed.

当前状态:

我可以检测到何时未安装本机应用程序(通过软件包名称),并向选择器添加其他意图.

I can detect when the native app is not installed (via the package name), and add additional intents to the chooser.

问题:

用户必须选择通过"Facebook的网站"共享的项目显示为浏览器",并带有Android浏览器图标. LabeledIntent项目没有出现,并且我收到找不到针对Intent的活动{act = android.intent.action.VIEW dat = ...}

The item the user has to select to share via "Facebook's Website" says, "Browser" and has the Android Browser icon. The LabeledIntent item does not appear and I get a "No activity found for Intent { act=android.intent.action.VIEW dat=...}

代码(简体...):

Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_SUBJECT, "check this out");
intent.putExtra(Intent.EXTRA_TEXT, urlToShare);
boolean facebookInstalled = false;

Intent chooser = Intent.createChooser(intent, "Share this link!");
if (!facebookInstalled)
{
    Intent urlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com/sharer/sharer.php?u=" + Uri.encode(urlToShare)));
    Intent niceUrlIntent = new LabeledIntent(urlIntent, context.getApplicationContext().getPackageName(), "Facebook's Website", R.drawable.icon);

    // Ideally I would only add niceUrlIntent in the end, but that doesn't add anything to the chooser as-is
    chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[urlIntent, niceUrlIntent]);
}
context.startActivity(chooser);

解决方案

@CommonsWare指出的解决方案是使用LabeledIntent封装一个意图,该意图将传递给我创建的新Activity,该意图只是将ACTION_VIEW意图发送给相应的Uri.

The solution as @CommonsWare pointed out, is to use the LabeledIntent to wrap an intent that goes to a new Activity I create, that simply sends a ACTION_VIEW intent to the appropriate Uri.

Intent myActivity = new Intent(context, ViewUriActivity.class);
myActivity.putExtra(ViewUriActivity.EXTRA_URI, "http://...");
Intent niceUrlIntent = new LabeledIntent(myActivity, context.getApplicationContext().getPackageName(), "Facebook's Website", R.drawable.icon);
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{niceUrlIntent});

ViewUriActivity看起来像这样:

The ViewUriActivity looks like this:

public final class ViewUriActivity extends Activity
{
    public static final String EXTRA_URI = ViewUriActivity.class.getSimpleName() + "EXTRA_URI";

    protected void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        Intent urlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getIntent().getExtras().getString(EXTRA_URI)));
        startActivity(urlIntent);
        finish();
    }
}

推荐答案

尽管未记录,但LabeledIntents似乎仅适用于已解决的Intent.因此,除非该意图已经定义了特定的活动和程序包,否则您将无法使用它.

Though undocumented, looks like LabeledIntents only work on resolved Intents. So unless the intent already has a specific activity and package defined you will not be able to use it.

这不足为奇,因为可能有许多Activity可以解决一个LabeledIntent,并且您的ChooserActivity将所有图标和名称显示为相同.

This is not surprising since there may be many Activities that might get resolved for one LabeledIntent and your chooserActivity will show all icons and names as same.

因此,在您的情况下,您将必须先解决精度,然后再使用LabeledIntent.但这可能导致用户无法选择自己选择的浏览器. 使用下面的函数从您的意图中获取LabeledIntent.

So in your case you will have to resolve the acctivity first and then use LabeledIntent. But this might result in user not able to choose browser of his choice. Use below function to get LabeledIntent from you intent.

Intent urlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com/sharer/sharer.php?u=" + Uri.encode(urlToShare)));
Intent niceUrlIntent = getLabelintent(urlIntent, "Facebook's Website", R.drawable.icon);

和getLabelIntent

And getLabelIntent

public LabeledIntent getLabelintent(Intent in, String name, int drawable) {
    PackageManager pm = getPackageManager();
    ComponentName launchname = in.resolveActivity(pm);
    if (launchname != null) {
        Intent resolved = new Intent();
        resolved.setComponent(launchname);
        resolved.setData(in.getData());
        LabeledIntent niceUrlIntent = new LabeledIntent(resolved,
                getPackageName(), name, drawable);

        return niceUrlIntent;
    }
    return null;
}

这篇关于为了显示目的,在LabeledIntent中包装了Intent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆