Android呼叫系統分享和指定app分享-微信朋友圈圖文分享和qq分享
阿新 • • 發佈:2019-02-12
1.呼叫系統分享
根據每個手機的情況不同,開啟的分享面板內容也是不一樣的。其實本質上,分享的目標app是外掛化開發,這樣呼叫分享的時候,才能開啟對應的Activity,而不需要開啟一個完整的app程序。
- /**
- * 呼叫本地分享文字
- */
- privatevoid showLocationShare(int request) {
- Intent sendIntent = new Intent();
- sendIntent.setAction(Intent.ACTION_SEND);
-
sendIntent.putExtra(Intent.EXTRA_TEXT, "這是分享的內容!"
- sendIntent.setType("text/plain");
- startActivityForResult(sendIntent, request);
- }
2.指定app分享
首先要知道app的包名和Activity的名稱。通過以下方法獲得手機上全部可用於分享的app的包名和Activity名稱。
-
Intent share = new Intent(android.content.Intent.ACTION_SEND);
- PackageManager packageManager = getPackageManager();
- List<ResolveInfo> list=packageManager.queryIntentActivities(share, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
- for(ResolveInfo info:list){
-
MyUtils.log(""+info.activityInfo.packageName+"---"+info.activityInfo.name);
- }
- Intent sendIntent = new Intent();
- sendIntent.setAction(Intent.ACTION_SEND);
- sendIntent.putExtra(Intent.EXTRA_TEXT, shareTittle + "\n" + shareContent + "\n" + shareUrl);
- sendIntent.setType("text/plain");
- // sendIntent.setClassName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");//微信朋友
- // sendIntent.setClassName("com.tencent.mobileqq", "cooperation.qqfav.widget.QfavJumpActivity");//儲存到QQ收藏
- // sendIntent.setClassName("com.tencent.mobileqq", "cooperation.qlink.QlinkShareJumpActivity");//QQ面對面快傳
- // sendIntent.setClassName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.qfileJumpActivity");//傳給我的電腦
- sendIntent.setClassName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");//QQ好友或QQ群
- // sendIntent.setClassName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");//微信朋友圈,僅支援分享圖片
- startActivityForResult(sendIntent, QUN_QUEST);
3.Android系統分享-微信朋友圈分享
- Bitmap decodeRecource = BitmapFactory.decodeResource(getResources(), R.drawable.app_icon);
- File file = writeFileByBitmap2(decodeRecource);
- Uri img = Uri.fromFile(file);//獲得一張圖片的Uri
- Intent send = new Intent();
- <strong>send.setAction(Intent.ACTION_SEND)</strong>;
- send.putExtra(Intent.<strong>EXTRA_STREAM</strong>, img);
- send.setType("image/*");
- send.setClassName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");//微信朋友圈,僅支援分享圖片
- startActivityForResult(send, WX_QUEST);
- ArrayList<Uri> uriList = new ArrayList<Uri>();
- uriList.add(img);
- uriList.add(img);
- Intent send = new Intent();
- <span style="color:#ff6666;"><strong>send.setAction(Intent.ACTION_SEND_MULTIPLE);</strong></span>
- send.putExtra(Intent.EXTRA_STREAM, <strong>uriList</strong>);
- send.setType("image/*");
- send.setClassName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");//微信朋友圈,僅支援分享圖片
- startActivityForResult(send, WX_QUEST);
微信分享圖片+文字
用上面的方法新增圖片分享,如果要新增文字,再新增一句程式碼就可以
- send.putExtra("<span style="color:#ff0000;"><strong>Kdescription</strong></span>", shareContent+shareUrl);