Android:Umeng(友盟) 微信,QQ,新浪分享 (三)
时间:2016-05-13 02:26:48
收藏:0
阅读:924
第一步:
下载并安装SDK
添加代码和资源引用,我们提供了添加资源文件和jar文件的两种方式,可以根据需求选择
解压SDK压缩包,将文件夹中的‘main/libs‘和‘main/res‘文件夹复制到你的项目工程根目录下(如使用‘ADT 17‘以下用户需要手动添加‘libs‘下的jar文件到工程Path中)
结合上两篇文章 PushSDKMoudle中添加QQ,微信,新浪分享后的libs结构:
注:除jar包外,其余文件夹均需添加对应分享的so包。没有则不添加,附图:
第二步:
AndroidManifest配置:
<!-- 分享--> <activity android:name="com.umeng.socialize.editorpage.ShareActivity" android:theme="@style/Theme.UMDefault" android:excludeFromRecents="true"/>
增加权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_LOGS" /> <uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission android:name="android.permission.SET_DEBUG_APP" /> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.USE_CREDENTIALS" /> <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
针对不平台所需要添加的数据
按照自己需要的平台添加对应的配置,如下图,其余请参考官方文档:
微信平台
<activity android:name="com.umeng.soexample.wxapi.WXEntryActivity" android:configChanges="keyboardHidden|orientation|screenSize" android:exported="true" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar" />注:打包后的apk文件签名必须与微信平台设置一致
WXEntryActivity需建在wxapi的目录下,该类继承自WXCallbackActivity,预留空类即可,如图:
<!-- 微信分享 --> <activity android:name=".wxapi.WXEntryActivity" android:configChanges="keyboardHidden|orientation|screenSize" android:exported="true" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
public class WXEntryActivity extends WXCallbackActivity { }
QQ、Qzone
<activity android:name="com.tencent.tauth.AuthActivity" android:launchMode="singleTask" android:noHistory="true" > <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="tencentAPPID" /> </intent-filter> </activity> <activity android:name="com.tencent.connect.common.AssistActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar" />注:将Android manifest xml里的 腾讯平台APPID替换为自己申请的APPID,注意tencent前缀不要修改,否则无法正常实现QQ及Qzone登录
新浪微博
<activity android:name=".WBShareActivity" 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.sina.weibo.sdk.component.WeiboSdkBrowser" android:configChanges="keyboardHidden|orientation" android:windowSoftInputMode="adjustResize" android:exported="false" > </activity> <service android:name="com.sina.weibo.sdk.net.DownloadService" android:exported="false"> </service>
注:这里的DownloadService亮红不影响程序编译运行,不需要处理
同微信注册相同,在你的工程应用包名的相应目录下新增一个WBShareActivity
类,该类继承自WBShareCallBackActivity,预留空类即可,如图:
public class WBShareActivity extends WBShareCallBackActivity { }
//微信 appid appsecret PlatformConfig.setWeixin("wx967daebe835fbeac", "5bb696d9ccd75a38c8a0bfe0675559b3"); //新浪微博 appkey appsecret PlatformConfig.setSinaWeibo("3921700954","04b48b094faeb16683c32669824ebdad"); // QQ和Qzone appid appkey PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba");
第三步:
自定义分享,以及分享不同内容
在application中添加代码:
public static final SHARE_MEDIA[] displaylist = {SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE,SHARE_MEDIA.SINA /*,SHARE_MEDIA.DOUBAN*/};
在需要启用分享的Activity中添加如下代码:
//分享 private ShareAction shareAction = new ShareAction(WebViewActivity.this) .setDisplayList(BaseApplication.displaylist) .setShareboardclickCallback(shareBoardlistener);
针对不同平台使用不同监听器
使用setShareboardclickCallback()
可以进行分享面板不同按钮的点击回调,方便开发者进行特殊处理
private ShareBoardlistener shareBoardlistener = new ShareBoardlistener() { @Override public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) { switch (share_media) { case WEIXIN: new ShareAction(WebViewActivity.this).setPlatform(share_media).setCallback(umShareListener) .setShareContent(contents[1]) .share(); break; case WEIXIN_CIRCLE: new ShareAction(WebViewActivity.this).setPlatform(share_media).setCallback(umShareListener) .setShareContent(contents[1]) .share(); break; case SINA: new ShareAction(WebViewActivity.this).setPlatform(share_media).setCallback(umShareListener) .setShareContent(contents[1]) .share(); break; case QZONE: new ShareAction(WebViewActivity.this).setPlatform(share_media).setCallback(umShareListener) .setShareContent(contents[0]) .share(); break; case QQ: new ShareAction(WebViewActivity.this).setPlatform(share_media).setCallback(umShareListener) .setShareContent(contents[0]) .share(); break; default: new ShareAction(WebViewActivity.this).setPlatform(share_media).setCallback(umShareListener) .setShareContent(contents[1]) .share(); break; } } };
private UMShareListener umShareListener = new UMShareListener() { @Override public void onResult(SHARE_MEDIA platform) { Toast.makeText(WebViewActivity.this, " 分享成功", Toast.LENGTH_SHORT).show(); } @Override public void onError(SHARE_MEDIA platform, Throwable t) { Toast.makeText(WebViewActivity.this, " 分享失败", Toast.LENGTH_SHORT).show(); } @Override public void onCancel(SHARE_MEDIA platform) { Toast.makeText(WebViewActivity.this, " 分享取消", Toast.LENGTH_SHORT).show(); } };
设置分享内容ShareContent
private ShareContent[] contents = new ShareContent[3]; { contents[0] = new ShareContent(); contents[1] = new ShareContent(); contents[2] = new ShareContent(); }
contents[0].mTitle = "分享"; contents[0].mText = "内容"; contents[0].mTargetUrl = "链接地址"; contents[0].mMedia = new UMImage(mContext, resource);//图片资源
最后启动分享功能:
shareAction.open();
评论(0)