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

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值