Android工具類— 分享到QQ(QQ空間)、微信(朋友圈)
阿新 • • 發佈:2019-01-28
1、分享到QQ、QQ空間:
首先第一步,在騰訊開發者平臺註冊,新增應用,獲得APPID,然後下載SDK,匯入到工程中;然後,在AndroidManifest中註冊兩個Activity,程式碼如下:
<!--分享到QQ的Activity-->
<activity
android:name="com.tencent.tauth.AuthActivity"
android:noHistory="true"
android:launchMode="singleTask" >
<intent-filter >
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="tencent**********" />
</intent-filter>
</activity>
<activity android:name="com.tencent.connect.common.AssistActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:configChanges="orientation|keyboardHidden|screenSize"
/>
*其中<data android:scheme="tencent**********" />裡面的****就是你在開發者平臺申請的APPID*
然後在Activity中寫以下程式碼:
/**
* 分享圖片到 QQ 、QQ 空間
*/
public static void sharePicToQQ(Activity activity, int flag, String picPath) {
Tencent mTencent = Tencent.createInstance(APPID, activity.getApplicationContext());
ShareListener myListener = new ShareListener();
Bundle params = new Bundle();
params.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, picPath);
params.putString(QQShare.SHARE_TO_QQ_APP_NAME, activity.getString(R.string.app_name));
params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_IMAGE);
params.putInt(QQShare.SHARE_TO_QQ_EXT_INT, QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN);
params.putInt(QQShare.SHARE_TO_QQ_EXT_INT, flag);
mTencent.shareToQQ(activity, params, myListener);
}
/**
* 分享網址到QQ、QQ空間
*/
private void shareToQQ(Context context, int flag, String url, String imgurl, String title, String info) {
//flag 1是qq空間,0是好友,
Tencent mTencent = Tencent.createInstance(APPID, context.getApplicationContext());
ShareListener myListener = new ShareListener();
final Bundle params = new Bundle();
params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
params.putString(QQShare.SHARE_TO_QQ_TITLE, title);
params.putString(QQShare.SHARE_TO_QQ_SUMMARY, info);
params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, url);
params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, imgurl);
params.putString(QQShare.SHARE_TO_QQ_APP_NAME, String.valueOf(R.string.app_name));
params.putInt(QQShare.SHARE_TO_QQ_EXT_INT, flag);
mTencent.shareToQQ(Webview.this, params, myListener);
}
private class ShareListener implements IUiListener {
@Override
public void onCancel() {
ToastUtil.showToast(context, "分享取消");
}
@Override
public void onComplete(Object arg0) {
ToastUtil.showToast(context, "分享成功");
}
@Override
public void onError(UiError arg0) {
ToastUtil.showToast(context, "分享出錯");
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
ShareListener myListener = new ShareListener();
Tencent.onActivityResultData(requestCode, resultCode, data, myListener);
}
然後,在點選事件中呼叫:
//0代表QQ好友,1代表QQ空間
case R.id.rl_share_qq:
shareToQQ(context, 0, ArticleUrl, imgurl, title, info);
break;
case R.id.rl_share_qqzone:
shareToQQ(context, 1, ArticleUrl, imgurl, title, info);
break;
2、分享到微信、朋友圈:
首先第一步,在微信開發者平臺註冊,新增應用,獲得APPID,然後下載SDK,匯入到工程中;然後,可以寫一個靜態方法,在需要的地方呼叫:
/**
* 分享圖片到微信、朋友圈
*/
public static void shareImgToWechat(Context context, int flag, String path, String title, String info) {
String APP_ID = "";
final IWXAPI api = WXAPIFactory.createWXAPI(context.getApplicationContext(), APP_ID, true);
WXImageObject imgObj = new WXImageObject();
imgObj.setImagePath(path);
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = imgObj;
msg.description = info;
Bitmap bmp = BitmapFactory.decodeFile(path);
bmp.recycle();
msg.title = title;
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = "img" + String.valueOf(System.currentTimeMillis());
req.message = msg;
req.scene = flag;
api.sendReq(req);
}
/**
* 分享網址到微信、朋友圈
*/
public static void shareToWechat(Context context, int flag, String url, String title, String info) {
//flag 1是朋友圈,0是好友,
String APP_ID = "";
final IWXAPI api = WXAPIFactory.createWXAPI(context.getApplicationContext(), APP_ID, true);
api.registerApp(APP_ID);
if (!api.isWXAppInstalled()) {
ToastUtil.showToast(context, "您還未安裝微信客戶端");
return;
}
WXWebpageObject webpage = new WXWebpageObject();
webpage.webpageUrl = url;
WXMediaMessage msg = new WXMediaMessage(webpage);
msg.title = title;
msg.description = info;
Bitmap thumb = BitmapFactory.decodeResource(context.getResources(),
R.mipmap.ico_login_biglogo);
msg.setThumbImage(thumb);
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = String.valueOf(System.currentTimeMillis());
req.message = msg;
req.scene = flag;
api.sendReq(req);
}
點選事件裡面的呼叫程式碼:
case R.id.rl_share_wechat:
ShareUtil.shareToWechat(context, 0, ArticleUrl, title, info);
break;
case R.id.rl_share_wechatzone:
ShareUtil.shareToWechat(context, 1, ArticleUrl, title, info);
break;