Android 分享功能的实现代码
Android分享功能的实现代码
一个Activity中,取出设备上安装的所有支持分享动作的Activity,在grid中显示。
实例代码:
/** *分享activity */ publicclassNShareActivityextendsAppCompatActivity{ publicfinalstaticStringEXTRA_STR_TO_SHARE="str_to_share1"; privateclassSharedPkgInfo{ StringpkgName; Drawableicon; StringappName; StringactivityClassName; } classVhextendsRecyclerView.ViewHolder{ TextViewtv; ImageViewiv; publicVh(ViewitemView){ super(itemView); itemView.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewview){ //点击了某个app的图标,用选择的app分享内容 Intentshare=newIntent(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,justifyouwannashareanimage. SharedPkgInfopi=sharePkgInfo.get(getAdapterPosition()); share.setClassName(pi.pkgName,pi.activityClassName); //share.setPackage(); startActivity(share); } }); } } //获取支持供享的包的信息 ListsharePkgInfo=newArrayList<>(); //要分享出去的文本放在这里 privateStringstrToShare=null; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); Intentintent=this.getIntent(); strToShare=intent.getStringExtra(EXTRA_STR_TO_SHARE); getAllSharePackages(); //将可共享的app图标都放在一个gridview中 RecyclerViewv=newRecyclerView(this); v.setPadding(16,16,16,16); GridLayoutManagerlm=newGridLayoutManager(this,4); v.setLayoutManager(lm); v.setAdapter(newRecyclerView.Adapter () { @Override publicVhonCreateViewHolder(ViewGroupparent,intviewType){ //必须创建新的viewholder LinearLayoutv=newLinearLayout(NShareActivity.this); v.setPadding(8,8,8,8); Vhvh=newVh(v); //先创建itemview:上面一个图标,下面一个文本 LinearLayout.LayoutParamslp=newLinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); v.setOrientation(LinearLayout.VERTICAL); v.setLayoutParams(lp); ImageViewimgv=newImageView(NShareActivity.this); imgv.setLayoutParams(newLinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, 120)); TextViewtv=newTextView(NShareActivity.this); tv.setGravity(Gravity.CENTER); v.addView(imgv); v.addView(tv); vh.tv=tv; vh.iv=imgv; returnvh; } @Override publicvoidonBindViewHolder(Vhholder,intposition){ //将视图与数据绑定 SharedPkgInfospi=sharePkgInfo.get(position); holder.tv.setText(spi.appName); holder.iv.setImageDrawable(spi.icon); } @Override publicintgetItemCount(){ returnsharePkgInfo.size(); } }); v.setBackgroundColor(Color.WHITE); this.setContentView(v); } //获取所有支持sendAction的包名和图片 voidgetAllSharePackages() { Intentshare=newIntent(android.content.Intent.ACTION_SEND); //分析网站地址的话用这个: //intent.setType("text/plain");//纯文本 share.setType("text/*"); //getsthelistofintentsthatcanbeloaded. List resInfo=getPackageManager().queryIntentActivities(share,0); if(!resInfo.isEmpty()){ for(ResolveInfoinfo:resInfo){ SharedPkgInfospi=newSharedPkgInfo(); 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); } } } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!