版權聲明:本文爲HaiyuKing原創文章,轉載請註明出處!html
這裏簡單記錄下友盟分享SDK集成的步驟。java
若是想要使用自定義分享對話框實現,請參考《ShareDialogDemo【分享對話框】》,這個demo中就不集成分享對話框了,只是簡單記錄下和自定義對話框搭配的相關代碼!android
另外,由於可能用到存儲權限,因此還須要搭配運行時權限申請功能。web
還有就是QQ、微信、新浪等appkey值,須要在各自的平臺上申請,這裏就不一一介紹了。api
注意事項:微信
一、 導入類文件後須要change包名以及從新import R文件路徑app
二、 Values目錄下的文件(strings.xml、dimens.xml、colors.xml等),若是項目中存在,則複製裏面的內容,不要整個覆蓋ide
具體請閱讀官網:https://developer.umeng.com/docs/66632/detail/66639 gradle
方法一:下載SDK,將解壓出來的jar包和res文件複製到工程libs/目錄下。ui
下載地址:https://developer.umeng.com/
文件夾裏面的jar文件、res文件都用獲得。
(1)首先,新建thirdlib這個module,用來統一管理第三方平臺的SDK;而且在app的bundle.gradle文件中引入thirdlib
//thirdlib
implementation project(':thirdlib')
(2)而後,將下載的友盟分享SDK中的jar文件複製到thirdlib這個module的libs目錄下,而且在thirdlib的bundle.gradle文件中引用這些jar文件;以及res文件也要複製
還須要在app的bundle.gradle中添加如下代碼
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.why.project.umengsharedemo"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
//集成友盟分享sdk repositories { flatDir { dirs 'libs' dirs project(':thirdlib').file('libs') } }
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//thirdlib
implementation project(':thirdlib')
}
回到thirdlib中,添加回調Activity
微信
在包名目錄下建立wxapi文件夾,新建一個名爲WXEntryActivity的activity繼承WXCallbackActivity。
支付寶
與微信類似,支付寶須要在包名目錄下建立apshare文件夾,而後創建一個ShareEntryActivity的類,繼承ShareCallbackActivity。
釘釘
與微信類似,釘釘須要在包名目錄下建立ddshare文件夾,而後創建一個DDShareActivity的類,DingCallBack。
QQ與新浪
QQ與新浪不須要添加Activity,但須要在使用QQ分享或者受權的Activity中,添加:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data); }
注意onActivityResult不可在fragment中實現,若是在fragment中調用登陸或分享,須要在fragment依賴的Activity中實現。
(3)而後,在app的AndroidManifest.xml中添加權限、配置Appkey【注意,紫色標記的代碼】
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.why.project.umengsharedemo"> <!-- ======================友盟分享========================== --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <application android:name=".MyApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <!-- ======================友盟分享========================== --> <!--value的值填寫你在友盟後臺申請的應用Appkey--> <meta-data android:value="y483298439438209dewdewdew8989328" android:name="UMENG_APPKEY"/> <!--value的值填寫渠道名稱,例如yingyongbao。這裏設置動態渠道變量【若是配合友盟統計一塊兒使用的話,這裏換成友盟統計的寫法】--> <meta-data android:value="umengshare" android:name="UMENG_CHANNEL"/> <!-- 新浪 --> <activity android:name="com.umeng.socialize.media.WBShareCallBackActivity" android:configChanges="keyboardHidden|orientation" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:exported="false" > </activity> <activity android:name="com.sina.weibo.sdk.web.WeiboSdkWebActivity" android:configChanges="keyboardHidden|orientation" android:exported="false" android:windowSoftInputMode="adjustResize" > </activity> <activity android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" android:launchMode="singleTask" android:name="com.sina.weibo.sdk.share.WbShareTransActivity"> <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.why.project.thirdlib.wxapi.WXEntryActivity" android:configChanges="keyboardHidden|orientation|screenSize" android:exported="true" android:theme="@android:style/Theme.Translucent.NoTitleBar" /> <!-- QQ 請注意將qq appkey替換成您本身的qq appkey --> <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="tencent74743893832980203290" /> </intent-filter> </activity> <activity android:name="com.tencent.connect.common.AssistActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:configChanges="orientation|keyboardHidden|screenSize"/> </application> </manifest>
由於可能用到存儲權限,因此還須要搭配運行時權限申請功能。
(4)而後,在app中新建MyApplication.java文件,並添加友盟分享初始化方法
package com.why.project.umengsharedemo; import android.app.Application; import com.umeng.commonsdk.UMConfigure; import com.umeng.socialize.PlatformConfig; /** * Created by HaiyuKing * Used */ public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); /*=================友盟分享sdk=====================*/ initUmengShare(); } //友盟分享 private void initUmengShare(){ /** * 若是項目的Manifest文件中已經配置【友盟+】的AppKey和Channel,則使用該方法初始化,同時沒必要再次傳入AppKey和Channel兩個參數。 */ UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE,""); PlatformConfig.setWeixin("wx3232432432", "ferfefre5324432fref3rwf"); PlatformConfig.setSinaWeibo("34432423dfew", "gfgreerrerererwe","http://sns.whalecloud.com"); PlatformConfig.setQQZone("3223354321", "r234r432ferfrwewe"); } }
好了,友盟分享集成到項目中了。
<resources> <string name="app_name">UmengShareDemo</string> <string name="share_title">分享簡書app</string> <string name="share_url">https://www.jianshu.com/</string> <string name="share_desc">人生海海,有趣的人相聚於此</string> <string name="share_open_error">手機中沒有安裝該應用</string> </resources>
package com.why.project.umengsharedemo; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.widget.Toast; import com.umeng.socialize.ShareAction; import com.umeng.socialize.UMShareAPI; import com.umeng.socialize.UMShareListener; import com.umeng.socialize.bean.SHARE_MEDIA; import com.umeng.socialize.media.UMImage; import com.umeng.socialize.media.UMWeb; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //顯示分享底部區域【顯示自定義分享對話框的代碼】 /*Bundle bundle = new Bundle(); ShareDialog shareDialog = ShareDialog.getInstance(mContext,bundle); shareDialog.setOnShareClickLitener(new ShareDialog.OnShareClickLitener() { @Override public void onShareToQQ() { openShare("QQ"); } @Override public void onShareToQZone() { openShare("QZone"); } @Override public void onShareToWX() { openShare("WX"); } @Override public void onShareToWXCircle() { openShare("WXCircle"); } @Override public void onShareToSina() { openShare("Sina"); } }); shareDialog.show(getSupportFragmentManager(), "Share");*/ } //友盟分享SDK--打開分享面板 private void openShare() { UMWeb web = new UMWeb(getResources().getString(R.string.share_url)); web.setTitle(getResources().getString(R.string.share_title));//標題 UMImage thumb = new UMImage(this, R.drawable.share_thumb);//縮略圖不要超過18k thumb.compressStyle = UMImage.CompressStyle.SCALE;//大小壓縮,默認爲大小壓縮,適合普通很大的圖 web.setThumb(thumb); //縮略圖 web.setDescription(getResources().getString(R.string.share_desc));//描述 new ShareAction(MainActivity.this) .withMedia(web) .setDisplayList(SHARE_MEDIA.SINA,SHARE_MEDIA.QQ,SHARE_MEDIA.QZONE,SHARE_MEDIA.WEIXIN,SHARE_MEDIA.WEIXIN_CIRCLE,SHARE_MEDIA.WEIXIN_FAVORITE) .setCallback(shareListener) .open(); } //友盟分享SDK--打開分享面板 private void openShare(String type){ UMWeb web = new UMWeb(getResources().getString(R.string.share_url)); web.setTitle(getResources().getString(R.string.share_title));//標題 UMImage thumb = new UMImage(this, R.drawable.share_thumb);//縮略圖不要超過18k thumb.compressStyle = UMImage.CompressStyle.SCALE;//大小壓縮,默認爲大小壓縮,適合普通很大的圖 web.setThumb(thumb); //縮略圖 web.setDescription(getResources().getString(R.string.share_desc));//描述 switch (type){ case "QQ": new ShareAction(MainActivity.this) .setPlatform(SHARE_MEDIA.QQ)//傳入平臺 .withMedia(web)//分享內容 .setCallback(shareListener)//回調監聽器 .share(); break; case "QZone": new ShareAction(MainActivity.this) .setPlatform(SHARE_MEDIA.QZONE)//傳入平臺 .withMedia(web)//分享內容 .setCallback(shareListener)//回調監聽器 .share(); break; case "WX": new ShareAction(MainActivity.this) .setPlatform(SHARE_MEDIA.WEIXIN)//傳入平臺 .withMedia(web)//分享內容 .setCallback(shareListener)//回調監聽器 .share(); break; case "WXCircle": new ShareAction(MainActivity.this) .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)//傳入平臺 .withMedia(web)//分享內容 .setCallback(shareListener)//回調監聽器 .share(); break; case "Sina": new ShareAction(MainActivity.this) .setPlatform(SHARE_MEDIA.SINA)//傳入平臺 .withMedia(web)//分享內容 .setCallback(shareListener)//回調監聽器 .share(); break; } } private UMShareListener shareListener = new UMShareListener() { /** * @descrption 分享開始的回調 * @param platform 平臺類型 */ @Override public void onStart(SHARE_MEDIA platform) { } /** * @descrption 分享成功的回調 * @param platform 平臺類型 */ @Override public void onResult(SHARE_MEDIA platform) { } /** * @descrption 分享失敗的回調 * @param platform 平臺類型 * @param t 錯誤緣由 */ @Override public void onError(SHARE_MEDIA platform, Throwable t) { Log.e("why","t.getMessage()="+t.getMessage());//錯誤碼:2008 錯誤信息:沒有安裝應用 if(t.getMessage().contains("2008")){ Toast.makeText(MainActivity.this,getResources().getString(R.string.share_open_error),Toast.LENGTH_SHORT).show(); } } /** * @descrption 分享取消的回調 * @param platform 平臺類型 */ @Override public void onCancel(SHARE_MEDIA platform) { } }; //友盟分享SDK--QQ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);//友盟分享 } }
#=====================友盟分享sdk=====================
-dontshrink
-dontoptimize
-dontwarn com.google.android.maps.**
-dontwarn android.webkit.WebView
-dontwarn com.umeng.**
-dontwarn com.tencent.weibo.sdk.**
-dontwarn com.facebook.**
-keep public class javax.**
-keep public class android.webkit.**
-dontwarn android.support.v4.**
-keep enum com.facebook.**
-keepattributes Exceptions,InnerClasses,Signature
-keepattributes *Annotation*
-keepattributes SourceFile,LineNumberTable
-keep public interface com.facebook.**
-keep public interface com.tencent.**
-keep public interface com.umeng.socialize.**
-keep public interface com.umeng.socialize.sensor.**
-keep public interface com.umeng.scrshot.**
-keep public class com.umeng.socialize.* {*;}
-keep class com.facebook.**
-keep class com.facebook.** { *; }
-keep class com.umeng.scrshot.**
-keep public class com.tencent.** {*;}
-keep class com.umeng.socialize.sensor.**
-keep class com.umeng.socialize.handler.**
-keep class com.umeng.socialize.handler.*
-keep class com.umeng.weixin.handler.**
-keep class com.umeng.weixin.handler.*
-keep class com.umeng.qq.handler.**
-keep class com.umeng.qq.handler.*
-keep class UMMoreHandler{*;}
-keep class com.tencent.mm.sdk.modelmsg.WXMediaMessage {*;}
-keep class com.tencent.mm.sdk.modelmsg.** implements com.tencent.mm.sdk.modelmsg.WXMediaMessage$IMediaObject {*;}
-keep class im.yixin.sdk.api.YXMessage {*;}
-keep class im.yixin.sdk.api.** implements im.yixin.sdk.api.YXMessage$YXMessageData{*;}
-keep class com.tencent.mm.sdk.** {
*;
}
-keep class com.tencent.mm.opensdk.** {
*;
}
-keep class com.tencent.wxop.** {
*;
}
-keep class com.tencent.mm.sdk.** {
*;
}
-dontwarn twitter4j.**
-keep class twitter4j.** { *; }
-keep class com.tencent.** {*;}
-dontwarn com.tencent.**
-keep class com.kakao.** {*;}
-dontwarn com.kakao.**
-keep public class com.umeng.com.umeng.soexample.R$*{
public static final int *;
}
-keep public class com.linkedin.android.mobilesdk.R$*{
public static final int *;
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class com.tencent.open.TDialog$*
-keep class com.tencent.open.TDialog$* {*;}
-keep class com.tencent.open.PKDialog
-keep class com.tencent.open.PKDialog {*;}
-keep class com.tencent.open.PKDialog$*
-keep class com.tencent.open.PKDialog$* {*;}
-keep class com.umeng.socialize.impl.ImageImpl {*;}
-keep class com.sina.** {*;}
-dontwarn com.sina.**
-keep class com.alipay.share.sdk.** {
*;
}
-keepnames class * implements android.os.Parcelable {
public static final ** CREATOR;
}
-keep class com.linkedin.** { *; }
-keep class com.android.dingtalk.share.ddsharemodule.** { *; }
-keepattributes Signature
暫時空缺
連接:https://pan.baidu.com/s/1n3DScJYDZS4ztuJMEFqrPg 提取碼:wekp