2012-03-14 66 views
1

是否可以列出Intent.ACTION_SEND? 我的意思是我需要知道是否有人在Facebook上分享,或者通过action_send在Twitter上发布推文。Android - 共享

+0

你想要实现什么? – 2012-03-14 12:32:47

+0

我有“共享”按钮用于在用户的Facebook墙上发布,并且我有DialogListener知道用户是否真的共享或只是按下按钮并退出。我想有可能分享到Twitter或短信,但我需要知道用户是否真的分享以增加他的分数。 – goodm 2012-03-14 12:36:01

回答

2

也许你想要一个更完整的答案,因为接受一个比较短,我一年为时已晚,但希望它仍然是有用的:)

因此,这里是在处理多个意图可能的解决方案...

1)你想知道意图的结果(例如成功或失败)?

使用以下行刚开始的意图:

startActivityForResult(intent, 1); //instead of startActivity(intent) 

而且通过重写onActivityResult检索requestCode和resultCode为:

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == 0) { 
     if(resultCode == Activity.RESULT_OK){ 
      //intent 0 = succesful (Facebook) 
     } else{ 
      //intent 0 = failed or canceled 
     } 
    } else if (requestCode == 1) { 
     if(resultCode == Activity.RESULT_OK){ 
      //intent 1 = succesful (Twitter) 
     } else{ 
      //intent 1 = failed or canceled 
     } 
    } 
} 

2)你想知道哪些应用程式的意图打开?

不要信任内置的意向选择器,使你自己的对话框,并给每个意向另一个requestCode(唯一的整数值,以确定意图)

一个例子:

new AlertDialog.Builder(this) 
.setTitle("Share with friends!") 
.setSingleChoiceItems(new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, 
    new String[]{"Facebook", "Twitter"}), -1, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       if (which == 0) { 
        StartFacebookShare(); 
       } else if (which == 1) { 
        StartTwitterShare(); 
       } 
       dialog.dismiss(); 
      } 
}).show(); 

private void StartFacebookShare() { 
    String messageUrl = "http://www.stackoverflow.com"; //the url you want to share 
    try { 
     Intent intent = new Intent("android.intent.category.SEND"); 
     intent.putExtra(Intent.EXTRA_TEXT, messageUrl); 
     intent.setClassName("com.facebook.katana", "com.facebook.katana.ShareLinkActivity"); 
     startActivityForResult(intent, 0); 
    } catch (Exception e) { 
     Intent intent = new Intent(); 
     intent.setAction(Intent.ACTION_VIEW); 
     intent.setData(Uri.parse("https://m.facebook.com/sharer.php?u=" + messageUrl)); 
     startActivityForResult(intent, 1); 
    } 
} 
private void StartTwitterShare() { 
    String messageUrl= "http://www.stackoverflow.com"; //the string you want to tweet 
    try { 
     //see edit below for more info on API 
     Twitter twitter = TwitterFactory.getSingleton(); 
     Status status = twitter.updateStatus(messageUrl); 
    } catch (Exception e) { 
     Intent intent = new Intent(); 
     intent.setAction(Intent.ACTION_VIEW); 
     intent.setData(Uri.parse("https://twitter.com/intent/tweet?text=" + messageUrl)); 
     startActivityForResult(intent, 1); 
    } 
} 

一些有用的信息,可以发现herehere,也许搜索here或者如果你有我的代码的建议发表评论(我总是喜欢反馈^^)或者如果你坚持的东西:)

编辑:一些小的变化,如意图,并总是添加一个网络意图(现在不能失败,对吧?) 而对于Twitter部分我使用jar(将它放在你的'libs'文件夹中)以及需要下列registrationconfiguration的Twitter API,祝你好运!

+1

好的一段代码。现在不需要它,但我肯定将来需要它!谢谢 – goodm 2013-04-27 19:32:40

+0

切勿将第三方应用程序中的类和包名称硬编码,这些应用程序不属于某些记录和支持的API的一部分。 – CommonsWare 2013-04-27 19:41:50

+0

@CommonsWare同意。看到编辑,这是否有点你想的解决方案? – 2013-04-29 09:01:46

2

不,您无法确定某个人是否在通过ACTION_SEND链接到的应用程序中确实执行了任何操作。这与Web非常相似,您不知道某个人是否在通过URL链接到的网站上执行任何操作。