1.只要填写上正确的app_id,且引用上该工具类你就能实现分享到朋友圈和分享到微信。
2.需要到微信平台下载jar包,以及注册一个appid
import android.content.Context; import com.fortrun.client.config.AppConfig;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.SendMessageToWX;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.mm.sdk.openapi.WXMediaMessage;
import com.tencent.mm.sdk.openapi.WXTextObject; /**
* 分享到微信和朋友圈的帮助类
* @author yw-tony
*
*/
public class WeiXinUtils{
private WeiXinUtils(){}
private static WeiXinUtils instance;
public IWXAPI api;
public SendMessageToWX.Req req;
public static WeiXinUtils getInstance(){
synchronized (WeiXinUtils.class) {
if(instance == null){
instance = new WeiXinUtils();
}
}
return instance;
}
/**
* 初始化分享功能
* @param context
*
*/
public void init(Context context){
api = WXAPIFactory.createWXAPI(context, AppConfig.APP_ID, true);
api.registerApp(AppConfig.APP_ID);//将应用appid注册到微信
WXTextObject textObject = new WXTextObject();
textObject.text = "分享链接";//内容 WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = textObject;
msg.description = "内容描述";//描述 req = new SendMessageToWX.Req();
req.transaction = String.valueOf(System.currentTimeMillis());
req.message = msg; }
/**
* 分享消息到微信
* type 0:分享到微信
* type 1:分享到朋友圈
*/
public void sendToWeiXin(int type){
if(type == 1){
req.scene = SendMessageToWX.Req.WXSceneTimeline;//加上这一段代码即未分享到朋友圈,否则分享到微信
}
api.sendReq(req);
}
}