Skip to content

Commit

Permalink
将监听器由接口变成了类,自动置空静态监听器
Browse files Browse the repository at this point in the history
  • Loading branch information
Kale committed Apr 1, 2017
1 parent 6a3ea10 commit be76b28
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 34 deletions.
54 changes: 31 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,43 @@ compile 'com.github.tianzhijiexian:ShareLoginLib:1.3.8'
### 登录、分享
```JAVA
// 登录
SsoLoginManager.login(this, SsoLoginType.XXX, new LoginListener() {
SsoLoginManager.login(this, SsoLoginType.XXX, new SsoLoginManager.LoginListener(){
@Override
public void onSuccess(String accessToken, String uId, long expiresIn, @Nullable String wholeData) {
super.onSuccess(accessToken, uId, expiresIn, wholeData); // must call super
}

public void onSuccess(String accessToken, String uId, long expiresIn, @Nullable String wholeJsonData) {}
@Override
public void onCancel() {
super.onCancel(); // must call super
}

public void onError(String errorMsg) {}

public void onCancel() {}
});
@Override
public void onError(String errorMsg) {
super.onError(errorMsg); // must call super
}
});


// 分享
SsoShareManager.share(MainActivity.this, SsoShareType.XXX
new ShareContentWebpage("title", "summary", "http://www.kale.com", mBitmap),
new ShareStateListener() {

public void onSuccess() {}

public void onCancel() {}

public void onError(String errorMsg) {}
});
new SsoShareManager.ShareStateListener(){
@Override
public void onSuccess() {
super.onSuccess(); // must call super
}

@Override
public void onCancel() {
super.onCancel(); // must call super
}

@Override
public void onError(String msg) {
super.onError(msg); // must call super
}
});

```

Expand Down Expand Up @@ -112,6 +128,7 @@ ShareLoginSDK.init(this, config);

## 重要说明

- 需要强制获取外部存储卡的权限,否则会拿不到分享的图片
- 签名后的app才可以进行测试
- 使用者要在第三方平台进行注册后才可测试
- 库作者不会提供任何和签名、密码、AppId等有关信息
Expand All @@ -124,17 +141,8 @@ ShareLoginSDK.init(this, config);
- 安装第三方应用,但第三方应用未登录
- 未开启不保留活动,并且第三方应用已经登录

## 已知的第三方SDK的bug(本lib无法解决)
1. 不能信任第三方的回调,操作后可能会得不到任何回调
2. 分享到了微信,用户留在了微信,那么就永远接收不到回调了
1. 如果没进行微博的登录,直接调用微博分享,有一定概率会出现分享失败
2. 分享途中通过通知消息进入别的app后,可能会因为内存不足等奇葩情况,你的应用被杀死,没有回调
3. 如果你手机中安装了微信,并且微信已经登录。直接从你的应用分享到微信是没有任何回调的,只有在你用微信登录你的应用(无论登录是否成功,取消也行)后,才能有回调
4. 当开启不保留活动后,有可能会出现界面的显示异常,这个和第三方的应用有密切关系,微博尤其明显

## 配置运行本demo的环境


如果你要运行该项目给出的demo,那么可以修改本地的`gradle.properties`文件,将下列信息修改成你自己的值。

```
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/com/liulishuo/engzo/LoginListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @author Kale
* @date 2016/4/5
*/
class LoginListener implements SsoLoginManager.LoginListener {
class LoginListener extends SsoLoginManager.LoginListener {

private static final String TAG = "LoginListener";

Expand All @@ -30,23 +30,26 @@ class LoginListener implements SsoLoginManager.LoginListener {

@Override
public void onSuccess(String accessToken, String userId, long expiresIn, String data) {
super.onSuccess(accessToken, userId, expiresIn, data);
Log.d(TAG, "accessToken = " + accessToken + "\nuid = " + userId + "\nexpires_in = " + expiresIn);
loadUserInfo(accessToken, userId);

String result = "登录成功";
Toast.makeText(activity, result, Toast.LENGTH_SHORT).show();
activity.handResult(result);
}

@Override
public void onError(String msg) {
super.onError(msg);
String result = "登录失败,失败信息:" + msg;
Toast.makeText(activity, result, Toast.LENGTH_SHORT).show();
activity.handResult(result);
}

@Override
public void onCancel() {
super.onCancel();
String result = "取消登录";
Toast.makeText(activity, result, Toast.LENGTH_SHORT).show();
activity.handResult(result);
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/liulishuo/engzo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ private void loadPicFromTempFile() {
public void onClick(View v) {
SsoShareManager.ShareStateListener mShareListener = new ShareListener(this);

if (SsoLoginManager.listener != null || SsoShareManager.listener != null) {
throw new RuntimeException("static listener leaked");
}

int i = v.getId();
switch (i) {
case R.id.QQ登录:
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/com/liulishuo/engzo/ShareListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @author Jack Tony
* @date 2015/10/23
*/
class ShareListener implements SsoShareManager.ShareStateListener {
class ShareListener extends SsoShareManager.ShareStateListener {

private MainActivity activity;

Expand All @@ -19,20 +19,23 @@ class ShareListener implements SsoShareManager.ShareStateListener {

@Override
public void onSuccess() {
super.onSuccess();
String result = "分享成功";
Toast.makeText(activity, result, Toast.LENGTH_SHORT).show();
activity.handResult(result);
}

@Override
public void onError(String msg) {
super.onError(msg);
String result = "分享失败,出错信息:" + msg;
Toast.makeText(activity, result, Toast.LENGTH_SHORT).show();
activity.handResult(result);
}

@Override
public void onCancel() {
super.onCancel();
String result = "取消分享";
Toast.makeText(activity, result, Toast.LENGTH_SHORT).show();
activity.handResult(result);
Expand Down
23 changes: 19 additions & 4 deletions lib/src/main/java/com/liulishuo/share/SsoLoginManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.CallSuper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

Expand Down Expand Up @@ -69,19 +70,33 @@ public static void recycle() {
SL_WeiXinHandlerActivity.wxRespListener = null;
}

public interface LoginListener {
public static class LoginListener {

/**
* @param accessToken 第三方给的一次性token,几分钟内会失效
* @param uId 用户的id
* @param expiresIn 过期时间
* @param wholeData 第三方本身返回的全部json数据
*/
void onSuccess(String accessToken, String uId, long expiresIn, @Nullable String wholeData);
@CallSuper
public void onSuccess(String accessToken, String uId, long expiresIn, @Nullable String wholeData) {
onComplete();
}

void onError(String msg);
@CallSuper
public void onError(String errorMsg) {
onComplete();
}

void onCancel();
@CallSuper
public void onCancel() {
onComplete();
}

@CallSuper
protected void onComplete() {
SsoLoginManager.recycle();
}
}

public interface WXLoginRespListener {
Expand Down
23 changes: 19 additions & 4 deletions lib/src/main/java/com/liulishuo/share/SsoShareManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.CallSuper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

Expand Down Expand Up @@ -92,13 +93,27 @@ private static void doShareSync(@NonNull Activity activity, @SsoShareType String
}
}

public interface ShareStateListener {
public static class ShareStateListener {

void onSuccess();
@CallSuper
public void onSuccess() {
onComplete();
}

void onCancel();
@CallSuper
public void onCancel() {
onComplete();
}

void onError(String msg);
@CallSuper
public void onError(String msg) {
onComplete();
}

@CallSuper
protected void onComplete() {
SsoShareManager.recycle();
}
}

public static void recycle() {
Expand Down

0 comments on commit be76b28

Please sign in to comment.