通过shareSDK进行社会化分享集成

来源:互联网 发布:迅龙数据恢复软件激活 编辑:程序博客网 时间:2024/05/17 21:47

首先,导入ShareSDK库到项目中的libs目录:


在Androidmanifest.xml配置文件中添加配置如下:

 <!-- ShareSDK社会化分享插件 -->        <activity            android:name="cn.sharesdk.framework.ShareSDKUIShell"            android:configChanges="keyboardHidden|orientation|screenSize"            android:screenOrientation="portrait"            android:theme="@android:style/Theme.Translucent.NoTitleBar"            android:windowSoftInputMode="stateHidden|adjustResize" >            <!--            Adapter表示一个继承自cn.sharesdk.framework.authorize.AuthorizeAdapter的类,            这个类可以监听到页面的生命周期,也可以获取授权页面的各种UI控件。             开发者可以通过继承AuthorizeAdapter,重写其方法,并获取各种UI来自定义这个页面的行为。            -->            <meta-data                android:name="Adapter"                android:value="com.chinatsp.yuantecar.adapter.MyAdapter" />            <intent-filter>                <data android:scheme="tencent100564783" />                <action android:name="android.intent.action.VIEW" />                <category android:name="android.intent.category.DEFAULT" />                <category android:name="android.intent.category.BROWSABLE" />            </intent-filter>        </activity>

在代码中继承并重写自己的AuthorizeAdapter如下:

public class MyAdapter extends AuthorizeAdapter implements PlatformActionListener {private PlatformActionListener backListener;public void onCreate() {// 隐藏标题栏右部的Share SDK LogohideShareSDKLogo();TitleLayout llTitle = getTitleLayout();llTitle.getTvTitle().setTextColor(getActivity().getResources().getColor(R.color.red));llTitle.setPadding(30, 30, 30, 30);llTitle.getBtnBack().setVisibility(View.VISIBLE);String platName = getPlatformName();if (SinaWeibo.NAME.equals(platName)|| SinaWeibo.NAME.equals(platName)) {llTitle.getTvTitle().setText("绑定新浪微博");interceptPlatformActionListener(platName);return;}// 使弹出动画失效,只能在onCreate中调用,否则无法起作用// disablePopUpAnimation();// 下面的代码演示如何设置自定义的授权页面打开动画//disablePopUpAnimation();//View rv = (View) getBodyView().getParent();//TranslateAnimation ta = new TranslateAnimation(//Animation.RELATIVE_TO_SELF, -1,//Animation.RELATIVE_TO_SELF, 0,//Animation.RELATIVE_TO_SELF, 0,//Animation.RELATIVE_TO_SELF, 0);//ta.setDuration(500);//rv.setAnimation(ta);}private void interceptPlatformActionListener(String platName) {Platform plat = ShareSDK.getPlatform(getActivity(), platName);// 备份此前设置的事件监听器backListener = plat.getPlatformActionListener();// 设置新的监听器,实现事件拦截plat.setPlatformActionListener(this);}public void onError(Platform plat, int action, Throwable t) {if (action == Platform.ACTION_AUTHORIZING) {// 授权时即发生错误plat.setPlatformActionListener(backListener);if (backListener != null) {backListener.onError(plat, action, t);}}else {// 关注时发生错误plat.setPlatformActionListener(backListener);if (backListener != null) {backListener.onComplete(plat, Platform.ACTION_AUTHORIZING, null);}}}public void onComplete(Platform plat, int action,HashMap<String, Object> res) {if (action == Platform.ACTION_FOLLOWING_USER) {// 当作授权以后不做任何事情plat.setPlatformActionListener(backListener);if (backListener != null) {backListener.onComplete(plat, Platform.ACTION_AUTHORIZING, null);}}else {// 如果没有标记为“授权并关注”则直接返回plat.setPlatformActionListener(backListener);if (backListener != null) {// 关注成功也只是当作授权成功返回backListener.onComplete(plat, Platform.ACTION_AUTHORIZING, null);}}}public void onCancel(Platform plat, int action) {plat.setPlatformActionListener(backListener);if (action == Platform.ACTION_AUTHORIZING) {// 授权前取消if (backListener != null) {backListener.onCancel(plat, action);}}else {// 当作授权以后不做任何事情if (backListener != null) {backListener.onComplete(plat, Platform.ACTION_AUTHORIZING, null);}}}}

然后即可进行分享到指定的平台:

以新浪微博为例:

Platform.ShareParams spsp = new SinaWeibo.ShareParams();spsp.text = text;Platform sinaweibo = ShareSDK.getPlatform(getActivity(),SinaWeibo.NAME);sinaweibo.setPlatformActionListener(MoreFragment.this);sinaweibo.share(spsp);

至此ShareSDK集成完成,希望对大家有一点帮助!


0 0
原创粉丝点击