Android Sina authorization and sharing experience

Source: Internet
Author: User

Android Sina authorization and sharing

1. First, release a demo and jar package from the official documentation of Sina. The structure is as follows:

 

2. reference the sdk project in your own project. The reference method is as follows:

 

3. Public classes for sharing and authorization code calls are as follows:

 

Public class ShareUtils {public static final String APP_KEY = app Sina authorization id; // public static final String REDIRECT_URL = sina calls the network address; public static final String SCOPE = email, direct_messages_read, direct_messages_write, + response, friendships_groups_write, statuses_to_me_read, + response, + invitation_write; private iweibow.api mweibow.api = null; private Oauth2AccessToke N mAccessToken; private static ShareUtils instance; private static Context context; private WeiboAuth mWeiboAuth; private writable interface msharecils; public static ShareUtils getInstance (Context mcontext) {if (instance = null) instance = new ShareUtils (); context = mcontext; return instance;} // share text or image Methods public void shareMessage (String shareTxt, Drawable mDrawable) {// However, you need to apply for the permission to integrate and share in the attachment column. For details, see the Demo prompt mW. Eibow.api = WeiboShareSDK. createWeiboAPI (context, APP_KEY); if (mweibow.api. checkEnvironment (true) {mweibow.api. registerApp (); if (mweibow.api. isWeiboAppSupportAPI () {int supportApi = mWeiboShareAPI. getWeiboAppSupportAPI (); if (supportApi> = 10351/* ApiUtils. BUILD_INT_VER_2_2 */) {shareMultiMessage (shareTxt, mDrawable);} else {sendSingleMessage (shareTxt, mDrawable) ;}}// send a Sina authorized callback event pu Blic void setsharecall( partition interface msharecall) {this. msharecall= msharecall;} // use Sina to authorize public void onAuthSina () {// create a Weibo instance mAccessToken = AccessTokenKeeper. readAccessToken (context); mWeiboAuth = new WeiboAuth (context, APP_KEY, REDIRECT_URL, SCOPE); mWeiboAuth. anthorize (new AuthListener ();} public void shareMultiMessage (String shareTxt, Drawable mDrawable) {WeiboMultiMessage weiboMessage = new WeiboMultiMessage (); if (! ShareTxt. equals () {weiboMessage. textObject = getTextObj (shareTxt);} if (mDrawable! = Null) {weiboMessage. imageObject = getImageObj (mDrawable);} sendmultimessagetoweibo orequest request = new sendmultimessagetoweibo orequest (); request. transaction = String. valueOf (System. currentTimeMillis (); request. multiMessage = weiboMessage; mweibow.api. sendRequest (request);} private ImageObject getImageObj (Drawable mDrawable) {ImageObject imageObject = new ImageObject (); BitmapDrawable bitmapDrawabl E = (BitmapDrawable) mDrawable; imageObject. setImageObject (bitmapDrawable. getBitmap (); return imageObject;} private TextObject getTextObj (String shareTxt) {TextObject textObject = new TextObject (); textObject. text = shareTxt; return textObject;} private void sendSingleMessage (String shareTxt, Drawable mDrawable) {// 1. initialize a shared message on Weibo. // you can share a message in text, image, webpage, music, or video. WeiboMessage: weiboMessage = new WeiboMessage (); If (! ShareTxt. equals () {weiboMessage. mediaObject = getTextObj (shareTxt);} if (mDrawable! = Null) {weiboMessage. mediaObject = getImageObj (mDrawable);}/** if (hasVoice) {weiboMessage. mediaObject = getVoiceObj ();} * // 2. initialize a message request from a third party to Weibo: sendmessagetoweibo orequest request = new sendmessagetoweibo orequest (); // use transaction to uniquely identify a request. transaction = String. valueOf (System. currentTimeMillis (); request. message = weiboMessage; // 3. send a request message to Weibo, which calls the mweibow.api on the Weibo sharing interface. sendRequest (request);} class AuthListener implements WeiboAuthListener {@ Overridepublic void onComplete (Bundle values) {// parse TokenmAccessToken = Oauth2AccessToken from Bundle. parseAccessToken (values); if (mAccessToken. isSessionValid () {// display Token // updateTokenView (false); // Save the Token to sharedpreferencesmsharecils. onAuthListener (mAccessToken); AccessTokenKeeper. writeAccessToken (context, mAccessToken); Log. I (ZYN, updateTokenView ---> MAccessToken. getToken () + mAccessToken. getToken () + mAccessToken. getToken () + mAccessToken. getUid ();} else {// when your registered application signature is incorrect, the Code will be received. Make sure that the signature is correct. onAuthListener (null); String code = values. getString (code); String message =; if (! TextUtils. isEmpty (code) {message = message + Obtained the code: + code ;}}@ Overridepublic void onCancel () {}@ Overridepublic void onWeiboException (WeiboException e) {}}/*** displays the current Token information. ** @ Param hasExisted * check whether the token information is valid in the configuration file */private void updateTokenView (boolean hasExisted) {String date = new SimpleDateFormat (yyyy/MM/dd HH: mm: ss ). format (new java. util. date (mAccessToken. getExpiresTime (); String format = context. getString (R. string. weibosdk_demo_token_to_string_format_1); String message = String. format (format, mAccessToken. getToken (), date );}}


4. The code for creating a listener authorization callback event is as follows:

public interface ShareInterface {public void onAuthListener(Oauth2AccessToken token);}

5. Share the call method as follows:

ShareUtils.getInstance(mAct).shareMessage(share_txt, null);

6. The authorization call method is as follows:
ShareUtils shareUtils = ShareUtils. getInstance (this. getActivity ());

  shareUtils.setShareCalls(this);  shareUtils.onAuthSina();
@ Overridepublic void onAuthListener (Oauth2AccessToken token) {// TODO Auto-generated method stubtoken. getUid () Sina authorized user ID
Token. getToken () Sina authorized tokentoken. getExpiresTime () active time}




 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.