只需要分享一个动态文本字符串的URL到应用程序.
Android的原始
Android分享意图设置正确,并且使用Twitter,Gmail等功能完美无缺,但是很多人会猜测,它与Facebook无效.显然,因为Facebook不会接受intent.EXTRA_TEXT字段中的文本,只有一个URL.
那么我的问题是:有没有办法根据他们选择分享哪种方法来分支股份意向追加?例如,如果他们通过gmail或Twitter共享,请使用现有的字符串URL(所需选项)EXTRA_TEXT,但如果他们选择通过Facebook共享,则只能使用URL作为EXTRA_TEXT.
不是真的想实现Facebook Android SDK这样一个简单的任务,内置在Android本身.
欣赏你的时间和建议.
尝试这样的东西,但它显然失败了,因为它只检查共享选项是否存在(当共享弹出时,不是在他们点击共享方法后),它在选择方法时不响应.
String shareBody = "app string text " + act_txt + " more text! Get the app at http://www.appurl.com"; PackageManager pm = view.getContext().getPackageManager(); List<ResolveInfo> activityList = pm.queryIntentActivities(sharingIntent,0); for(final ResolveInfo app : activityList) { Log.i(TAG,"app.actinfo.name: " + app.activityInfo.name); //if((app.activityInfo.name).contains("facebook")) { if("com.facebook.katana.ShareLinkActivity".equals(app.activityInfo.name)) { sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,"http://www.appurl.com"); startActivity(Intent.createChooser(sharingIntent,"Share idea")); break; } else { sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"app name"); sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,shareBody); startActivity(Intent.createChooser(sharingIntent,"Share")); break; } }
解决方法
找到一个解决方案,在这个SO问题要求别的东西:
https://stackoverflow.com/a/8550043/1938669
尝试发布我在这里的原始问题是接近的.在可能的shareIntent列表的周期内,您需要创建针对特定共享选项的新共享意图(如Facebook或Twitter)
这是一个最终的工作解决方案,如果选择了Facebook,则只共享一个URL,否则共享完整的文本字符串url:
public void shareIt(View view){ //sharing implementation List<Intent> targetedShareIntents = new ArrayList<Intent>(); Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); sharingIntent.setType("text/plain"); String shareBody = "string of text " + txt_var + " more text! Get the app at http://someapp.com"; PackageManager pm = view.getContext().getPackageManager(); List<ResolveInfo> activityList = pm.queryIntentActivities(sharingIntent,0); for(final ResolveInfo app : activityList) { String packageName = app.activityInfo.packageName; Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND); targetedShareIntent.setType("text/plain"); targetedShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"share"); if(TextUtils.equals(packageName,"com.facebook.katana")){ targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT,"http://someurl.com"); } else { targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT,shareBody); } targetedShareIntent.setPackage(packageName); targetedShareIntents.add(targetedShareIntent); } Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0),"Share Idea"); chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,targetedShareIntents.toArray(new Parcelable[]{})); startActivity(chooserIntent); }