2011-11-29 23 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啓發。

相關問題