2012-03-02 15 views
8

En mi proyecto, necesito compartir información en Just Facebook y Twitter. Actualmente, cuando utiliza los siguientes códigos, Android ofrece una lista de todas las redes sociales que tiene en su teléfono móvil.Android, ¿Cómo se filtra el intercambio social por tener solo Facebook y 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:")); 
} 

El requisito es simplemente mostrar Facebook y Twitter en esta lista. ¿Es posible filtrar esta lista para tener estos dos?

enter image description here

+0

Tal vez usted no puede hacer esto directamente dentro de una intención, pero yo creo que se puede implementar su propio diálogo para hacerlo . – Huang

Respuesta

0

No sé si eso es lo que quieres decir, pero se puede utilizar el Android incorporado en el menú para compartir ...

Puede compartir una URL para Facebook, Twitter, Gmail y más (siempre y cuando las aplicaciones se instalan en el dispositivo) utilizando Intentos:

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

no se olvide de insertar este a la 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> 

Hope this helps!

+2

Creo que quiere decir cómo no puede tener Gmail y otras aplicaciones allí, solo Facebook y Twitter. –

+0

gracias querido Mike, este código es igual al mío. Como mencionó Ken, necesito filtrar la lista para tener solo esas dos redes sociales. – Hesam

3

Puede consultar las actividades que coinciden con la intención y luego filtrar los nombres de los paquetes de las aplicaciones de Twitter y Facebook, ya que el nombre del paquete de una aplicación nunca cambia. A continuación, coloque estos resultados en un cuadro de diálogo personalizado.

Usando algo como esto puede filtrar los resultados por Nombre del paquete:

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

Gracias querido Ken, Por lo tanto, muestra que no es posible filtrar la lista. Probaré tu sugerencia y luego te contaré el resultado. Gracias de nuevo. – Hesam

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(); 
     } 
    } 
Cuestiones relacionadas