欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现的新浪微博分享代码实例

程序员文章站 2024-03-02 11:34:28
weibo.java 复制代码 代码如下:         @override  &n...

weibo.java

复制代码 代码如下:

        @override
        protected void oncreate(bundle savedinstancestate) {
                 super.oncreate(savedinstancestate);
                 setcontentview(r.layout.user_center);
                 // 创建微博实例
                mweiboauth = new weiboauth(this, constants.app_key,
                                 constants.redirect_url, constants.scope);
                 // 创建微博分享接口实例
                mweiboshareapi = weibosharesdk.createweiboapi(this, constants.app_key);
                 // 当 activity 被重新初始化时(该 activity 处于后台时,可能会由于内存不足被杀掉了),
                // 需要调用 iweiboshareapi#handleweiboresponse} 来接收微博客户端返回的数据。
                // 执行成功,返回 true,并调用 iweibohandler.response#onresponse};
                // 失败返回 false,不调用上述回调
                if (savedinstancestate != null) {
                         mweiboshareapi.handleweiboresponse(getintent(), this);
                 }
        }
 /**
          * 检查用户是否安装新浪微博
         */
         public void isnotinstall() {
                 try {
                         // 检查微博客户端环境是否正常,如果未安装微博,弹出对话框询问用户下载微博客户端
                        if (mweiboshareapi.checkenvironment(true)) {
                                 // 注册第三方应用 到微博客户端中,注册成功后该应用将显示在微博的应用列表中。
                                // 但该附件栏集成分享权限需要合作申请,详情请查看 demo 提示
                                mweiboshareapi.registerapp();
                                startsinashare();
                         }
                 } catch (weiboshareexception e) {
                         e.printstacktrace();
                         toast.maketext(usercenter.this, e.getmessage(), toast.length_long)
                                         .show();
                 }
                 if (dialog != null) {
                         dialog.dismiss();
                 }
         }
         /**
          * 微博认证授权回调类。 1. sso 授权时,需要在 #onactivityresult} 中调用
         * ssohandler#authorizecallback} 后, 该回调才会被执行。 2. 非 sso
          * 授权时,当授权结束后,该回调就会被执行。 当授权成功后,请保存该 access_token、expires_in、uid 等信息到
         * sharedpreferences 中。
         */
         class authlistener implements weiboauthlistener {
                 @override
                 public void oncomplete(bundle values) {
                         // 从 bundle 中解析 token
                         maccesstoken = oauth2accesstoken.parseaccesstoken(values);
                         if (maccesstoken.issessionvalid()) {
                                 // 保存 token 到 sharedpreferences
                                 accesstokenkeeper.writeaccesstoken(usercenter.this,
                                                 maccesstoken);
                                 sendmessage();
                         }
                 }
                 @override
                 public void oncancel() {
                 }
                 @override
                 public void onweiboexception(weiboexception e) {
                         toast.maketext(usercenter.this,
                                         "auth exception : " + e.getmessage(), toast.length_long)
                                         .show();
                 }
         }
         /**
          * 新浪微博用户授权
         */
         public void startsinashare() {
                 mssohandler = new ssohandler(usercenter.this, mweiboauth);
                 // 从 sharedpreferences 中读取上次已保存好 accesstoken 等信息,
                maccesstoken = accesstokenkeeper.readaccesstoken(this);
                 // 如果token有效,则直接调用发送微博
                if (maccesstoken.issessionvalid()) {
                         sendmessage();
                 } else {
                         mssohandler.authorize(new authlistener());
                 }
         }
         /**
          * @see activity#onnewintent}
          */
         @override
         protected void onnewintent(intent intent) {
                 super.onnewintent(intent);
                 // 从当前应用唤起微博并进行分享后,返回到当前应用时,需要在此处调用该函数
                // 来接收微博客户端返回的数据;执行成功,返回 true,并调用
                // iweibohandler.response#onresponse};失败返回 false,不调用上述回调
                mweiboshareapi.handleweiboresponse(intent, this);
         }
         /**
          * 当 sso 授权 activity 退出时,该函数被调用。
         *
          * @see activity#onactivityresult}
          */
         @override
         protected void onactivityresult(int requestcode, int resultcode, intent data) {
                 super.onactivityresult(requestcode, resultcode, data);
                 // sso 授权回调
                // 重要:发起 sso 登陆的 activity 必须重写 onactivityresult
                 if (mssohandler != null) {
                         mssohandler.authorizecallback(requestcode, resultcode, data);
                 }
         }
         /**
          * 接收微客户端博请求的数据。 当微博客户端唤起当前应用并进行分享时,该方法被调用。
         *
          * @param baserequest
          *            微博请求数据对象
         * @see iweiboshareapi#handleweiborequest}
          */
         @override
         public void onresponse(baseresponse baseresp) {
                 switch (baseresp.errcode) {
                 case wbconstants.errorcode.err_ok:
                         if (publicfun.sharecondition()) {
                                 gainbobi();
                         } else {
                                 toast.maketext(this, r.string.share_success, toast.length_long)
                                                 .show();
                         }
                         break;
                 case wbconstants.errorcode.err_cancel:
                         break;
                 case wbconstants.errorcode.err_fail:
                         toast.maketext(this, r.string.errcode_deny, toast.length_long)
                                         .show();
                         break;
                 }
                 if (dialog != null) {
                         dialog.dismiss();
                 }
         }
         /**
          * 第三方应用发送请求消息到微博,唤起微博分享界面。
          * @see #sendmultimessage} 或者 #sendsinglemessage}
          */
         private void sendmessage() {
                 if (mweiboshareapi.isweiboappsupportapi()) {
                         sendmultimessage();
                 } else {
                         toast.maketext(this, r.string.sina_share_hint, toast.length_short)
                                         .show();
                 }
         }
         /**
          * 第三方应用发送请求消息到微博,唤起微博分享界面。 注意:当
         * iweiboshareapi#getweiboappsupportapi()} >= 10351 时,支持同时分享多条消息,
         *
          * @param hastext
          *            分享的内容是否有文本
         * @param hasimage
          *            分享的内容是否有图片
         * @param haswebpage
          *            分享的内容是否有网页
         */
         private void sendmultimessage() {
                 // 1. 初始化微博的分享消息
                weibomultimessage weibomessage = new weibomultimessage();
                 weibomessage.textobject = gettextobj();
                 // 用户可以分享其它媒体资源(网页、音乐、视频、声音中的一种)
                weibomessage.mediaobject = getwebpageobj();
                 // 2. 初始化从第三方到微博的消息请求
                sendmultimessagetoweiborequest request = new sendmultimessagetoweiborequest();
                 // 用transaction唯一标识一个请求
                request.transaction = string.valueof(system.currenttimemillis());
                 request.multimessage = weibomessage;
                 // 3. 发送请求消息到微博,唤起微博分享界面
                mweiboshareapi.sendrequest(request);
                 // 记录分享日志
                publicfun.sendshareapplog(usercenter.this,
                                 getresources().getstring(r.string.micro_blog));
                 if (dialog != null) {
                         dialog.dismiss();
                 }
         }
         /**
          * 创建文本消息对象。
          * @return 文本消息对象。
         */
         private textobject gettextobj() {
                 textobject textobject = new textobject();
                 textobject.text = getresources().getstring(r.string.share_content);
                 return textobject;
         }
         /**
          * 创建多媒体(网页)消息对象。
          * @return 多媒体(网页)消息对象。
         */
         private webpageobject getwebpageobj() {
                 webpageobject mediaobject = new webpageobject();
                 mediaobject.actionurl = getstring(r.string.share_url);
                 mediaobject.identify = utility.generateguid();
                 mediaobject.title = getresources().getstring(r.string.share_title);
                 mediaobject.description = getstring(r.string.share_content);
                 bitmap bmp = bitmapfactory.decoderesource(getresources(),
                                 r.drawable.icon);
                 mediaobject.setthumbimage(bmp);
                 return mediaobject;
         }
