使用 Android 意图在 facebook 和 twitter 上发布不同的文本

发布于 2024-12-10 10:37:30 字数 286 浏览 0 评论 0原文

我想让我的 Android 应用程序的用户能够在 Facebook、Twitter 上发布一些数据,并将其通过电子邮件发送给某人。我为此使用 Intent.ACTION_SEND 。我可以添加电子邮件主题并将测试添加为 Intent.EXTRA_TEXT。但我希望将不同的文本发送到不同的应用程序。 就像发送到 Twitter 的文本很短一样,发送到 Facebook 的文本将包含链接和镜头描述,而 on ein 电子邮件则包含所有内容。 我怎样才能实现这样的功能? 我最多可以让facebook和twitter采用相同的文本,但与电子邮件中的内容不同。

I want to enable the user of my android app to post some data on fb,twitter and email it to someone as well. I am using Intent.ACTION_SEND for this. I can add the email subject and add test as Intent.EXTRA_TEXT. But I want different texts to be sent to dirrerent applications.
Like the text to be sent to twitter will be short, the text to be sent to facebook will have a link and a shot description, and the on ein email have all the content.
How can I achieve such a functionality?
At most I can let facebook and twitter take the same text but different from what it is in email.

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

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

发布评论

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

评论(2

秋千易 2024-12-17 10:37:30

首先,创建一个 Intent 来表示您想要发送电子邮件、发布 Twitter 等的内容。在 Intent.EXTRA_TEXT 和主题中放置一些好的默认值。然后调用 Intent.createChooser() 符合您的意图。此方法将返回一个 Intent,表示用户选择了哪个 Activity。现在,我们在这里添加您想要的自定义。像这样检查返回的 Intent:

Intent intentYouWantToSend = new Intent(Intent.ACTION_SEND);
intentYouWantToSend.putExtra(Intent.EXTRA_TEXT, "Good default text");
List<ResolveInfo> viableIntents = getPackageManager().queryIntentActivities(  
  intentYouWantToSend, PackageManager.MATCH_DEFAULT_ONLY);
//Here you'll have to insert code to have the user select from the list of 
//resolve info you just received.
//Once you've determined what intent the user wants, store it in selectedIntent
//This details of this is left as an exercise for the implementer. but should be fairly
//trivial
if(isTwitterIntent(selectedIntent)){
  selectedIntent.putExtra(Intent.EXTRA_TEXT, "Different text for twitter");
}
else if(isFacebookIntent(selectedIntent)){
  selectedIntent.putExtra(Intent.EXTRA_TEXT, "Different text for facebook");
}
startActivity(selectedIntent);

通过检查 Intent.createChooser 返回的 Intent,我们可以确定在启动它之前需要如何修改它。不过,您必须自己实现 isTwiterIntent 和 isFacebookIntent 函数。我想这会相对容易,因为您可能只需要检查意图的上下文。我会做更多的研究,看看是否无法找到准确的解决方案来确定 Intent 是用于 Twitter 还是 Facebook 或其他什么,并尝试为您提供更完整的答案。

First, create an Intent representing what you want to potentially e-mail, post twitter, etc. Put some good default values in the Intent.EXTRA_TEXT and the subject. Then call, Intent.createChooser() with your intent. This method will return an Intent representing which Activity the user selected. Now, here's where we add the customization you want. Examine the Intent that is returned like so:

Intent intentYouWantToSend = new Intent(Intent.ACTION_SEND);
intentYouWantToSend.putExtra(Intent.EXTRA_TEXT, "Good default text");
List<ResolveInfo> viableIntents = getPackageManager().queryIntentActivities(  
  intentYouWantToSend, PackageManager.MATCH_DEFAULT_ONLY);
//Here you'll have to insert code to have the user select from the list of 
//resolve info you just received.
//Once you've determined what intent the user wants, store it in selectedIntent
//This details of this is left as an exercise for the implementer. but should be fairly
//trivial
if(isTwitterIntent(selectedIntent)){
  selectedIntent.putExtra(Intent.EXTRA_TEXT, "Different text for twitter");
}
else if(isFacebookIntent(selectedIntent)){
  selectedIntent.putExtra(Intent.EXTRA_TEXT, "Different text for facebook");
}
startActivity(selectedIntent);

By examining the Intent that is returned by Intent.createChooser, we can determine how we need to modify it before launching it. You'll have to implement the isTwiterIntent and isFacebookIntent function yourself though. I imagine this will be relatively easy though, as you probably just have to examine the context of the Intent. I'll do a little more research and see if I can't find an exact solution for determining if an Intent is for Twitter or Facebook, or whatever and try to give you a more complete answer.

不语却知心 2024-12-17 10:37:30
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    List<ResolveInfo> activities = getPackageManager().queryIntentActivities(sharingIntent, 0);

通过此代码,您可以获得支持 Intent.ACTION_SEND 操作的应用程序列表。
之后,您可以构建一个警报对话框来显示这些应用程序。

然后在特定应用程序的单击监听器上,您可以按照给定的代码进行更改

public void onClick(DialogInterface dialog, int which) 
        {
            ResolveInfo info = (ResolveInfo) adapter.getItem(which);
            if(info.activityInfo.packageName.contains("facebook"))
            {
                shareToFacebook();
            } 
            else { 

                Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
                sharingIntent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
                sharingIntent.setType("text/plain");
                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "hello");
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "intent"); 
startActivity(sharingIntent);
            } 
        }
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    List<ResolveInfo> activities = getPackageManager().queryIntentActivities(sharingIntent, 0);

By this code you get list of applications that support Intent.ACTION_SEND action.
After that u can built a Alert Dialog to display those applications.

then on click listener of the particular application you can make your changes as given code

public void onClick(DialogInterface dialog, int which) 
        {
            ResolveInfo info = (ResolveInfo) adapter.getItem(which);
            if(info.activityInfo.packageName.contains("facebook"))
            {
                shareToFacebook();
            } 
            else { 

                Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
                sharingIntent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
                sharingIntent.setType("text/plain");
                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "hello");
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "intent"); 
startActivity(sharingIntent);
            } 
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文