@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); // 从当前应用唤起微博并进行分享后,返回到当前应用时,需要在此处调用该函数 // 来接收微博客户端返回的数据;执行成功,返回 true,并调用 // {@link IWeiboHandler.Response#onResponse};失败返回 false,不调用上述回调 mWeiboShareAPI.handleWeiboResponse(intent, this); }
public void shareWebMessage( String shareUrl, String title, String description, Drawable mDrawable) { WeiboMultiMessage weiboMessage = new WeiboMultiMessage(); if (mDrawable == null) mDrawable = context.getResources().getDrawable(R.drawable.ic_launcher); BitmapDrawable bitmapDrawable = (BitmapDrawable) mDrawable; Bitmap shareBitmap = null; try { shareBitmap = bitmapDrawable.getBitmap(); if (shareBitmap == null) shareBitmap = ((BitmapDrawable) context.getResources().getDrawable(R.drawable.ic_launcher)) .getBitmap(); else shareBitmap = zoomImg(shareBitmap, 100); weiboMessage.mediaObject = getWebpageObj(shareUrl, title, description, shareBitmap); weiboMessage.textObject = getTextObj(title); SendMultiMessageToWeiboRequest request = new SendMultiMessageToWeiboRequest(); request.transaction = String.valueOf(System.currentTimeMillis()); request.multiMessage = weiboMessage; mWeiboShareAPI.sendRequest(request); } catch (Exception e) { } finally { if (shareBitmap != null) { // shareBitmap.recycle(); shareBitmap = null; } } }
private void sendSingleMessage(String shareTxt, Drawable mDrawable) { WeiboMessage weiboMessage = new WeiboMessage(); if (!shareTxt.equals("")) { weiboMessage.mediaObject = getTextObj(shareTxt); } if (mDrawable != null) { weiboMessage.mediaObject = getImageObj(mDrawable); } SendMessageToWeiboRequest request = new SendMessageToWeiboRequest(); request.transaction = String.valueOf(System.currentTimeMillis()); request.message = weiboMessage; mWeiboShareAPI.sendRequest(request); }
public void shareMessage( String shareUrl, String title, String description, Drawable mDrawable, int type) { try { mWeiboShareAPI = WeiboShareSDK.createWeiboAPI(act, APP_KEY); if (!mWeiboShareAPI.isWeiboAppInstalled()) { mWeiboShareAPI.registerWeiboDownloadListener( new IWeiboDownloadListener() { @Override public void onCancel() { Toast.makeText( act, act.getResources().getString(R.string.weibosdk_demo_cancel_download_weibo), Toast.LENGTH_SHORT) .show(); } }); } if (mWeiboShareAPI.checkEnvironment(true)) { mWeiboShareAPI.registerApp(); if (mWeiboShareAPI.isWeiboAppSupportAPI()) { int supportApi = mWeiboShareAPI.getWeiboAppSupportAPI(); if (supportApi >= 10351 /* ApiUtils.BUILD_INT_VER_2_2 */) { if (type == this.Share_Text_Type) { shareMultiMessage(title, mDrawable); } else if (type == this.Share_Webpage_Type) { shareWebMessage(shareUrl, title, description, mDrawable); } } else { if (type == this.Share_Text_Type) { sendSingleMessage(title, mDrawable); } else if (type == this.Share_Webpage_Type) { shareWebMessage(shareUrl, title, description, mDrawable); } } } } } catch (Exception e) { e.printStackTrace(); } }
/** 注意:SsoHandler 仅当 SDK 支持 SSO 时有效 */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /** 创建微博 SDK 接口实例 */ mWeiboShareAPI = WeiboShareSDK.createWeiboAPI(this, Constants.APP_KEY); /** 获取授权 */ mAuthInfo = new AuthInfo(this, Constants.APP_KEY, Constants.REDIRECT_URL, Constants.SCOPE); mSsoHandler = new SsoHandler(MainActivity.this, mAuthInfo); /** 判断微博是否安装 */ findViewById(R.id.iswbinstall) .setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { Toast.makeText( getBaseContext(), String.valueOf(mWeiboShareAPI.isWeiboAppInstalled()), Toast.LENGTH_LONG) .show(); } }); /** 打开微博 */ findViewById(R.id.openwb) .setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { Toast.makeText( getBaseContext(), String.valueOf(mWeiboShareAPI.launchWeibo(MainActivity.this)), Toast.LENGTH_LONG) .show(); } }); /** * 还可以进行一下判断测试 boolean isInstalledWeibo = mWeiboShareAPI.isWeiboAppInstalled(); int * supportApiLevel = mWeiboShareAPI.getWeiboAppSupportAPI(); */ /** 注册到微博 */ findViewById(R.id.inittowb) .setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { Toast.makeText( getBaseContext(), String.valueOf(mWeiboShareAPI.registerApp()), Toast.LENGTH_LONG) .show(); } }); /** 分享一条为本消息到微博 注意要用正式版本的key */ findViewById(R.id.shareText) .setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { TextObject object = new TextObject(); object.text = "叶落无声撒"; WeiboMessage message = new WeiboMessage(); message.mediaObject = object; SendMessageToWeiboRequest request = new SendMessageToWeiboRequest(); request.transaction = String.valueOf(System.currentTimeMillis()); request.message = message; mWeiboShareAPI.sendRequest(MainActivity.this, request); } }); /** 获取授权 */ findViewById(R.id.auth) .setOnClickListener( new OnClickListener() { @Override public void onClick(View v) { mSsoHandler.authorizeClientSso(new AuthListener()); } }); if (savedInstanceState != null) { mWeiboShareAPI.handleWeiboResponse(getIntent(), this); } }