欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 分享功能的实现代码

程序员文章站 2022-10-14 14:16:31
android 分享功能的实现代码 一个activity中,取出设备上安装的所有支持分享动作的activity,在grid中显示。 实例代码: /** *...

android 分享功能的实现代码

一个activity中,取出设备上安装的所有支持分享动作的activity,在grid中显示。

实例代码:

/**
 * 分享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 recyclerview.viewholder {
    textview tv;
    imageview iv;
    public vh(view itemview) {
      super(itemview);

      itemview.setonclicklistener(new view.onclicklistener() {
        @override
        public void onclick(view view) {
          //点击了某个app的图标,用选择的app分享内容
          intent share = new intent(android.content.intent.action_send);
          share.settype("text/*");
          share.putextra(intent.extra_subject, "分享");
          share.putextra(intent.extra_text,nshareactivity.this.strtoshare);
          //share.putextra(intent.extra_stream, uri); // optional, just if you wanna share an image.
          sharedpkginfo pi = sharepkginfo.get(getadapterposition());
          share.setclassname(pi.pkgname,pi.activityclassname);
          //share.setpackage();
          startactivity(share);
        }
      });
    }
  }

  //获取支持供享的包的信息
  list<sharedpkginfo> sharepkginfo=new arraylist<>();

  //要分享出去的文本放在这里
  private string strtoshare=null;

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);

    intent intent= this.getintent();
    strtoshare = intent.getstringextra(extra_str_to_share);

    getallsharepackages();

    //将可共享的app图标都放在一个gridview中
    recyclerview v=new recyclerview(this);
    v.setpadding(16,16,16,16);

    gridlayoutmanager lm=new gridlayoutmanager(this,4);
    v.setlayoutmanager(lm);
    v.setadapter(new recyclerview.adapter<vh>()
    {
      @override
      public vh oncreateviewholder(viewgroup parent, int viewtype) {
        //必须创建新的view holder
        linearlayout v=new linearlayout(nshareactivity.this);
        v.setpadding(8,8,8,8);
        vh vh=new vh(v);

        //先创建item view:上面一个图标,下面一个文本
        linearlayout.layoutparams lp=new linearlayout.layoutparams(
            linearlayout.layoutparams.match_parent,
            linearlayout.layoutparams.wrap_content);
        v.setorientation(linearlayout.vertical);
        v.setlayoutparams(lp);

        imageview imgv=new imageview(nshareactivity.this);
        imgv.setlayoutparams(new linearlayout.layoutparams(
            linearlayout.layoutparams.match_parent,
            120));
        textview tv=new textview(nshareactivity.this);

        tv.setgravity(gravity.center);
        v.addview(imgv);
        v.addview(tv);

        vh.tv=tv;
        vh.iv=imgv;

        return vh;
      }

      @override
      public void onbindviewholder(vh holder, int position) {
        //将视图与数据绑定
        sharedpkginfo spi=sharepkginfo.get(position);
        holder.tv.settext(spi.appname);
        holder.iv.setimagedrawable(spi.icon);
      }

      @override
      public int getitemcount() {
        return sharepkginfo.size();
      }
    });

    v.setbackgroundcolor(color.white);
    this.setcontentview(v);
  }

  //获取所有支持send action的包名和图片
  void getallsharepackages()
  {
    intent share = new intent(android.content.intent.action_send);
    //分析网站地址的话用这个:
    //intent.settype("text/plain"); //纯文本
    share.settype("text/*");
    // gets the list of intents that can be loaded.
    list<resolveinfo> resinfo = getpackagemanager().queryintentactivities(share, 0);
    if (!resinfo.isempty()) {
      for (resolveinfo info : resinfo) {
        sharedpkginfo spi = new sharedpkginfo();

        spi.pkgname = info.activityinfo.packagename;
        spi.icon = info.loadicon(getpackagemanager());
        spi.appname = info.loadlabel(getpackagemanager()).tostring();
        spi.activityclassname=info.activityinfo.name;

        sharepkginfo.add(spi);
        //log.w("shared",spi.pkgname+" , "+spi.appname+","+info.activityinfo.name);
      }
    }
  }

}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!