转载请注明出处:http://blog.csdn.net/u010214991/article/details/48049389
最近在做分享功能,主要是实现QQ和微信分享,打算把自己实现过程跟大家分享一下,也是本人第一次发博客。好了,废话不多说,直接正题。
我把各种分享封装到一个类ShareUtil中,打算通过调用这个类里面的方法来实现分享,不过在使用这个类之前,我们必须首先进行一系列配置和准备工作。
一、注册官网帐号并登陆,审核应用并通过,下载签名工具获取你的应用的签名并填写到官方网站应用签名中
1、在分享之前,我们必须到相关官方开放平台申请创建我们的应用,填写应用的包名等相关信息并通过审核,通过审核后我们会得到应用的APPID和APPSECRET;这里的APPID有重大用处,我们后面的分享要用到。还有要注意,目前微信分享必须要先下载签名工具,直接在官方网站那下载,用来获取你的APP签名,做法是下载签名工具完后安装到手机,你必须已经在你的手机安装你的APP,打开签名工具输入你的应用包名,会得到该应用的签名(一大串字符),然后填写到官方网站中你的应用的签名中,注意你手机上的应用跟网站上申请的应用包名要一致,签名也要一致。
以下是相关平台的注册网址:
腾讯开放平台,地址 http://open.qq.com/
微信开放平台,地址 https://open.weixin.qq.com/
2、下载分享要用到的相关jar包,导入你工程中的libs文件中(直接复制粘贴进去)
QQ分享所需jar(懒人通道)
到http://wiki.open.qq.com/wiki/mobile/SDK%E4%B8%8B%E8%BD%BD中下载Android SDK后解压,将jar文件内的mta-sdk-1.6.2.jar和open_sdk_r5276.jar文件拷贝到你工程的libs文件中。
微信分享所需jar(懒人通道)
到https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419319167&token=&lang=zh_CN下载android开发工具包,解压后将libs文件中的libammsdk.jar拷贝到你的工程libs文件中。
导入这些包之后,你就可调用QQ分享和微信分享相关接口了。
二、AndroidManifest.xml文件配置
1、在你的工程的AndroidManifest.xml配置文件中添加如下代码
2、添加权限
三、ShareUtil工具类(完整代码如下:)
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import com.tencent.connect.share.QQShare;
import com.tencent.connect.share.QzoneShare;
import com.tencent.mm.sdk.modelmsg.SendMessageToWX;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;
import com.tencent.mm.sdk.modelmsg.WXWebpageObject;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
/**
*
* @author _H_JY
* 2015-8-27下午4:26:35
*
* 分享工具类:可以分享到微信好友、微信收藏、微信朋友圈、QQ好友、QQ空间、短信
*/
public class ShareUtil {
public static final String WX_APP_ID = "wxa44a945e29e96f94";//改成你在微信开放平台审核通过的appID
public static final String QQ_APP_ID = "1101799954";//改成你在QQ开放平台审核通过的appID
private IWXAPI iwxapi;
private Tencent tencent;
public ShareUtil() {
super();
// TODO Auto-generated constructor stub
}
/**要分享必须先注册(微信)*/
public void regToWX(Context context) {
iwxapi = WXAPIFactory.createWXAPI(context, WX_APP_ID, true);
iwxapi.registerApp(WX_APP_ID);
}
/**要分享必须先注册(QQ)*/
public void regToQQ(Context context) {
tencent = Tencent.createInstance(QQ_APP_ID, context);
}
public IWXAPI getIwxapi() {
return iwxapi;
}
public void setIwxapi(IWXAPI iwxapi) {
this.iwxapi = iwxapi;
}
public Tencent getTencent() {
return tencent;
}
public void setTencent(Tencent tencent) {
this.tencent = tencent;
}
public String getWxAppId() {
return WX_APP_ID;
}
public String getQqAppId() {
return QQ_APP_ID;
}
/**分享到短信*/
public Intent sendSMS(String description) {
Uri smsToUri = Uri.parse("smsto:");
Intent sendIntent = new Intent(Intent.ACTION_VIEW, smsToUri);
//sendIntent.putExtra("address", "123456"); // 电话号码,这行去掉的话,默认就没有电话
sendIntent.putExtra("sms_body", description);
sendIntent.setType("vnd.android-dir/mms-sms");
return sendIntent;
}
/**分享到微信好友*/
public void shareToWXSceneSession(String url,String shareTitle,String description){
WXWebpageObject webpageObject = new WXWebpageObject();
webpageObject.webpageUrl = url;
WXMediaMessage mWxMediaMessage = new WXMediaMessage(webpageObject);
mWxMediaMessage.title = shareTitle;
mWxMediaMessage.description = description;
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = String.valueOf(System.currentTimeMillis());//transaction字段用于唯一标识一个请求
req.message = mWxMediaMessage;
req.scene = SendMessageToWX.Req.WXSceneSession;
iwxapi.sendReq(req);
}
/**分享到微信收藏*/
public void shareToWXSceneFavorite(String url,String shareTitle,String description){
WXWebpageObject webpageObject = new WXWebpageObject();
webpageObject.webpageUrl = url;
WXMediaMessage wxMediaMessage = new WXMediaMessage(webpageObject);
wxMediaMessage.title = shareTitle;
wxMediaMessage.description = description;
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = String.valueOf(System.currentTimeMillis());
req.message = wxMediaMessage;
req.scene = SendMessageToWX.Req.WXSceneFavorite;
iwxapi.sendReq(req);
}
/**分享到微信朋友圈*/
public void shareToWXSceneTimeline(String url,String shareTitle,String description,String imageUrl){
WXWebpageObject webpageObject = new WXWebpageObject();
webpageObject.webpageUrl = url;
WXMediaMessage mediaMessage = new WXMediaMessage(webpageObject);
mediaMessage.title = shareTitle;
mediaMessage.description = description;
Bitmap bitmap = BitmapFactory.decodeFile(imageUrl);
Bitmap thumBmp = Bitmap.createScaledBitmap(bitmap, 150, 150, true);//图片大小有限制,太大分享不了
mediaMessage.thumbData = Tools.getBitmapByte(thumBmp);
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = String.valueOf(System.currentTimeMillis());
req.message = mediaMessage;
req.scene = SendMessageToWX.Req.WXSceneTimeline;
iwxapi.sendReq(req);
}
/**分享到QQ好友*/
public void shareToQQ(Activity activity,String url,String shareTitle,String description,IUiListener uiListener){
Bundle qqParams = new Bundle();
qqParams.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
qqParams.putString(QQShare.SHARE_TO_QQ_TITLE, shareTitle);
qqParams.putString(QQShare.SHARE_TO_QQ_SUMMARY, description);
qqParams.putString(QQShare.SHARE_TO_QQ_TARGET_URL, url);
//qqParams.putString(QQShare.SHARE_TO_QQ_APP_NAME, "APP名称");
tencent.shareToQQ(activity, qqParams, uiListener);
}
/**分享到QQ空间*/
public void shareToQzone(Activity activity,String url,String imageUrl,String shareTitle,String description,IUiListener uiListener){
Bundle qzoneParams = new Bundle();
qzoneParams.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE, QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
qzoneParams.putString(QzoneShare.SHARE_TO_QQ_TITLE, shareTitle);//必填
qzoneParams.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, description);
qzoneParams.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, url);//必填
ArrayList imageUrlList =new ArrayList();
imageUrlList.add(imageUrl);
qzoneParams.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, imageUrlList);
tencent.shareToQzone(activity, qzoneParams, uiListener);
}
}
ShareUtil shareUtil = new ShareUtil();
shareUtil.regToWX(context);//向微信终端注册appID
shareUtil.regToQQ(context);//向QQ终端注册appID
shareUtil.shareToSinaWeiBo(getActivity(), mShareTitle, description,TEST_IMAGE);
shareUtil.shareToWXSceneSession(url, mShareTitle, description);
shareUtil.shareToWXSceneFavorite(url, mShareTitle, description);
shareUtil.shareToWXSceneTimeline(url, mShareTitle, description, TEST_IMAGE);
shareUtil.shareToQQ(getActivity(), url, mShareTitle, description, new BaseUiListener());
shareUtil.shareToQzone(getActivity(), url, TEST_IMAGE, mShareTitle, description, new BaseUiListener());
startActivity(shareUtil.sendSMS(description));
请注意,上面的QQ分享方法代码中最后一个参数是要在你当前的Activity中实现回调监听的,我在我的Activity中写了这个类供参考,并且还要在这个Activity中复写onActivityResult方法,否则无法回调到:
private class BaseUiListener implements IUiListener {//QQ和Qzone分享回调
@Override
public void onCancel() {
// TODO Auto-generated method stub
Toast.makeText(context, "分享取消", Toast.LENGTH_SHORT).show();
}
@Override
public void onComplete(Object arg0) {
// TODO Auto-generated method stub
Toast.makeText(context, "分享成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(UiError arg0) {
// TODO Auto-generated method stub
Toast.makeText(context, "分享失败", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
shareUtil.getTencent().onActivityResultData(requestCode, resultCode, data, new BaseUiListener());
}
四、微信分享回调
微信分享必须新建一个包,在该包内新建一个类供微信分享回调用,建议在你的工程包中新建一个包,比如你的包名是com.example.test,那么新建的包名称为:
com.example.test.wxapi,在该包中新建一个类名称为WXEntryActivity,我把我写的这个类的代码也贴出来供大家参考:
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
/** 微信客户端回调activity示例 */
public class WXEntryActivity extends Activity implements IWXAPIEventHandler{
private Context context;
private IWXAPI iwxapi;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
context = this;
iwxapi = WXAPIFactory.createWXAPI(this, "wxa44a945e29e96f94", false);
iwxapi.handleIntent(getIntent(), this);
}
@Override
public void onReq(BaseReq req) {//发送请求时调用
// TODO Auto-generated method stub
}
@Override
public void onResp(BaseResp resp) {
// TODO Auto-generated method stub
switch (resp.errCode) {
case BaseResp.ErrCode.ERR_OK:
//分享成功
Toast.makeText(context, "分享成功", Toast.LENGTH_SHORT).show();
this.finish();
break;
case BaseResp.ErrCode.ERR_USER_CANCEL:
//分享取消
Toast.makeText(context, "分享取消", Toast.LENGTH_SHORT).show();
this.finish();
break;
case BaseResp.ErrCode.ERR_AUTH_DENIED:
//分享拒绝
Toast.makeText(context, "分享拒绝", Toast.LENGTH_SHORT).show();
this.finish();
break;
}
}
}
好了,本文到这里也该结束了,由于本人水平有限,可能有疏漏或者错误之处,还请各位读者发现问题并指正,探讨交流,第一次发博客,谢谢大家的支持!