本文主要是介绍一键发圈,一键转发到微信朋友圈或者微信好友,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
转载地址:http://blog.csdn.net/zflove168/article/details/79216773
package com.derivative.client.util;import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;import com.derivative.client.R;import java.io.File;
import java.util.ArrayList;/*** 拉起微信,朋友圈功能类,支持单张图片,多张图片,文字*/
public class ShareUtils {PopupWindow popupWindow;Context context;private String path;//单张图片路径private String content;private Button btn;private Uri[] uris;//多张图片路径uri数组public ShareUtils(Context context, String content){this.context=context;// this.path=path;this.content=content;// this.btn=btn;showpop();}public void setUri(Uri[] uris){this.uris = uris;}public void setPath(String path){this.path = path;}private void showpop() {View view= LayoutInflater.from(context).inflate(R.layout.share_view, null);ImageView img_weixin= (ImageView) view.findViewById(R.id.share_weixin);ImageView img_pyq= (ImageView) view.findViewById(R.id.share_pengyouquan);popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT, true);popupWindow.setBackgroundDrawable(new BitmapDrawable()); // 点击返回按钮popwindow消失img_weixin.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (StringUtils.isWeixinAvilible(context)) {// 判断是否安装微信客户端// shareweixin(path);shareWXSomeImg(context,uris);// login(SHARE_MEDIA.WEIXIN);} else {ActivityUtil.showToast(context, "请安装微信客户端");}popupWindow.dismiss();}});img_pyq.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (StringUtils.isWeixinAvilible(context)) {// 判断是否安装微信客户端// shareweipyq(path,content);//拉起微信朋友圈带一张图片shareweipyqSomeImg(context,uris);//拉起微信朋友圈带多张图片// login(SHARE_MEDIA.WEIXIN);} else {ActivityUtil.showToast(context, "请安装微信客户端");}popupWindow.dismiss();}});popupWindow.showAtLocation( LayoutInflater.from(context).inflate(R.layout.activity_posterxq, null).findViewById(R.id.img_share), Gravity.BOTTOM, 0, 0);// 先设置popwindow的所有参数,最后再show}/*** 拉起微信好友发送单张图片* */private void shareweixin(String path){Uri uriToImage = Uri.fromFile(new File(path));Intent shareIntent = new Intent();//发送图片到朋友圈//ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");//发送图片给好友。ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");shareIntent.setComponent(comp);shareIntent.setAction(Intent.ACTION_SEND);shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);shareIntent.setType("image/jpeg");context.startActivity(Intent.createChooser(shareIntent, "分享图片"));}/*** 拉起微信朋友圈发送单张图片* */private void shareweipyq(String path,String content){Uri uriToImage = Uri.fromFile(new File(path));Intent shareIntent = new Intent();//发送图片到朋友圈ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");//发送图片给好友。
// ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");shareIntent.setComponent(comp);shareIntent.putExtra("Kdescription", content);shareIntent.setAction(Intent.ACTION_SEND);shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);shareIntent.setType("image/jpeg");context.startActivity(Intent.createChooser(shareIntent, "分享图片"));}/*** 拉起微信朋友圈发送多张图片* */private void shareweipyqSomeImg(Context context,Uri[] uri){Intent shareIntent = new Intent();//1调用系统分析shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//2添加图片数组ArrayList<Uri> imageUris = new ArrayList<>();for (int i = 0; i < uri.length; i++) {imageUris.add(uri[i]);}shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,imageUris);shareIntent.setType("image/*");//3指定选择微信ComponentName componentName = new ComponentName("com.tencent.mm","com.tencent.mm.ui.tools.ShareToTimeLineUI");shareIntent.setComponent(componentName);//4开始分享context.startActivity(Intent.createChooser(shareIntent,"分享图片"));}/*** 拉起微信发送多张图片给好友* */private void shareWXSomeImg(Context context,Uri[] uri){Intent shareIntent = new Intent();//1调用系统分析shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//2添加图片数组ArrayList<Uri> imageUris = new ArrayList<>();for (int i = 0; i < uri.length; i++) {imageUris.add(uri[i]);}shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,imageUris);shareIntent.setType("image/*");//3指定选择微信ComponentName componentName = new ComponentName("com.tencent.mm","com.tencent.mm.ui.tools.ShareImgUI");shareIntent.setComponent(componentName);//4开始分享context.startActivity(Intent.createChooser(shareIntent,"分享图片"));}
}
这篇关于一键发圈,一键转发到微信朋友圈或者微信好友的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!