2011-11-29 26 views
21

我喜欢共享意图,它是完美的打开与图像和文本参数的共享应用程序。如何强制共享意图打开特定的应用程序?

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

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

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")); 

感谢

+0

你有Facebook相关的问题吗? – Lix

+0

这样做会使它失败,如果他们不使用官方的twitter应用程序?为什么你想限制某人分享的方式? – lathomas64

回答

34

对于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); 
     } 
    } 
+0

我可以附加Tweet图片吗? – Intathep

+6

它不再适用于Facebook – younes0

+1

如果安装了Twitter应用程序,它会发现活动未发现异常enen – Rahul

6

有一种更通用的方法可以做到这一点,并且不需要知道应用程序意图的完整软件包名称。如果你想分享你想要的任何应用程序的东西,或通过每一个动作打开一个URL How to customize share intent in Android?

1

100%工作液

,只是用这个方法:

看到这个职位

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>); 

此答案受this启发。

相关问题