package com.gh.gamecenter; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.widget.Toast; import com.facebook.common.references.CloseableReference; import com.facebook.datasource.DataSource; import com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber; import com.facebook.imagepipeline.image.CloseableImage; import com.gh.common.util.AccessTokenKeeper; import com.gh.common.util.ImageUtils; import com.gh.common.util.ShareUtils; import com.gh.common.util.Utils; import com.sina.weibo.sdk.api.ImageObject; import com.sina.weibo.sdk.api.TextObject; import com.sina.weibo.sdk.api.WebpageObject; import com.sina.weibo.sdk.api.WeiboMultiMessage; import com.sina.weibo.sdk.api.share.BaseResponse; import com.sina.weibo.sdk.api.share.IWeiboHandler; import com.sina.weibo.sdk.api.share.IWeiboShareAPI; import com.sina.weibo.sdk.api.share.SendMultiMessageToWeiboRequest; import com.sina.weibo.sdk.api.share.WeiboShareSDK; import com.sina.weibo.sdk.auth.AuthInfo; import com.sina.weibo.sdk.auth.Oauth2AccessToken; import com.sina.weibo.sdk.auth.WeiboAuthListener; import com.sina.weibo.sdk.constant.WBConstants; import com.sina.weibo.sdk.exception.WeiboException; import com.sina.weibo.sdk.utils.Utility; /** * Created by khy on 2016/11/23. * * 微博分享 */ public class WeiBoShareActivity extends Activity implements IWeiboHandler.Response{ private String shareUrl; private String shareGameName; private String shareIcon; private String shareNewsTitle; // shareNewsTitle不为空就是新闻分享,否则是游戏分享 private boolean isPlugin; private boolean ispopupWindow; private IWeiboShareAPI mWeiboShareAPI; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bundle extras = getIntent().getExtras(); shareUrl = extras.getString("shareUrl"); shareGameName = extras.getString("shareGameName"); shareIcon = extras.getString("shareIcon"); shareNewsTitle = extras.getString("shareNewsTitle"); isPlugin = extras.getBoolean("isPlugin"); ispopupWindow = extras.getBoolean("ispopupWindow"); Utils.toast(this, "分享跳转中..."); mWeiboShareAPI = WeiboShareSDK.createWeiboAPI(this, "1723629218"); mWeiboShareAPI.registerApp(); weiboLoadBitMap(shareIcon); } protected void onNewIntent(Intent intent) { super.onNewIntent(intent); mWeiboShareAPI.handleWeiboResponse(intent, this); //当前应用唤起微博分享后,返回当前应用 } @Override public void onResponse(BaseResponse baseResponse) { switch (baseResponse.errCode) { case WBConstants.ErrorCode.ERR_OK: Toast.makeText(this, "分享成功", Toast.LENGTH_SHORT).show(); break; case WBConstants.ErrorCode.ERR_CANCEL: Toast.makeText(this, "分享取消", Toast.LENGTH_SHORT).show(); break; case WBConstants.ErrorCode.ERR_FAIL: Toast.makeText(this, "分享失败", Toast.LENGTH_SHORT).show(); break; } finish(); } private void weiboLoadBitMap( String iconUrl){ ImageUtils.getInstance().display(this, iconUrl, new BaseBitmapDataSubscriber() { @Override protected void onNewResultImpl(Bitmap bitmap) { Utils.log("分享获取bitmap成功,准备分享"); Bitmap bgBitmap = ShareUtils.getInstance(getApplication()).addBackGround(bitmap); TextObject textObject = new TextObject(); if (ispopupWindow) { if (shareNewsTitle != null){ textObject.text = shareNewsTitle + "@光环助手"; }else { if (isPlugin){ textObject.text ="向你推荐:" + shareGameName + "(光环加速版)" + " @光环助手 "; }else { textObject.text ="向你推荐:" + shareGameName + " @光环助手 " ; } } } else { textObject.text = "这个App可以下载各种热门卡牌手游的加速版,绿色安全,超级省心,做日常效率提高3-5倍!不用肝的感觉真好! @光环助手"; } ImageObject imageObject = new ImageObject(); imageObject.setImageObject(bgBitmap);//设置缩略图。 注意:最终压缩过的缩略图大小不得超过 32kb。 WebpageObject webObject = new WebpageObject(); webObject.identify = Utility.generateGUID(); webObject.title = ""; webObject.description = "光环助手"; webObject.defaultText = "光环助手"; webObject.actionUrl = shareUrl; webObject.setThumbImage(bgBitmap); WeiboMultiMessage weiboMessage = new WeiboMultiMessage();//初始化微博的分享消息 weiboMessage.textObject = textObject; weiboMessage.imageObject = imageObject; weiboMessage.mediaObject = webObject; SendMultiMessageToWeiboRequest request = new SendMultiMessageToWeiboRequest(); request.transaction = String.valueOf(System.currentTimeMillis()); request.multiMessage = weiboMessage; AuthInfo authInfo = new AuthInfo(WeiBoShareActivity.this, "1723629218", "https://api.weibo.com/oauth2/default.html", ""); Oauth2AccessToken accessToken = AccessTokenKeeper.readAccessToken(WeiBoShareActivity.this); String token = ""; if (accessToken != null) { token = accessToken.getToken(); } mWeiboShareAPI.sendRequest(WeiBoShareActivity.this, request, authInfo, token, new WeiboAuthListener() { // 微博web端 @Override public void onWeiboException( WeiboException arg0 ) { } @Override public void onComplete( Bundle bundle ) { // Oauth2AccessToken newToken = Oauth2AccessToken.parseAccessToken(bundle); // AccessTokenKeeper.writeAccessToken(getApplicationContext(), newToken); // Toast.makeText(getApplicationContext(), "onAuthorizeComplete token = " + newToken.getToken(), 0).show(); } @Override public void onCancel() { } }); } @Override protected void onFailureImpl(DataSource> dataSource) { Utils.log("分享获取bitmap失败"); } }); } }