一、前面部分引用官方文档,按步骤添加ShareSDK到自己的项目中
申请ShareSDK的appkey的流程
ShareSDK深受大家喜爱,看下亲爱的用户做的详细说明:Studio集成
进入ShareSDK解压目录,打开“Share SDK for Android”目录,可以找到“QuickIntegrater.jar”,这个就是快速集成ShareSDK的工具
首先确定您已经安装了JDK,并且正确配置了JAVA_HOME和PATH系统变量,此时您可以双击QuickIntegrater启动程序
同样需要先确保已经正确配置了JDK,之后启动终端,cd进入“Share SDK for Android”目录,输入: java -jar QuickIntegrater.jar 可启动QuickIntegrater
正确输入项目的名称和包名,勾选需要的集成的平台,然后点击“确定”。
QuickIntegrater会自行产生一个文件夹,复制此文件夹中所有文件到您的项目中覆盖即可。
下面清单文件的配置是全部的,没有的平台就是不需要配置
(注意: tencent后面的appid要保持和您配置的QQ的appid一致)
在您的代码中调用此方法,即可打开一键分享功能进行分享
二、第三方登录实现
在步骤一完成后进行如下操作
//1.威信的
//微信登录
//测试时,需要打包签名;sample测试时,用项目里面的demokey.keystore
//打包签名apk,然后才能产生微信的登录
//Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
//authorize(wechat);
//2.新浪微博
//新浪微博
Platform sina = ShareSDK.getPlatform(SinaWeibo.NAME);
authorize(sina);
//执行授权,获取用户信息
//文档:http://wiki.mob.com/Android_%E8%8E%B7%E5%8F%96%E7%94%A8%E6%88%B7%E8%B5%84%E6%96%99
private void authorize(Platform plat) {
if (plat == null) {
//popupOthers();
return;
}
plat.setPlatformActionListener(this);
//关闭SSO授权
plat.SSOSetting(true);
plat.showUser(null);
}
@Override
public void onCancel(Platform platform, int action) {
// TODO Auto-generated method stub
if (action == Platform.ACTION_USER_INFOR) {
handler.sendEmptyMessage(MSG_AUTH_CANCEL);
}
}
@Override
public void onComplete(Platform platform, int action,
HashMap res) {
// TODO Auto-generated method stub
if (action == Platform.ACTION_USER_INFOR) {
Message msg = new Message();
msg.what = MSG_AUTH_COMPLETE;
msg.obj = new Object[] {platform.getName(), res};
handler.sendMessage(msg);
}
}
@Override
public void onError(Platform arg0, int action, Throwable t) {
// TODO Auto-generated method stub
if (action == Platform.ACTION_USER_INFOR) {
handler.sendEmptyMessage(MSG_AUTH_ERROR);
}
t.printStackTrace();
}
实现方法:
@SuppressWarnings("unchecked")
public boolean handleMessage(Message msg) {
switch(msg.what) {
case MSG_AUTH_CANCEL: {
//取消授权
Toast.makeText(NewsDetailActivity.this, R.string.auth_cancel, Toast.LENGTH_SHORT).show();
} break;
case MSG_AUTH_ERROR: {
//授权失败
Toast.makeText(NewsDetailActivity.this, R.string.auth_error, Toast.LENGTH_SHORT).show();
} break;
case MSG_AUTH_COMPLETE: {
//授权成功
Toast.makeText(NewsDetailActivity.this, R.string.auth_complete, Toast.LENGTH_LONG).show();
Object[] objs = (Object[]) msg.obj;
String platform = (String) objs[0];
HashMap res = (HashMap) objs[1];
// if (signupListener != null && signupListener.onSignin(platform, res)) {
// SignupPage signupPage = new SignupPage();
// signupPage.setOnLoginListener(signupListener);
// signupPage.setPlatform(platform);
// signupPage.show(activity, null);
// }
doLogined(platform);
} break;
case MSG_SMSSDK_CALLBACK:
//{
// if (msg.arg2 == SMSSDK.RESULT_ERROR) {
// Toast.makeText(activity, "操作失败", Toast.LENGTH_SHORT).show();
// } else {
// switch (msg.arg1) {
// case SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE: {
// if(msgLoginDlg != null && msgLoginDlg.isShowing()){
// msgLoginDlg.dismiss();
// }
// Toast.makeText(activity, "提交验证码成功", Toast.LENGTH_SHORT).show();
// Message m = new Message();
// m.what = MSG_AUTH_COMPLETE;
// m.obj = new Object[] {"SMSSDK", (HashMap) msg.obj};
// handler.sendMessage(m);
// } break;
// case SMSSDK.EVENT_GET_VERIFICATION_CODE:{
// Toast.makeText(activity, "验证码已经发送", Toast.LENGTH_SHORT).show();
// } break;
// }
// }
//}
break;
}
return false;
}
private void doLogined(String platform) {
// TODO Auto-generated method stub
mPlatform=ShareSDK.getPlatform(platform);
String gender = "";
if(platform != null){
gender = mPlatform.getDb().getUserGender();
if(gender.equals("m")){
// userInfo.setUserGender(UserInfo.Gender.BOY);
gender = "男";
}else{
//userInfo.setUserGender(UserInfo.Gender.GIRL);
gender = "女";
}
// userInfo.setUserIcon(platform.getDb().getUserIcon());
// userInfo.setUserName(platform.getDb().getUserName());
// userInfo.setUserNote(platform.getDb().getUserId());
Toast.makeText(getApplicationContext(), gender+"/"+mPlatform.getDb().getUserName()+"/"+mPlatform.getDb().getUserId(), 4000).show();
}
// tvUserName.setText(userInfo.getUserName());
// tvUserGender.setText(gender);
// tvUserNote.setText("USER ID : " + userInfo.getUserNote());
}