/**
 * 该类定义了微博授权时所需要的参数。
 * @author sina
 * @since 2013-10-07
 */
 public class accesstokenkeeper {
     private static final string preferences_name = "com_weibo_sdk_android";
     private static final string key_uid           = "uid";
     private static final string key_access_token  = "access_token";
     private static final string key_expires_in    = "expires_in";
     /**
      * 保存 token 对象到 sharedpreferences。
     *
      * @param context 应用程序上下文环境
     * @param token   token 对象
     */
     public static void writeaccesstoken(context context, oauth2accesstoken token) {
         if (null == context || null == token) {
             return;
         }
         sharedpreferences pref = context.getsharedpreferences(preferences_name, context.mode_append);
         editor editor = pref.edit();
         editor.putstring(key_uid, token.getuid());
         editor.putstring(key_access_token, token.gettoken());
         editor.putlong(key_expires_in, token.getexpirestime());
         editor.commit();
     }
     /**
      * 从 sharedpreferences 读取 token 信息。
     *
      * @param context 应用程序上下文环境
     *
      * @return 返回 token 对象
     */
     public static oauth2accesstoken readaccesstoken(context context) {
         if (null == context) {
             return null;
         }
         oauth2accesstoken token = new oauth2accesstoken();
         sharedpreferences pref = context.getsharedpreferences(preferences_name, context.mode_append);
         token.setuid(pref.getstring(key_uid, ""));
         token.settoken(pref.getstring(key_access_token, ""));
         token.setexpirestime(pref.getlong(key_expires_in, 0));
         return token;
     }
     /**
      * 清空 sharedpreferences 中 token信息。
     *
      * @param context 应用程序上下文环境
     */
     public static void clear(context context) {
         if (null == context) {
             return;
         }
         sharedpreferences pref = context.getsharedpreferences(preferences_name, context.mode_append);
         editor editor = pref.edit();
         editor.clear();
         editor.commit();
     }
 }

以上所述就是本文的全部内容了,希望对大家熟练掌握java有所帮助。