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

首先,导入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 Logo
		hideShareSDKLogo();
		
		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集成完成,希望对大家有一点帮助!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值