Android 分享功能的实现代码
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); } } } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
赞 (0)