2
3
4
(上面是一些准备 具体可查看之前写的Android第三方登录详解1,2)
5
// 首先在您的Activity中添加如下成员变量 final UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.share"); // 设置分享内容 mController.setShareContent("友盟社会化组件(SDK)让移动应用快速整合社交分享功能,http://www.umeng.com/social");
//填到分享监听里 // 设置分享图片, 参数2为图片的url地址 mController.setShareMedia(new UMImage(getActivity(), "http://www.umeng.com/images/pic/banner_module_social.png")); // 设置分享图片,参数2为本地图片的资源引用 //mController.setShareMedia(new UMImage(getActivity(), R.drawable.icon)); // 设置分享图片,参数2为本地图片的路径(绝对路径) //mController.setShareMedia(new UMImage(getActivity(), // BitmapFactory.decodeFile("/mnt/sdcard/icon.png"))); // 设置分享音乐 //UMusic uMusic = new UMusic("http://sns.whalecloud.com/test_music.mp3"); //uMusic.setAuthor("GuGu"); //uMusic.setTitle("天籁之音"); // 设置音乐缩略图 //uMusic.setThumb("http://www.umeng.com/images/pic/banner_module_social.png"); //mController.setShareMedia(uMusic); // 设置分享视频 //UMVideo umVideo = new UMVideo( // "http://v.youku.com/v_show/id_XNTE5ODAwMDM2.html?f=19001023"); // 设置视频缩略图 //umVideo.setThumb("http://www.umeng.com/images/pic/banner_module_social.png"); //umVideo.setTitle("友盟社会化分享!"); //mController.setShareMedia(umVideo);
mController.getConfig().removePlatform( SHARE_MEDIA.RENREN, SHARE_MEDIA.DOUBAN);
//分享监听 yourShareButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 是否只有已登录用户才能打开分享选择页 mController.openShare(getActivity(), false);
} });
如果有使用新浪、人人的SSO授权或者集成了facebook平台, 则必须在对应的activity中实现onActivityResult方法, 并添加如下代码
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); /**使用SSO授权必须添加如下代码 */ UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(requestCode) ; if(ssoHandler != null){ ssoHandler.authorizeCallBack(requestCode, resultCode, data); } }
注意onActivityResult方法必须写在分享所在的Activity或fragment依赖的Activity中,不可以直接写在fragment中
//参数1为当前Activity,参数2为开发者在QQ互联申请的APP ID,参数3为开发者在QQ互联申请的APP kEY. UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(getActivity(), "100424468", "c7394704798a158208a74ab60104f0ba"); qqSsoHandler.addToSocialSDK();
//参数1为当前Activity,参数2为开发者在QQ互联申请的APP ID,参数3为开发者在QQ互联申请的APP kEY. QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(getActivity(), "100424468", "c7394704798a158208a74ab60104f0ba"); qZoneSsoHandler.addToSocialSDK();
//设置新浪
SSO handler mController.getConfig().setSsoHandler(new SinaSsoHandler());
以下是我做的(仅供参考)
package com.bawei.xiangqing; import com.bawei.jinritioutiao.R; import com.umeng.socialize.bean.SHARE_MEDIA; import com.umeng.socialize.controller.UMServiceFactory; import com.umeng.socialize.controller.UMSocialService; import com.umeng.socialize.media.UMImage; import com.umeng.socialize.sso.QZoneSsoHandler; import com.umeng.socialize.sso.SinaSsoHandler; import com.umeng.socialize.sso.UMQQSsoHandler; import com.umeng.socialize.sso.UMSsoHandler; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ImageView; public class DetailsShown extends Activity{ private WebView webView; private ImageView fenxiang; private String url; private UMSocialService mController; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.detailsshow); webView = (WebView) findViewById(R.id.webviews); fenxiang = (ImageView) findViewById(R.id.info_img_repost);//分享 Intent intent = getIntent(); url = intent.getStringExtra("url"); WebSettings settings = webView.getSettings();//获得websetting,设置一些参数 settings.setDefaultTextEncodingName("utf-8");//设置编码,解决乱码 webView.loadUrl(url);//加载html //webview 加载详情 webView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub webView.loadUrl(url); return true; } }); //第三方分享 mController = UMServiceFactory.getUMSocialService("com.umeng.share"); // 设置分享内容 mController.setShareContent("分享内容"); mController.getConfig().removePlatform( SHARE_MEDIA.RENREN, SHARE_MEDIA.DOUBAN); fenxiang.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 是否只有已登录用户才能打开分享选择页 mController.openShare(DetailsShown.this, false); // 设置分享图片, 参数2为图片的url地址 mController.setShareMedia(new UMImage(DetailsShown.this, url)); } }); //QQ参数1为当前Activity,参数2为开发者在QQ互联申请的APP ID,参数3为开发者在QQ互联申请的APP kEY. UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(DetailsShown.this, "100424468", "c7394704798a158208a74ab60104f0ba"); qqSsoHandler.addToSocialSDK(); //QQ空间参数1为当前Activity,参数2为开发者在QQ互联申请的APP ID,参数3为开发者在QQ互联申请的APP kEY. QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(DetailsShown.this, "100424468", "c7394704798a158208a74ab60104f0ba"); qZoneSsoHandler.addToSocialSDK(); //设置新浪SSO handler新浪微博 mController.getConfig().setSsoHandler(new SinaSsoHandler()); } //免登陆授权 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); /**使用SSO授权必须添加如下代码 */ UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(requestCode) ; if(ssoHandler != null){ ssoHandler.authorizeCallBack(requestCode, resultCode, data); } } }
具体请查看友盟 文档中心 > 社会化分享 > 社会化Android组件 > 4.3版本快速集成
http://dev.umeng.com/social/android/quick-integration