您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關怎么在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); } } } }
關于怎么在Android應用中添加一個分享功能就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。