1、申请注册你的appid
2、下载sdk
QQ: http://wiki.open.qq.com/wiki/mobile/SDK%E4%B8%8B%E8%BD%BD
微信:https://open.weixin.qq.com/cgi-bin/readtemplate?t=resource/app_download_android_tmpl&lang=zh_CN
3、将下载的文件
导入进工程下的app/libs/下,liba开头的的微信需要的,另外两个是QQ需要的
阅读官方文档QQ:
http://wiki.open.qq.com/wiki/mobile/API%E8%B0%83%E7%94%A8%E8%AF%B4%E6%98%8E
微信:
https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419317340&token=&lang=zh_CN
PS 微信需要安装签名生成工具输入包名申请。
4、在 AndroidManifest.xml 中配置
<activity>
<intent-filter>
<category android:name="android.intent.category.DEFAULT">
<category android:name="android.intent.category.BROWSABLE">
<data android:scheme="tencent你的appid">
</data></category></category></action></intent-filter>
</activity>
5、接下来就是代码,微信还需要在报名下指定文件夹中新建指定类名的activity
WXEntryActivity类:
public class WXEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {
private final String APP_ID= "你的appid";
private IWXAPI api;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 通过WXAPIFactory工厂,获取IWXAPI的实例
api = WXAPIFactory.createWXAPI(this, APP_ID, false);
api.registerApp(APP_ID);
api.handleIntent(getIntent(), this);
}
//微信发送消息给app,app接受并处理的回调函数
@Override
public void onReq(BaseReq req) {
}
//app发送消息给微信,微信返回的消息回调函数,根据不同的返回码来判断操作是否成功
@Override
public void onResp(BaseResp resp) {
int result = 0;
switch (resp.errCode) {
case BaseResp.ErrCode.ERR_OK:
result = R.string.errcode_success;
break;
case BaseResp.ErrCode.ERR_USER_CANCEL:
result = R.string.errcode_cancel;
break;
case BaseResp.ErrCode.ERR_AUTH_DENIED:
result = R.string.errcode_deny;
break;
default:
result = R.string.errcode_unknown;
break;
}
Toast.makeText(this, result, Toast.LENGTH_LONG).show();
finish();
}
}
ShareActivity类:
public class ShareActivity extends BaseActivity implements View.OnClickListener {
private final String W_APPID = "wxd70e5a411362e82b";
private IWXAPI api;
private final String Q_APPID = "1105648864";
private Tencent mTencent;
private Button quit;
private ImageView qZone;
private ImageView qq;
private ImageView weichat;
private ImageView weichatFriend;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.share_popup);
regToWx();
regToQQ();
setBaseActionbar();
initView();
}
private void regToQQ() {
// Tencent类是SDK的主要实现类,开发者可通过Tencent类访问腾讯开放的OpenAPI。
// 其中APP_ID是分配给第三方应用的appid,类型为String。
mTencent = Tencent.createInstance(Q_APPID, mContext);
}
private void regToWx() {
api = WXAPIFactory.createWXAPI(this, W_APPID, true);
api.registerApp(W_APPID);
}
private void initView() {
quit = (Button) findViewById(R.id.quit);
qZone = (ImageView) findViewById(R.id.qzone);
qq = (ImageView) findViewById(R.id.qq);
weichat = (ImageView) findViewById(R.id.weichat);
weichatFriend = (ImageView) findViewById(R.id.weichat_friend);
actionbarLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
quit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
qZone.setOnClickListener(this);
qq.setOnClickListener(this);
weichat.setOnClickListener(this);
weichatFriend.setOnClickListener(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
private void setBaseActionbar() {
setActionbarLeft(R.mipmap.pixel_ic_toolbar_back);
setActionbarTitle("分享", "SAHRE");
hideActionbarRight();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.qzone:
// mLog("点击QQ空间");
qq(true);
break;
case R.id.qq:
// mLog("点击QQ");
qq(false);
break;
case R.id.weichat:
// mLog("点击威信");
weiChat(0);
break;
case R.id.weichat_friend:
// mLog("点击朋友圈");
weiChat(1);
break;
default:
break;
}
}
private void qq(boolean flag) {
if (mTencent.isSessionValid() && mTencent.getOpenId() == null) {
mToast("您还未安装QQ", 0);
}
final Bundle params = new Bundle();
params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_APP);
params.putString(QQShare.SHARE_TO_QQ_TITLE, "要分享的标题");
params.putString(QQShare.SHARE_TO_QQ_SUMMARY, "要分享的摘要");
params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, "http://imgcache.qq.com/qzone/space_item/pre/0/66768.gif");
params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "测试应用222222");
if(flag){
params.putInt(QQShare.SHARE_TO_QQ_EXT_INT, QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN);
}
mTencent.shareToQQ(ShareActivity.this, params, new ShareListener());
}
// 0-分享给朋友 1-分享到朋友圈
private void weiChat(int flag) {
if (!api.isWXAppInstalled()) {
mToast("您还未安装微信", 0);
return;
}
//创建一个WXWebPageObject对象,用于封装要发送的Url
WXWebpageObject webpage = new WXWebpageObject();
webpage.webpageUrl = "http://www.mojichina.com/";
//创建一个WXMediaMessage对象
WXMediaMessage msg = new WXMediaMessage(webpage);
msg.title = "测试";
msg.description = "这是我做的一款天气类app,高端大气上档次,快来看看吧!";
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = String.valueOf(System.currentTimeMillis());//transaction字段用于唯一标识一个请求,这个必须有,否则会出错
req.message = msg;
//表示发送给朋友圈 WXSceneTimeline 表示发送给朋友 WXSceneSession
req.scene = flag == 0 ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline;
api.sendReq(req);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ShareListener myListener = new ShareListener();
Tencent.onActivityResultData(requestCode, resultCode, data, myListener);
}
private class ShareListener implements IUiListener {
@Override
public void onCancel() {
mToast("分享取消", 0);
}
@Override
public void onComplete(Object arg0) {
mToast("分享成功", 0);
}
@Override
public void onError(UiError arg0) {
mToast("分享出错", 0);
}
}
}
代码应该一目了然,actionbarLeft是BaseActivity中的,无视就行,接下来是几张截图,应该看的更明白。
QQ分享不出去,是因为appid的问题,大家可以去查一下。