解决分享由于持有activity实例造成的内存泄露
This commit is contained in:
@ -48,6 +48,7 @@ import com.tencent.tauth.Tencent;
|
||||
import com.tencent.tauth.UiError;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ -95,9 +96,9 @@ public class ShareUtils {
|
||||
|
||||
private ShareType mShareType;
|
||||
|
||||
private Activity mActivity;
|
||||
private WeakReference<Activity> mActivity;
|
||||
|
||||
private Context mContext;
|
||||
private Context mContext; // application context
|
||||
//QQ或者QQ空间分享回调处理
|
||||
public IUiListener QqShareListener = new IUiListener() {
|
||||
@Override
|
||||
@ -120,8 +121,7 @@ public class ShareUtils {
|
||||
mTencent = Tencent.createInstance(Config.TENCENT_APPID, context); //初始化QQ分享
|
||||
mIWXAPI = WXAPIFactory.createWXAPI(context, Config.WECHAT_APPID); //初始化微信分享
|
||||
WbSdk.install(context, new AuthInfo(context, Config.WEIBO_APPKEY, "http://www.sina.com", SCOPE));
|
||||
// FIXME 此处严重泄露,把Activity Context 存为static?????
|
||||
mContext = context;
|
||||
mContext = context.getApplicationContext();
|
||||
}
|
||||
|
||||
public static ShareUtils getInstance(Context context) {
|
||||
@ -149,17 +149,17 @@ public class ShareUtils {
|
||||
|
||||
public void showShareWindows(Activity activity, View view, String url, String icon, String shareTitle, String shareSummary, ShareType shareType) {
|
||||
if (activity.isFinishing()) return;
|
||||
this.mActivity = activity;
|
||||
this.mActivity = new WeakReference<>(activity);
|
||||
this.shareIcon = icon;
|
||||
this.shareUrl = url;
|
||||
this.mSummary = shareSummary;
|
||||
this.mTitle = shareTitle;
|
||||
this.mShareType = shareType;
|
||||
|
||||
View contentView = View.inflate(mActivity, R.layout.share_popup_layout, null);
|
||||
View contentView = View.inflate(activity, R.layout.share_popup_layout, null);
|
||||
contentView.setFocusable(true);
|
||||
contentView.setFocusableInTouchMode(true);
|
||||
RecyclerView shareRecyclerView = (RecyclerView) contentView.findViewById(R.id.share_rv);
|
||||
RecyclerView shareRecyclerView = contentView.findViewById(R.id.share_rv);
|
||||
|
||||
shareRecyclerView.setPadding(DisplayUtils.dip2px(mContext, 20), DisplayUtils.dip2px(mContext, 10), DisplayUtils.dip2px(mContext, 20), 0);
|
||||
|
||||
@ -198,24 +198,16 @@ public class ShareUtils {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
contentView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
contentView.setOnClickListener(v -> popupWindow.dismiss());
|
||||
|
||||
contentView.setOnKeyListener((v, keyCode, event) -> {
|
||||
if (keyCode == KeyEvent.KEYCODE_BACK
|
||||
&& event.getRepeatCount() == 0
|
||||
&& popupWindow != null
|
||||
&& popupWindow.isShowing()) {
|
||||
popupWindow.dismiss();
|
||||
}
|
||||
});
|
||||
|
||||
contentView.setOnKeyListener(new View.OnKeyListener() {
|
||||
@Override
|
||||
public boolean onKey(View v, int keyCode, KeyEvent event) {
|
||||
|
||||
if (keyCode == KeyEvent.KEYCODE_BACK
|
||||
&& event.getRepeatCount() == 0 && popupWindow != null
|
||||
&& popupWindow.isShowing()) {
|
||||
popupWindow.dismiss();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
@ -241,7 +233,10 @@ public class ShareUtils {
|
||||
params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, shareIcon);
|
||||
params.putString(QQShare.SHARE_TO_QQ_APP_NAME, mContext.getString(R.string.app_name));
|
||||
|
||||
mTencent.shareToQQ(mActivity, params, QqShareListener);
|
||||
Activity activity = mActivity.get();
|
||||
if (activity != null) {
|
||||
mTencent.shareToQQ(activity, params, QqShareListener);
|
||||
}
|
||||
|
||||
if (mShareType != ShareType.shareGh) {
|
||||
popupWindow.dismiss();
|
||||
@ -384,7 +379,11 @@ public class ShareUtils {
|
||||
params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, imageUrls);
|
||||
params.putString(QzoneShare.SHARE_TO_QQ_APP_NAME, mContext.getString(R.string.app_name));
|
||||
|
||||
mTencent.shareToQzone(mActivity, params, QqShareListener);
|
||||
Activity activity = mActivity.get();
|
||||
if (activity != null) {
|
||||
mTencent.shareToQzone(activity, params, QqShareListener);
|
||||
}
|
||||
|
||||
if (mShareType != ShareType.shareGh) {
|
||||
popupWindow.dismiss();
|
||||
}
|
||||
@ -435,9 +434,19 @@ public class ShareUtils {
|
||||
|
||||
//新浪微博分享
|
||||
private void sinaWeiboShare() {
|
||||
Intent intent = WeiBoShareActivity.getWeiboshareIntent(mContext,
|
||||
shareUrl, shareIcon, mTitle, mSummary, mShareType.toString());
|
||||
mContext.startActivity(intent);
|
||||
|
||||
Activity activity = mActivity.get();
|
||||
if (activity != null) {
|
||||
Intent intent = WeiBoShareActivity.getWeiboshareIntent(
|
||||
activity,
|
||||
shareUrl,
|
||||
shareIcon,
|
||||
mTitle,
|
||||
mSummary,
|
||||
mShareType.toString());
|
||||
activity.startActivity(intent);
|
||||
}
|
||||
|
||||
|
||||
if (mShareType != ShareType.shareGh) {
|
||||
popupWindow.dismiss();
|
||||
@ -508,53 +517,50 @@ public class ShareUtils {
|
||||
public void onBindViewHolder(final ViewHolder holder, int position) {
|
||||
holder.shareLogo.setImageResource(arrLogo[position]);
|
||||
holder.shareLabel.setText(arrLabel[position]);
|
||||
holder.itemView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
switch (holder.getPosition()) {
|
||||
case 0:
|
||||
wechatShare();
|
||||
break;
|
||||
case 1:
|
||||
wechatMomentsShare();
|
||||
break;
|
||||
case 2:
|
||||
qqShare();
|
||||
break;
|
||||
case 3:
|
||||
qZoneShare();
|
||||
break;
|
||||
case 4:
|
||||
sinaWeiboShare();
|
||||
break;
|
||||
case 5:
|
||||
shortMessageShare();
|
||||
break;
|
||||
case 6:
|
||||
if (mShareType == ShareType.askInvite) {
|
||||
copyLink(mTitle + " - 光环助手" + shareUrl);
|
||||
} else if (mShareType == ShareType.askNormal) {
|
||||
copyLink(shareUrl);
|
||||
} else if (mShareType != ShareType.shareGh) {
|
||||
copyLink(shareUrl);
|
||||
} else {
|
||||
try {
|
||||
Intent data = IntentUtils.getEmailToGHIntent();
|
||||
mContext.startActivity(data);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Utils.toast(mContext, "设备邮件服务异常,分享失败");
|
||||
}
|
||||
holder.itemView.setOnClickListener(v -> {
|
||||
switch (holder.getPosition()) {
|
||||
case 0:
|
||||
wechatShare();
|
||||
break;
|
||||
case 1:
|
||||
wechatMomentsShare();
|
||||
break;
|
||||
case 2:
|
||||
qqShare();
|
||||
break;
|
||||
case 3:
|
||||
qZoneShare();
|
||||
break;
|
||||
case 4:
|
||||
sinaWeiboShare();
|
||||
break;
|
||||
case 5:
|
||||
shortMessageShare();
|
||||
break;
|
||||
case 6:
|
||||
if (mShareType == ShareType.askInvite) {
|
||||
copyLink(mTitle + " - 光环助手" + shareUrl);
|
||||
} else if (mShareType == ShareType.askNormal) {
|
||||
copyLink(shareUrl);
|
||||
} else if (mShareType != ShareType.shareGh) {
|
||||
copyLink(shareUrl);
|
||||
} else {
|
||||
try {
|
||||
Intent data = IntentUtils.getEmailToGHIntent();
|
||||
mContext.startActivity(data);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Utils.toast(mContext, "设备邮件服务异常,分享失败");
|
||||
}
|
||||
break;
|
||||
case 7:
|
||||
if (mShareType != ShareType.shareGh) {
|
||||
popupWindow.dismiss();
|
||||
} else {
|
||||
copyLink("推荐光环助手,绿色安全的手游加速助手:" + shareUrl);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 7:
|
||||
if (mShareType != ShareType.shareGh) {
|
||||
popupWindow.dismiss();
|
||||
} else {
|
||||
copyLink("推荐光环助手,绿色安全的手游加速助手:" + shareUrl);
|
||||
}
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user