解决分享由于持有activity实例造成的内存泄露
This commit is contained in:
@ -45,6 +45,7 @@ import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@ -52,12 +53,20 @@ import java.util.List;
|
||||
* Created by khy on 2016/11/8.
|
||||
*/
|
||||
public class MessageShareUtils {
|
||||
|
||||
private static MessageShareUtils instance;
|
||||
private IWXAPI mIWXAPI;
|
||||
private Tencent mTencent;
|
||||
|
||||
private WeakReference<Activity> mActivity; // 用来关闭分享页面
|
||||
private Context mContext; // application context
|
||||
|
||||
private PopupWindow mPopupWindow;
|
||||
private Bitmap shareBm; //分享截图
|
||||
private String picName;
|
||||
private boolean isPlugin = false;
|
||||
|
||||
|
||||
private String[] mArrLabel = {"微信好友", "朋友圈", "QQ好友", "QQ空间", "保存"};
|
||||
private int[] arrLogo = {
|
||||
R.drawable.share_wechat_logo,
|
||||
R.drawable.share_wechatmoments_logo,
|
||||
@ -66,14 +75,6 @@ public class MessageShareUtils {
|
||||
R.drawable.share_save
|
||||
};
|
||||
|
||||
private String[] mArrLabel = {"微信好友", "朋友圈", "QQ好友", "QQ空间", "保存"};
|
||||
|
||||
private PopupWindow mPopupWindow;
|
||||
|
||||
private Context mContext;
|
||||
|
||||
//TODO 干掉activity,将context变成applicationcontext
|
||||
private Activity mActivity; // 用来关闭分享页面
|
||||
//QQ或者QQ空间分享回调处理
|
||||
public IUiListener QqShareListener = new IUiListener() {
|
||||
@Override
|
||||
@ -84,8 +85,9 @@ public class MessageShareUtils {
|
||||
@Override
|
||||
public void onError(UiError uiError) {
|
||||
// 单分享图片不支持显示未安装弹窗,手动调出
|
||||
if (!ShareUtils.isQQClientAvailable(mActivity.getApplication())) {
|
||||
new TDialog(mActivity,
|
||||
Activity activity = mActivity.get();
|
||||
if (activity != null && !ShareUtils.isQQClientAvailable(activity.getApplication())) {
|
||||
new TDialog(activity,
|
||||
"",
|
||||
"http://openmobile.qq.com/oauth2.0/m_jump_by_version",
|
||||
null,
|
||||
@ -100,6 +102,7 @@ public class MessageShareUtils {
|
||||
Utils.toast(mContext, R.string.share_cancel_hint);
|
||||
}
|
||||
};
|
||||
|
||||
// 适配快传成绩单分享
|
||||
private int contentSize;
|
||||
private int paddTop;
|
||||
@ -112,8 +115,7 @@ public class MessageShareUtils {
|
||||
private MessageShareUtils(Context context) {
|
||||
mTencent = Tencent.createInstance(Config.TENCENT_APPID, context); //初始化QQ分享
|
||||
mIWXAPI = WXAPIFactory.createWXAPI(context, Config.WECHAT_APPID); //初始化微信分享
|
||||
//TODO changed to application context
|
||||
mContext = context;
|
||||
mContext = context.getApplicationContext();
|
||||
}
|
||||
|
||||
public static MessageShareUtils getInstance(Context context) {
|
||||
@ -123,10 +125,6 @@ public class MessageShareUtils {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public IWXAPI getIWXAPI() {
|
||||
return mIWXAPI;
|
||||
}
|
||||
|
||||
public Tencent getTencent() {
|
||||
return mTencent;
|
||||
}
|
||||
@ -135,7 +133,7 @@ public class MessageShareUtils {
|
||||
this.shareBm = bitmap;
|
||||
this.picName = picName;
|
||||
this.shareType = shareType;
|
||||
this.mActivity = activity;
|
||||
this.mActivity = new WeakReference<>(activity);
|
||||
|
||||
if (shareType == 2) {
|
||||
contentSize = 75;
|
||||
@ -194,24 +192,16 @@ public class MessageShareUtils {
|
||||
mPopupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
|
||||
mPopupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
|
||||
|
||||
contentView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
contentView.setOnClickListener(v -> mPopupWindow.dismiss());
|
||||
|
||||
contentView.setOnKeyListener((v, keyCode, event) -> {
|
||||
if (keyCode == KeyEvent.KEYCODE_BACK
|
||||
&& event.getRepeatCount() == 0
|
||||
&& mPopupWindow != null
|
||||
&& mPopupWindow.isShowing()) {
|
||||
mPopupWindow.dismiss();
|
||||
}
|
||||
});
|
||||
|
||||
contentView.setOnKeyListener(new View.OnKeyListener() {
|
||||
@Override
|
||||
public boolean onKey(View v, int keyCode, KeyEvent event) {
|
||||
|
||||
if (keyCode == KeyEvent.KEYCODE_BACK
|
||||
&& event.getRepeatCount() == 0 && mPopupWindow != null
|
||||
&& mPopupWindow.isShowing()) {
|
||||
mPopupWindow.dismiss();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
@ -225,8 +215,12 @@ public class MessageShareUtils {
|
||||
params.putString(QQShare.SHARE_TO_QQ_APP_NAME, mContext.getString(R.string.app_name));
|
||||
params.putInt(QQShare.SHARE_TO_QQ_EXT_INT,
|
||||
QQShare.SHARE_TO_QQ_FLAG_QZONE_ITEM_HIDE);
|
||||
mTencent.shareToQQ(
|
||||
mActivity, params, QqShareListener);
|
||||
|
||||
Activity activity = mActivity.get();
|
||||
if (activity != null) {
|
||||
mTencent.shareToQQ(activity, params, QqShareListener);
|
||||
}
|
||||
|
||||
if (mPopupWindow == null) return;
|
||||
mPopupWindow.dismiss();
|
||||
}
|
||||
@ -241,8 +235,12 @@ public class MessageShareUtils {
|
||||
params.putString(QQShare.SHARE_TO_QQ_APP_NAME, mContext.getString(R.string.app_name));
|
||||
params.putInt(QQShare.SHARE_TO_QQ_EXT_INT,
|
||||
QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN);
|
||||
mTencent.shareToQQ(
|
||||
mActivity, params, QqShareListener);
|
||||
|
||||
Activity activity = mActivity.get();
|
||||
if (activity != null) {
|
||||
mTencent.shareToQQ(activity, params, QqShareListener);
|
||||
}
|
||||
|
||||
if (mPopupWindow == null) return;
|
||||
mPopupWindow.dismiss();
|
||||
}
|
||||
@ -430,38 +428,35 @@ public class MessageShareUtils {
|
||||
public void onBindViewHolder(final ViewHolder holder, int position) {
|
||||
holder.shareLogo.setImageResource(arrLogo[position]);
|
||||
holder.shareLabel.setText(mArrLabel[position]);
|
||||
holder.itemView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
switch (holder.getPosition()) {
|
||||
case 0:
|
||||
wechatSahre();
|
||||
if (shareType != 2) {
|
||||
holder.itemView.setOnClickListener(v -> {
|
||||
switch (holder.getPosition()) {
|
||||
case 0:
|
||||
wechatSahre();
|
||||
if (shareType != 2) {
|
||||
// activity.finish();
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
wechatMomentsSahre();
|
||||
if (shareType != 2) {
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
wechatMomentsSahre();
|
||||
if (shareType != 2) {
|
||||
// activity.finish();
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
qqSahre();
|
||||
break;
|
||||
case 3:
|
||||
qZoneSahre();
|
||||
break;
|
||||
case 4:
|
||||
String savePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictures/ghzhushou/";
|
||||
writeBitmap(savePath, "gh-" + new Date().getTime() + ".jpg", shareBm, true);
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
qqSahre();
|
||||
break;
|
||||
case 3:
|
||||
qZoneSahre();
|
||||
break;
|
||||
case 4:
|
||||
String savePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pictures/ghzhushou/";
|
||||
writeBitmap(savePath, "gh-" + new Date().getTime() + ".jpg", shareBm, true);
|
||||
|
||||
if (mPopupWindow == null) return;
|
||||
mPopupWindow.dismiss();
|
||||
if (mPopupWindow == null) return;
|
||||
mPopupWindow.dismiss();
|
||||
// activity.finish();
|
||||
break;
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user