标签:分享组件
应用中经常使用到分享组件,当然,很多第三方都提供了社交分享,像ShareSDK、友盟都有很好的集成。
今天记录一下集成的细节:
①无论分享到哪个平台,都需要到这个平台注册相应的应用。
②以分享到微信、朋友圈、微博为例:
/** * */ public class Constant { // 微信 public static final String AppID = "wx9117e18be87a2aca"; public static final String AppSecret = "1ed5744aa7258808567873a2aa764409"; // 这里是sina开发的appkey 交大移动图书馆 public static final String APP_KEY = "2253710460"; public static final String REDIRECT_URL = "http://blog.csdn.net/leokelly001"; public static final String SCOPE = "email,direct_messages_read,direct_messages_write," + "friendships_groups_read,friendships_groups_write,statuses_to_me_read," + "follow_app_official_microblog," + "invitation_write";// 应用申请的高级权限 }
package com.leo.xjtulib.scanbook.common; import java.io.File; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.widget.Toast; import com.leo.xjtulib.R; 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.constant.WBConstants; import com.sina.weibo.sdk.utils.Utility; /** * */ public class Share2Weibo extends Activity implements IWeiboHandler.Response { private IWeiboShareAPI mWeiboShareAPI; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String name = getIntent().getStringExtra("name"); String url = getIntent().getStringExtra("url"); String picurl = getIntent().getStringExtra("picurl"); String score = getIntent().getStringExtra("score"); System.out.println("-------start1-------"); mWeiboShareAPI = WeiboShareSDK.createWeiboAPI(this, Constant.APP_KEY); mWeiboShareAPI.registerApp(); System.out.println("-------start2-------"); sendMultiMessage(score, name, url, picurl); } private TextObject getTextObj(String score) { TextObject textObject = new TextObject(); textObject.text = "我在@交大移动图书馆 发现了一本不错的书,豆瓣评分:" + score + "分,快来看看吧"; return textObject; } private WebpageObject getWebpageObj(String name, String url) { WebpageObject mediaObject = new WebpageObject(); mediaObject.identify = Utility.generateGUID(); mediaObject.title = name; mediaObject.description = name; mediaObject.setThumbImage(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)); if (url.equals("")) url = "http://book.douban.com"; mediaObject.actionUrl = url; mediaObject.defaultText = ""; return mediaObject; } private ImageObject getImageObj(String picurl) { ImageObject imageObject = new ImageObject(); String tempPath = FileUtils.getCachePath() + "/temp.jpg"; File f = new File(tempPath); if (f.exists()) { Bitmap thumb = BitmapFactory.decodeFile(tempPath); imageObject.setImageObject(thumb); } else { Bitmap thumb = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); imageObject.setImageObject(thumb); } return imageObject; } private void sendMultiMessage(String score, String name, String url, String picurl) { WeiboMultiMessage weiboMessage = new WeiboMultiMessage();// 初始化微博的分享消息 // System.out.println("----------初始化微博的分享消息--------"); weiboMessage.textObject = getTextObj(score); weiboMessage.imageObject = getImageObj(picurl); weiboMessage.mediaObject = getWebpageObj(name, url); SendMultiMessageToWeiboRequest request = new SendMultiMessageToWeiboRequest(); request.transaction = String.valueOf(System.currentTimeMillis()); request.multiMessage = weiboMessage; mWeiboShareAPI.sendRequest(request); // System.out.println("----------结束微博的分享消息--------"); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); mWeiboShareAPI.handleWeiboResponse(intent, this); } @Override public void onResponse(BaseResponse baseResp) { System.out.println("------onResponse--------"); switch (baseResp.errCode) { case WBConstants.ErrorCode.ERR_OK: Toast.makeText(this, "分享成功", Toast.LENGTH_LONG).show(); finish(); break; case WBConstants.ErrorCode.ERR_CANCEL: Toast.makeText(this, "取消分享", Toast.LENGTH_LONG).show(); finish(); break; case WBConstants.ErrorCode.ERR_FAIL: Toast.makeText(this, "分享失败", Toast.LENGTH_LONG).show(); finish(); break; default: finish(); break; } } }
package com.leo.xjtulib.scanbook.common; import java.io.File; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import com.leo.xjtulib.R; import com.tencent.mm.sdk.openapi.IWXAPI; import com.tencent.mm.sdk.openapi.SendMessageToWX; import com.tencent.mm.sdk.openapi.WXAPIFactory; import com.tencent.mm.sdk.openapi.WXMediaMessage; import com.tencent.mm.sdk.openapi.WXWebpageObject; import com.tencent.mm.sdk.platformtools.Util; /** * */ public class Share2Weixin extends Activity { private IWXAPI WXAPI; private static final int TIMELINE = 1; private static final int FRIEND = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final String name = getIntent().getStringExtra("name"); final String url = getIntent().getStringExtra("url"); final String score = getIntent().getStringExtra("score"); final int type = getIntent().getIntExtra("type", 1); share(Share2Weixin.this, type, name, score, url); } public void share(Context context, int type, String name, String score, String url) { WXAPI = WXAPIFactory.createWXAPI(context, Constant.AppID); WXAPI.registerApp(Constant.AppID); WXWebpageObject webpage = new WXWebpageObject(); if (url.equals("")) url = "http://book.douban.com"; webpage.webpageUrl = url; WXMediaMessage msg = new WXMediaMessage(webpage); if (type == TIMELINE) { msg.title = "我在@交大移动图书馆 发现了一本不错的书,《" + name + "》豆瓣评分:" + score + "分"; msg.description = name; } else { msg.title = name; msg.description = "我在@交大移动图书馆 发现了一本不错的书,《" + name + "》豆瓣评分:" + score + "分"; } String tempPath = FileUtils.getCachePath() + "/temp.jpg"; File f = new File(tempPath); if (f.exists()) { Bitmap thumb = BitmapFactory.decodeFile(tempPath); msg.thumbData = Util.bmpToByteArray(thumb, true); } else { Bitmap thumb = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); msg.thumbData = Util.bmpToByteArray(thumb, true); } SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = ""; req.message = msg; req.scene = type == TIMELINE ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession; WXAPI.sendReq(req); finish(); } }
在应用中启动activity
Intent intent = new Intent( BookViewActivity_scanbook.this, Share2Weibo.class); intent.putExtra("url", mBook.getUrl()); intent.putExtra("score", mBook.getRate() + ""); intent.putExtra("picurl", mBook.getBitmap()); intent.putExtra("name", mBook.getTitle()); startActivity(intent);
在xml中注册
<pre name="code" class="html"><activity android:name="com.leo.xjtulib.scanbook.common.Share2Weibo" android:configChanges="keyboardHidden|orientation" android:screenOrientation="portrait" > <intent-filter> <action android:name="com.sina.weibo.sdk.action.ACTION_SDK_REQ_ACTIVITY" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name="com.leo.xjtulib.scanbook.common.Share2Weixin" android:configChanges="keyboardHidden|orientation" android:screenOrientation="portrait" > </activity>
标签:分享组件
原文地址:http://blog.csdn.net/leokelly001/article/details/45679941