首先,導入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集成完成,希望對大家有一點幫助!