2012-03-02 76 views
8

在我的項目中,我需要將一些信息分享到Facebook和Twitter上。目前,當您使用以下代碼時,android會提供您手機中所有社交網絡的列表。Android,如何過濾只有Facebook和Twitter的社交分享?

public void share(String subject,String text) { 
final Intent intent = new Intent(Intent.ACTION_SEND); 

intent.setType("text/plain"); 
intent.putExtra(Intent.EXTRA_SUBJECT, subject); 
intent.putExtra(Intent.EXTRA_TEXT, text); 
startActivity(Intent.createChooser(intent, "Share with:")); 
} 

要求只是在此列表中顯示Facebook和Twitter。是否有可能過濾這個列表以獲得這兩個?

enter image description here

+0

或許你不能直接一個Intent內做到這一點,但我相信你能實現自己的對話框這樣做。 – Huang 2012-03-02 05:03:13

回答

0

我不知道如果這是你的意思,但你可以使用Android內置的共享菜單...

您可以共享一個URL Facebook,微博,Gmail的多(只要應用程序被安裝在設備上)使用意圖:

Intent i = new Intent(Intent.ACTION_SEND); 
i.setType("text/plain"); 
i.putExtra(Intent.EXTRA_SUBJECT, "Sharing URL"); 
i.putExtra(Intent.EXTRA_TEXT, "http://www.url.com"); 
startActivity(Intent.createChooser(i, "Share URL")); 

不要忘了這個插入到的Manifest.xml:

<activity android:name="ShareLink"> 
       <intent-filter> 
        <action android:name="android.intent.action.SEND" /> 
        <category android:name="android.intent.category.DEFAULT" /> 
        <data android:mimeType="text/plain" /> 
       </intent-filter> 
      <meta-data/> 
      </activity> 

希望這有助於!

+2

我認爲他的意思是他怎麼可能沒有Gmail和其他應用程序,只有Facebook和Twitter。 – 2012-03-02 05:27:22

+0

謝謝親愛的Mike,這段代碼和我的一樣。正如Ken所提到的,我需要過濾這個列表才能擁有這兩個社交網絡。 – Hesam 2012-03-02 06:14:38

3

您可以查詢符合意圖的活動,然後篩選Twitter和Facebook應用的軟件包名稱,因爲應用的軟件包名稱不會更改。然後將這些結果放入自定義對話框中。

使用這樣的事情,你可以過濾結果通過包名:

final PackageManager pm = context.getPackageManager(); 
final Intent intent = new Intent(Intent.ACTION_SEND); 
List<ResolveInfo> riList = pm.queryIntentActivities(intent, 0); 
for (ResolveInfo ri : riList) { 
    ActivityInfo ai = ri.activityInfo; 
    String pkg = ai.packageName; 
    if (pkg.equals("com.facebook.katana") || pkg.equals("com.twitter.android")) { 

     // Add to the list of accepted activities. 

     // There's a lot of info available in the 
     // ResolveInfo and ActivityInfo objects: the name, the icon, etc. 

     // You could get a component name like this: 
     ComponentName cmp = new ComponentName(ai.packageName, ai.name); 
    } 
} 
+0

謝謝親愛的肯,因此它顯示不可能過濾列表。我會測試你的建議,然後我會告訴結果。再次感謝。 – Hesam 2012-03-02 06:18:46

8
Button btnshare=(Button)rootView.findViewById(R.id.btnshare); 
     btnshare.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Share(getShareApplication(),"Hello Text Share"); 
      } 
     }); 

    private List<String> getShareApplication(){ 
     List<String> mList=new ArrayList<String>(); 
     mList.add("com.facebook.katana"); 
     mList.add("com.twitter.android"); 
     mList.add("com.google.android.gm"); 
     return mList; 

    } 
    private void Share(List<String> PackageName,String Text) { 
     try 
     { 
      List<Intent> targetedShareIntents = new ArrayList<Intent>(); 
      Intent share = new Intent(android.content.Intent.ACTION_SEND); 
      share.setType("text/plain"); 
      List<ResolveInfo> resInfo = getActivity().getPackageManager().queryIntentActivities(share, 0); 
      if (!resInfo.isEmpty()){ 
       for (ResolveInfo info : resInfo) { 
        Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND); 
        targetedShare.setType("text/plain"); // put here your mime type 
        if (PackageName.contains(info.activityInfo.packageName.toLowerCase())) { 
         targetedShare.putExtra(Intent.EXTRA_TEXT,Text); 
         targetedShare.setPackage(info.activityInfo.packageName.toLowerCase()); 
         targetedShareIntents.add(targetedShare); 
        } 
       } 
       Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Share"); 
       chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{})); 
       startActivity(chooserIntent); 
      } 
     } 
     catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 
相關問題