SoFunction
Updated on 2025-03-11

Android sharing function implementation code

Android sharing function implementation code

In an activity, remove all activities that support sharing actions installed on the device and display them in the grid.

Example code:

/**
  * Share activity
  */
public class NShareActivity extends AppCompatActivity {
  public final static String EXTRA_STR_TO_SHARE="str_to_share1";

  private class SharedPkgInfo{
    String pkgName;
    Drawable icon;
    String appName;
    String activityClassName;
  }

  class Vh extends  {
    TextView tv;
    ImageView iv;
    public Vh(View itemView) {
      super(itemView);

      (new () {
        @Override
        public void onClick(View view) {
          //Clicked the icon of an app and shared the content with the selected app          Intent share = new Intent(.ACTION_SEND);
          ("text/*");
          (Intent.EXTRA_SUBJECT, "share");
          (Intent.EXTRA_TEXT,);
          //(Intent.EXTRA_STREAM, uri); // Optional, just if you wanna share an image.
          SharedPkgInfo pi = (getAdapterPosition());
          (,);
          //();
          startActivity(share);
        }
      });
    }
  }

  //Get information on packages that support for enjoyment  List<SharedPkgInfo> sharePkgInfo=new ArrayList<>();

  //The text you want to share is here  private String strToShare=null;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);

    Intent intent= ();
    strToShare = (EXTRA_STR_TO_SHARE);

    getAllSharePackages();

    //Put all shareable app icons in a gridview    RecyclerView v=new RecyclerView(this);
    (16,16,16,16);

    GridLayoutManager lm=new GridLayoutManager(this,4);
    (lm);
    (new <Vh>()
    {
      @Override
      public Vh onCreateViewHolder(ViewGroup parent, int viewType) {
        //A new view holder must be created        LinearLayout v=new LinearLayout();
        (8,8,8,8);
        Vh vh=new Vh(v);

        // Create item view first: an icon above and a text below         lp=new (
            .MATCH_PARENT,
            .WRAP_CONTENT);
        ();
        (lp);

        ImageView imgv=new ImageView();
        (new (
            .MATCH_PARENT,
            120));
        TextView tv=new TextView();

        ();
        (imgv);
        (tv);

        =tv;
        =imgv;

        return vh;
      }

      @Override
      public void onBindViewHolder(Vh holder, int position) {
        //Bind view with data        SharedPkgInfo spi=(position);
        ();
        ();
      }

      @Override
      public int getItemCount() {
        return ();
      }
    });

    ();
    (v);
  }

  //Get all package names and pictures that support send Action  void getAllSharePackages()
  {
    Intent share = new Intent(.ACTION_SEND);
    // Use this to analyze the website address:    //("text/plain"); //plain text    ("text/*");
    // gets the list of intents that can be loaded.
    List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
    if (!()) {
      for (ResolveInfo info : resInfo) {
        SharedPkgInfo spi = new SharedPkgInfo();

         = ;
         = (getPackageManager());
         = (getPackageManager()).toString();
        =;

        (spi);
        //("shared",+" , "++","+);
      }
    }
  }

}

Thank you for reading, I hope it can help you. Thank you for your support for this site!