如何强制共享意图打开特定应用程序?

25

我喜欢共享意图,这是打开带有图像和文本参数的共享应用程序的完美方式。

但现在我正在研究如何强制共享意图从列表中打开特定的应用程序,并使用给定的参数。

这是我的实际代码,它显示了手机上安装的共享应用程序列表。请问有人可以告诉我应该添加什么代码来强制例如官方 Twitter 应用程序?和官方 Facebook 应用程序?

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse("file:///sdcard/test.jpg");  
sharingIntent.setType("image/*");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "body text");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));

感谢


你有关于Facebook的问题吗? - Lix
如果他们没有使用官方的 Twitter 应用程序,那么这样做会导致失败吗?为什么要限制别人分享的方式? - lathomas64
3个回答

41

对于Facebook

public void shareFacebook() {
        String fullUrl = "https://m.facebook.com/sharer.php?u=..";
        try {
            Intent sharingIntent = new Intent(Intent.ACTION_SEND);
            sharingIntent.setClassName("com.facebook.katana",
                    "com.facebook.katana.ShareLinkActivity");
            sharingIntent.putExtra(Intent.EXTRA_TEXT, "your title text");
            startActivity(sharingIntent);

        } catch (Exception e) {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(fullUrl));
            startActivity(i);

        }
    }

为了Twitter。

public void shareTwitter() {
        String message = "Your message to post";
        try {
            Intent sharingIntent = new Intent(Intent.ACTION_SEND);
            sharingIntent.setClassName("com.twitter.android","com.twitter.android.PostActivity");
            sharingIntent.putExtra(Intent.EXTRA_TEXT, message);
            startActivity(sharingIntent);
        } catch (Exception e) {
            Log.e("In Exception", "Comes here");
            Intent i = new Intent();
            i.putExtra(Intent.EXTRA_TEXT, message);
            i.setAction(Intent.ACTION_VIEW);
            i.setData(Uri.parse("https://mobile.twitter.com/compose/tweet"));
            startActivity(i);
        }
    }

我可以在推文中附加一张图片吗? - Intathep
1
即使已安装 Twitter 应用程序,它仍会产生“未找到活动”异常。 - Rahul
太好了!非常感谢!我已经像这样将PDF文件附加到我的意图中: sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f)); 如果您想知道setClassName的字段,可以使用以下代码: java.util.List<ResolveInfo> mApps = new ArrayList<ResolveInfo>(); PackageManager pManager = getPackageManager(); mApps = pManager.queryIntentActivities(target,PackageManager.COMPONENT_ENABLED_STATE_DEFAULT); - vLopez
如果类名未知,我该怎么办(系统意图选择器可以做到)? - user5395084
Instagram也有类似的东西吗? - Narendra Singh

6

4

100%有效解决方案

如果您想在任何应用程序中分享内容或通过每个操作打开URL,请使用以下方法:

private void shareOrViewUrlViaThisApp(String appPackageName, String url) {
    boolean found = false;
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse(url));
    
    List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(intent, 0);
    if (!resInfo.isEmpty()){
        for (ResolveInfo info : resInfo) {
            if (info.activityInfo.packageName.toLowerCase().contains(appPackageName) ||
                    info.activityInfo.name.toLowerCase().contains(appPackageName) ) {
            
                intent.setPackage(info.activityInfo.packageName);
                found = true;
                break;
            }
        }
        if (!found)
            return;

        startActivity(Intent.createChooser(intent, "Select"));
    }
}

然后只需调用:

shareOrViewUrlViaThisApp(<your package name>,<your url>);

这个答案的灵感来自这里


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接