基於最新友盟開發文件,整合友盟分享功能,賦demo
阿新 • • 發佈:2018-12-08
整合準備
獲取Appkey
快速整合
獲取SDK,頁面截圖:
下載後開啟
匯入jar和res
添加回調Activity
微信
在包名目錄下建立wxapi資料夾,新建一個名為WXEntryActivity的activity繼承WXCallbackActivity。
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); }
配置Android Manifest XML
sdk中需要的Activity
微信
<activity
android:name=".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="tencent100424468" /> </intent-filter> </activity> <activity android:name="com.tencent.connect.common.AssistActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:configChanges="orientation|keyboardHidden|screenSize"/>
許可權新增
請在AndroidManifest中新增如下許可權
<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" />
如果需要使用QQ純圖分享或避免其它平臺純圖分享的時候圖片不被壓縮,可以增加以下許可權:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Android6.0許可權適配
請檢視你的build.gradle檔案,如果 targetSdkVersion小於或等於22,可以忽略這一步,如果大於或等於23,需要做許可權的動態申請:
if(Build.VERSION.SDK_INT>=23){
String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.CALL_PHONE,Manifest.permission.READ_LOGS,Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.SET_DEBUG_APP,Manifest.permission.SYSTEM_ALERT_WINDOW,Manifest.permission.GET_ACCOUNTS,Manifest.permission.WRITE_APN_SETTINGS};
ActivityCompat.requestPermissions(this,mPermissionList,123);
}
其中123是requestcode,可以根據這個code判斷,使用者是否同意了授權。如果沒有同意,可以根據回撥進行相應處理:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
}
初始化設定
初始化需要在您的Application中呼叫我們的初始化介面:
@Override
public void onCreate() {
super.onCreate();
UMConfigure.init(this,"5a12384aa40fa3551f0001d1"
,"umeng",UMConfigure.DEVICE_TYPE_PHONE,"");//58edcfeb310c93091c000be2 5965ee00734be40b580001a0
}
需要設定各個平臺的appkey:
PlatformConfig.setWeixin("wxeb0858b766302e78", "6ab458c26cc7c6b8489ae34898426903");
PlatformConfig.setQQZone("1149581072", "c7394704798a158208a74ab60104f0ba");
簽名配置
簽名的概念將資料夾中的簽名檔案放入到工程中,例如我的簽名檔案是debug.keystore
然後增加簽名檔案的密碼:
signingConfigs {
debug {
storeFile file('debug.keystore')
storePassword "android"
keyAlias "androiddebugkey"
keyPassword "android"
}
}
signingConfigs配置進去,如下圖所示:
分享,例如在點選事件中直接加入如下程式碼
new ShareAction(MainActivity.this).withText("hello")
.setDisplayList(SHARE_MEDIA.QQ,SHARE_MEDIA.WEIXIN)
.setCallback(shareListener).open();
UMWeb web = new UMWeb("https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=false&word=%E7%BE%8E%E5%A5%B3&step_word=&hs=0&pn=6&spn=0&di=29553718610&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=2&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=-1&cs=3633976638%2C143512992&os=3804935736%2C2570557697&simid=2085973135%2C839809798&adpicid=0&lpn=0&ln=3660&fr=&fmq=1544077227262_R&fm=result&ic=0&s=undefined&hd=0&latest=0©right=0&se=&sme=&tab=0&width=&height=&face=undefined&ist=&jit=&cg=girl&bdtype=13&oriquery=&objurl=http%3A%2F%2Fscimg.jb51.net%2Fallimg%2F170109%2F106-1F109120J9646.jpg&fromurl=ippr_z2C%24qAzdH3FAzdH3Ffv_z%26e3B3kc8_z%26e3BgjpAzdH3Frtvp76jAzdH3Fut276jAzdH3F8m8c08_z%26e3Bip4&gsm=0&rpstart=0&rpnum=0&islist=&querylist=&selected_tags=0");
web.setTitle("This is music title");//標題
web.setDescription("my description");//描述
new ShareAction(MainActivity.this)
.withMedia(web)
.setDisplayList(SHARE_MEDIA.QQ,SHARE_MEDIA.QZONE,SHARE_MEDIA.WEIXIN,SHARE_MEDIA.WEIXIN_CIRCLE)
.setCallback(shareListener).open();
效果圖
完
最後賦上demo地址:https://download.csdn.net/download/heishuai123/10836478