【问题标题】:choosing certain apps to appear in the share chooser选择某些应用程序出现在共享选择器中
【发布时间】:2013-12-29 14:19:33
【问题描述】:

我有一个按钮,用于分享我的应用的链接; 但是这个按钮显示Skype、gmail、whatsapp... 例如,我怎样才能只显示whatsapp?我该怎么办?

另外,在发送邮件时,我只需要选择 gmail 和 hotmail,例如如何做到这一点?

我的意思是我知道如何选择诸如whatsapp之类的应用程序: Sending message through WhatsApp

但是我怎样才能选择几个应用程序.. 谢谢。

编辑 有人有关于如何创建自己的共享选择器的教程吗?谢谢。

【问题讨论】:

  • 你不能。在 Android 上,用户可以控制哪些应用响应哪些 Intent,以及哪些应用是这些 Intent 的默认设置(如果有)。用户可以选择,而不是你。
  • 是的,我知道,谢谢。但我的意思是如何设置whatsapp,例如接收应用程序查看:stackoverflow.com/questions/15462874/…,所以我知道如何选择一个应用程序,但我需要选择几个..

标签: android android-sharing


【解决方案1】:

创建您自己的选择器并使用以下代码:

public void share_on_gmail()
{
    Intent sharingIntent = is_intent_available("com.google.android.gm",
            "com.google.android.gm.ComposeActivityGmail", "Gmail");

    send_mail(sharingIntent);
}

public void share_on_yahoo()
{
    Intent sharingIntent = is_intent_available("com.android.email",
            "com.android.email.activity.MessageCompose", "Email");

    send_mail(sharingIntent);
}

public void share_on_facebook()
{
    Intent sharingIntent = is_intent_available("com.facebook.katana",
                    "com.facebook.katana.ShareLinkActivity", "Facebook");
}

public void share_on_twitter()
{
    Intent sharingIntent = is_intent_available("com.twitter.android",
                            "com.twitter.android.PostActivity", "Twitter");

    if (sharingIntent != null)
    {
        String subject = ((XmlNewsDetailsParser)xmlParser).subject;
        String body = ((XmlNewsDetailsParser)xmlParser).body;
        File  mFile = savebitmap(((Picture)itemsAdapter.getItem(0)).image.bitmap);
        sharingIntent.setType("image/png");
        sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mFile));
        sharingIntent.putExtra(Intent.EXTRA_TEXT, "القارئ العربي" + "\n\n" + subject + "\n\n" + Html.fromHtml(body));

        startActivity(sharingIntent);
    }
}

public Intent is_intent_available(final String className, String activityName, String appName)
{
        Intent I = new Intent(android.content.Intent.ACTION_SEND);
        if (!activityName.equals(""))
        {
            I.setClassName(className, activityName);
        }
        else
        {
            I.setPackage(className);
        }

        PackageManager packageManager = getPackageManager();
        List list = packageManager.queryIntentActivities(I, PackageManager.MATCH_DEFAULT_ONLY);

        if (list==null || list.size() == 0)
        {
            AlertDialog ad = new AlertDialog.Builder(this).create();
            ad.setMessage("Please install the " + appName + " app to share news with your friends.");
            ad.setButton2("Later", new DialogInterface.OnClickListener() {  
                @Override  
                public void onClick(DialogInterface dialog, int which) {  
                    dialog.dismiss();
                }
            });

            ad.setButton("Install Now", new DialogInterface.OnClickListener() {  
                @Override  
                public void onClick(DialogInterface dialog, int which) {  
                    Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=" + className));
                    startActivity(marketIntent);
                }
            });  
            ad.show();
            return null;
        }
        return I;
}

public void send_mail(Intent sharingIntent)
{
    if (sharingIntent == null) return;
    String subject = ((XmlNewsDetailsParser)xmlParser).subject;
    String body = ((XmlNewsDetailsParser)xmlParser).body;
    File  mFile = savebitmap(((Picture)itemsAdapter.getItem(0)).image.bitmap);
    sharingIntent.setType("image/png");
    sharingIntent.putExtra(Intent.EXTRA_SUBJECT, subject + "القارئ العربي - ");
    sharingIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body));
    sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mFile));
    startActivity(sharingIntent);
}

我还有几个应用程序的类名和活动名。我可以提供。试试这些,我会修改答案。

【讨论】:

  • 非常感谢哈桑,但这是否允许我使用多个应用程序,例如打开一个选择器:whatsapp、gmail 和 hotmail 作为选择,或者这将只允许我选择一个应用程序?
  • 选择器默认选择可用于与您提供的内容共享的应用程序。但是,正如我从您的问题中了解到的那样,您希望其中一些应用程序出现在某些情况下,而其他应用程序出现在其他情况下。除非您创建自己的选择器(您创建的包含取决于内容的应用程序的窗口),否则这是需要工作的。在这种情况下,您必须使用答案中的代码来处理它。
  • 如您所见,我没有在此处显示应用选择器。因为我想自定义出现在我创建的窗口中的应用程序(这些应用程序会根据情况而有所不同)。因此,我需要一种直接共享而不显示选择器窗口的方法,因为我实现了我的。当用户从我的选择器中选择时。我根据他的选择分享。在你想要的 facebook 示例中恍惚。
  • 好的,我稍后会。该回家了。
  • 是的,但不知道如何使用它,因为不明白代码中发生了什么所以没有用,但我相信这是一个很好的答案,并将其标记为解决方案只要它工作..所以稍后会重试并尝试找出代码是如何工作的..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-16
  • 1970-01-01
  • 2018-05-06
  • 2017-08-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多