原文:https://www.zybuluo.com/Tyhj/note/730547
最近用到支持第三方登录和分享,之前其实也做过了,使用的是友盟的SDK,所以这次想试试Mob的,毕竟有免费的那个短信验证
首先说一下那个分享吧:
很简单,直接用sdk自带的,相当于一个dialog就可以用了,界面也不差。但是要去每一个平台去创建应用,申请APPKey和AppSecret,没什么难的,就是别忘了添加代码混淆规则。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| private void showShare() { OnekeyShare oks = new OnekeyShare(); oks.disableSSOWhenAuthorize(); oks.setTitle("聚宝籍"); oks.setTitleUrl(getString(R.string.url_share_app)); oks.setText(getString(R.string.its_signature)); oks.setImageUrl(getString(R.string.logo)); oks.setUrl(getString(R.string.url_share_app)); oks.setSite("聚宝籍"); oks.setSiteUrl(getString(R.string.url_share_app)); oks.show(this); }
|
对于登录就麻烦了
首先要去微信申请那个登录权限,要Money的。
然后注意一点是,登录回调反馈
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <!-- 微信分享回调 --> <activity android:name=".wxapi.WXEntryActivity" android:configChanges="keyboardHidden|orientation|screenSize" android:exported="true" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar" /> //对于QQ <activity android:name="com.mob.tools.MobUIShell" android:configChanges="keyboardHidden|orientation|screenSize" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:windowSoftInputMode="stateHidden|adjustResize"> <intent-filter> //重点在这里,这个要改 <data android:scheme="tencent1106041134" /> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.BROWSABLE" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> <!-- 调用新浪原生SDK,需要注册的回调activity --> <intent-filter> <action android:name="com.sina.weibo.sdk.action.ACTION_SDK_REQ_ACTIVITY" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
|
然后就是授权和取消授权的问题,已授权的话不会跳到授权那个页面去的,所以每次授权的时候判断一下,如果已授权的话就撤销一下授权。对于微信还必须以正式签名打包安装才可以成功。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| weixinfd = ShareSDK.getPlatform(Login.this,Wechat.NAME); weixinfd.setPlatformActionListener(this); if(weixinfd.isAuthValid()){ weixinfd.removeAccount(true); } weixinfd.SSOSetting(false); weixinfd.showUser(null); @Override public void onComplete(Platform platform, int i, HashMap<String, Object> res) { String name=platform.getDb().getUserName(); String avatar=platform.getDb().getUserIcon(); String unionid = (String) res.get("unionid"); } @Override public void onError(Platform platform, int i, Throwable throwable) { toast("失败"); } @Override public void onCancel(Platform platform, int i) { Log.e("取消","xxx"); }
|
其中那个反馈只有onCancel在Ui线程中吧,其他的都是在子线程中返回的。