一、配置gradle,
build.gralde里,
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath "com.mob.sdk:MobSDK:2018.0319.1724"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
maven { url 'https://maven.google.com' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
app/build.gralde里
apply plugin: 'com.mob.sdk'
MobSDK {
appKey "2828000006f0"
appSecret "87e36afc13e6800000d0f7b2e06f7ce7"
permissions {//不需要的权限可以在这里移除
exclude "android.permission.RECEIVE_SMS", "android.permission.READ_SMS", "android.permission.READ_CONTACTS", "android.permission.BLUETOOTH"
}
SMSSDK {}//没用到短信验证码,可以去掉
ShareSDK {
devInfo {
SinaWeibo {
appKey "2970001936"
appSecret "cdd18f10b883efbc00068ff49e97c96e"
callbackUri "http://www.fanghenet.com/App1.html"
shareByAppClient true
}
Wechat {
appId "wxd1216f5dc7000282"
appSecret "a679ac45000000a2864f32c795f6d175"
userName "gh_a8664870fa5d"
path "pages/index/index.html?id=1"
withShareTicket true
miniprogramType 2
bypassApproval false
enable true
}
QQ {
appId "101500039"
appKey "6e56707462000873ad81e30fa8824def"
shareByAppClient true
bypassApproval false
enable true
}
QZone {
id 3
sortId 3
appId "101500039"
appKey "6e567074000f1873ad81e30fa8824def"
shareByAppClient true
bypassApproval false
enable true
}
WechatMoments {
id 5
sortId 5
appId "wxd1216f0007d79282"
appSecret "a679ac450006a0a2864f32c795f6d175"
bypassApproval false
enable true
}
}
}
}
二、在application里初始化,
MobSDK.init(this);
三、分享图片代码:
public class ResultActivity {
public final static int SUCCESSREQUESTCODE=0x01111;//请求成功代号
public final static int FAILURERREQUESTCODE=0x01112;//请求出错代号
public final static int CANCELERREQUESTCODE=0x01113;//请求取消代号
private void toShare(View view) {
Platform.ShareParams params = null;
String appName="";
String platName="";
switch (view.getId()) {
case R.id.ll_qq_friend:
params=new QQ.ShareParams();
appName="手机QQ";
platName= QQ.NAME;
break;
case R.id.ll_wx_center:
params = new WechatMoments.ShareParams();
params.setShareType(Platform.SHARE_IMAGE);
appName="手机微信";
platName= WechatMoments.NAME;
break;
case R.id.ll_wx_friend:
params = new Wechat.ShareParams();
params.setShareType(Platform.SHARE_IMAGE);
appName="手机微信";
platName= Wechat.NAME;
break;
}
shareToThirdPlat(appName, imagePath, platName, params);
}
public void shareToThirdPlat(String appName, String pic_url, String platFormName, Platform.ShareParams params) {
Platform platform = ShareSDK.getPlatform(platFormName);
if (!platform.isClientValid()) {
ToastUtil.showToastShort(getString(R.string.not_install_app, appName));
return;
}
showLoading("正在分享..");
params.setImagePath(pic_url);
platform.setPlatformActionListener(new PlatformActionListener() {
@Override
public void onComplete(Platform platform, int i, HashMap<String, Object> hashMa) {
handler.sendEmptyMessage(ConstantConfig.SUCCESSREQUESTCODE);//当平台为新浪时,不允许在监听事件里进行与界面有关的操作,包括Tost
}
@Override
public void onError(Platform platform, int i, Throwable throwable) {
handler.sendEmptyMessage(ConstantConfig.FAILURERREQUESTCODE);
}
@Override
public void onCancel(Platform platform, int i) {
handler.sendEmptyMessage(ConstantConfig.CANCELERREQUESTCODE);
}
});
platform.share(params);
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SUCCESSREQUESTCODE:
Toast.makeText(getApplicationContext(),"分享成功",Toast.LENGTH_LONG).show();
break;
case FAILURERREQUESTCODE:
Toast.makeText(getApplicationContext(),"分享失败",Toast.LENGTH_LONG).show();
break;
case CANCELERREQUESTCODE:
Toast.makeText(getApplicationContext(),"分享取消",Toast.LENGTH_LONG).show();
break;
}
}
};
}
四、第三方登录
public class UserInfoBindFragment {
public final static int SUCCESSREQUESTCODE=0x01111;//请求成功代号
public final static int FAILURERREQUESTCODE=0x01112;//请求出错代号
public final static int CANCELERREQUESTCODE=0x01113;//请求取消代号
private Handler handler;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.arg2 == SUCCESSREQUESTCODE) {
try {
JSONObject object = new JSONObject((String) msg.obj);
//获取到的授权信息
} catch (JSONException e) {
e.printStackTrace();
}
} else if (msg.arg2 == FAILURERREQUESTCODE) {
ToastUtil.showToastShort(msg.obj.toString());
}
}
};
// thirdLogin(Wechat.NAME,"手机微信",1);
// thirdLogin(QQ.NAME,"手机QQ",2);
}
private void thirdLogin(String platName, String appName, final int platType) {
final Platform plat = ShareSDK.getPlatform(platName);
if (!plat.isClientValid()) {
ToastUtil.showToastShort(getString(R.string.not_install_app, appName));
return;
}
showLoading("正在获取授权...");
plat.setPlatformActionListener(new PlatformActionListener() {
@Override
public void onComplete(Platform platform, int i, final HashMap<String, Object> hashMap) {
Message ms = new Message();
ms.arg2 = SUCCESSREQUESTCODE;
ms.arg1 = platType;
ms.obj = platform.getDb().exportData();
handler.sendMessage(ms);
plat.removeAccount(true);
}
@Override
public void onError(Platform platform, int i, Throwable throwable) {
Message ms = new Message();
ms.arg2 = FAILURERREQUESTCODE;
ms.obj = throwable.toString();
handler.sendMessage(ms);
}
@Override
public void onCancel(Platform platform, int i) {
Message ms = new Message();
ms.arg2 = FAILURERREQUESTCODE;
ms.obj = "您已取消第三方授权";
handler.sendMessage(ms);
}
});
plat.SSOSetting(false);
plat.authorize();
}
}
五、
public class BindMobleActivity extends BaseAppActivity {
private static Handler handler;
private static EventHandler eventHandler;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler = new Handler() {
public void handleMessage(Message msg) {
hideLoading();
if (msg.arg2 == SMSSDK.RESULT_COMPLETE) {
if (msg.arg1 == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
UserInfo user = BaseApplication.appContext.mUser;
user.setF_Mobile(phone);
BindUser(user);
} else if (msg.arg1 == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
startTick(60);
ToastUtil.showToastShort("验证码已发送");
} else {
ToastUtil.showToastShort(msg.obj.toString());
}
} else if (msg.obj instanceof Throwable) {
Throwable error = (Throwable) msg.obj;
try {
JSONObject object = new JSONObject(error.getLocalizedMessage());
ToastUtil.showToastShort(object.getString("detail"));
} catch (JSONException e) {
e.printStackTrace();
ToastUtil.showToastShort(error.getLocalizedMessage());
}
} else {
ToastUtil.showToastShort(msg.obj.toString());
}
}
};
eventHandler = new EventHandler() {
@Override
public void afterEvent(int event, int result, Object data) {
Message msg = new Message();
msg.arg1 = event;
msg.arg2 = result;
msg.obj = data;
handler.sendMessage(msg);
}
};
SMSSDK.registerEventHandler(eventHandler); // 注册回调监听接口
}
public void onGetVerity(View view) {
SMSSDK.getVerificationCode(短信模板id, "86", phone);//输入的手机号
}
public void onLogin(View view) {
SMSSDK.submitVerificationCode("86", phone, code);//输入的手机号与验证码
}
@Override
public void onDestroy() {
super.onDestroy();
SMSSDK.unregisterEventHandler(eventHandler); // 注册回调监听接口
}
}
切忌,如果配置了两个及以上的分享,会导致分享返回之后,弹出系统选择框。