获取友盟Appkey
如果你之前已经在友盟注册了应用,并获取到了Appkey,可以继续使用它.
如果你尚未在友盟注册开发者账号,需要先注册,注册之后登录你的账号,点击添加新应用,填写完应用基本信息后,将进入"下载SDK并添加代码"页面,此页面即可得到Appkey。
下载并安装SDK
下载[SDK最新版](http://dev.umeng.com/social/android/sdk-download)
1.进入友盟页面,点击最上面产品中的分享。
2.点击分享页面的sdk及文档下的安卓图标。
3.点击上面的SDK下载,下载里面的SDK v6.1.2,下面的选型全选,上面的图标,想分享到那个软件就选哪个软件。
4.将3.下载并安装里AndroidManifest配置下面的代码复制下来,粘贴到AndroidManifest.xml文件里。
5.解压下载的SDK包,将文件里的main文件里的lib和res文件夹放入所做的项目里。
6.将AndroidManifest配置-------设置友盟的appid下面的代码粘贴到AndroidManifest.xml文件里。
//增加一个activity
<activity
android:name="com.umeng.socialize.editorpage.ShareActivity"
android:theme="@style/Theme.UMDefault"
android:excludeFromRecents="true"/>
// 设置友盟appid
<meta-data
android:name="UMENG_APPKEY"
android:value="4eaee02c527015373b000003" >
</meta-data>
7.将AndroidManifest配置-------设置友盟的appid-----------设置权限下面的权限粘贴到AndroidManifest.xml文件中。
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.SET_DEBUG_APP" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
8.将自己要分享的平台的Activity放入到AndroidManifest.xml文件中。
微信平台
<activity
android:name="com.umeng.example.wxapi.WXEntryActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:exported="true"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Translucent.NoTitleBar" /> 9
人人网
<activity
android:name="com.renn.rennsdk.oauth.OAuthActivity"
android:configChanges="orientation|navigation|keyboardHidden" />15
腾讯
<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:screenOrientation="portrait"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />32
谷歌地图
<uses-library
android:name="com.google.android.maps"
android:required="false" />
<uses-library android:name="android.test.runner" />39 facebook
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<provider android:name="com.facebook.FacebookContentProvider" android:authorities="com.facebook.app.FacebookContentProvider+appid"
android:exported="true" />
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" />50
新浪微博
<activity
android:name=".WBShareActivity"
android:configChanges="keyboardHidden|orientation"
android:screenOrientation="portrait" >
<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.sina.weibo.sdk.component.WeiboSdkBrowser"
android:configChanges="keyboardHidden|orientation"
android:windowSoftInputMode="adjustResize"
android:exported="false" >
</activity>68
<service android:name="com.sina.weibo.sdk.net.DownloadService"
android:exported="false"></service>71
支付宝
<activity
android:name=".apshare.ShareEntryActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:exported="true"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
9.在友盟集合文档第四点初始化配置下面的4.1的代码放入Appcliation类中。(各个平台的配置,建议放在全局Application或者程序入口)
PlatformConfig.setWeixin("wx967daebe835fbeac","5bb696d9ccd75a38c8a0bfe0675559b3");//微信
PlatformConfig.setSinaWeibo("3921700954","04b48b094faeb16683c32669824ebdad");//新浪微博
PlatformConfig.setQQZone("100424468","c7394704798a158208a74ab60104f0ba");//QQ和Qzone
PlatformConfig.setAlipay("2015111700822536");//支付宝
PlatformConfig.setYixin("yxc0614e80c9304c11b0391514d09f13bf");//易信
PlatformConfig.setTwitter("3aIN7fuF685MuZ7jtXkQxalyi","MK6FEYG63eWcpDFgRYw4w9puJhzDl0tyuqWjZ3M7XJuuG7mMbO");//Twitter
PlatformConfig.setPinterest("1439206");//Pinterest
PlatformConfig.setLaiwang("laiwangd497e70d4","d497e70d4c3e4efeab1381476bac4c5e");//来往
授权
目前友盟社会化组件支持的第三方登录平台为:新浪微博、QQ、微信、人人网、豆瓣。(特别说明:QQ空间,qq微博使用qq授权)
首先获取UMShareAPI
private UMShareAPI mShareAPI;
mShareAPI = UMShareAPI.get( this );
选取需要授权的平台,并进行授权,其中umAuthLisrener是回调监听器,需要开发者根据需求重新定义
SHARE_MEDIA platform = SHARE_MEDIA.SINA;
mShareAPI.doOauthVerify(this, platform, umAuthListener)
private UMAuthListener umAuthListener = new UMAuthListener() {
@Override
public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
Toast.makeText( getApplicationContext(), "Authorize succeed", Toast.LENGTH_SHORT).show();
}
@Override
public void one rror(SHARE_MEDIA platform, int action, Throwable t) {
Toast.makeText( getApplicationContext(), "Authorize fail", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel(SHARE_MEDIA platform, int action) {
Toast.makeText( getApplicationContext(), "Authorize cancel", Toast.LENGTH_SHORT).show();
}
};
对于删除授权使用的接口是:mShareAPI.deleteOauth(AuthActivity.this, platform, umdelAuthListener);
注意要重写
onActivityResult()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mShareAPI.onActivityResult(requestCode, resultCode, data);
}
平台授权,是需要安装对应的客户端的。新浪微博,豆瓣网,人人网除外,这三个平台如果本地安装了对应平台是走本地授权,如果没有安装,需要走网络授权、
分享
final SHARE_MEDIA[] displaylist = new SHARE_MEDIA[]
{
SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE,SHARE_MEDIA.SINA,
SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE,SHARE_MEDIA.DOUBAN
};
new ShareAction(this).setDisplayList( displaylist )
.withText( "呵呵" )
.withMedia( image )
.setShareboardclickCallback(shareBoardlistener)
.open();
自定义shareboard需要,指明需要显示的平台,放入shareaction中,并执行open方法即可。
shareboard点击响应回调方法如下:
private ShareBoardlistener shareBoardlistener = new ShareBoardlistener() {
@Override
public void onclick(SHARE_MEDIA share_media) {
}
};
单平台直接分享:
new ShareAction(this)
.setPlatform(SHARE_MEDIA.DOUBAN)
.setCallback(umShareListener)
.withText("Hello 豆瓣")
.withMedia(image)
.share();
选取分享的平台,设定分享回调接口和内容,并执行share方法。
回调接口如下:
new UMShareListener() {
@Override
public void onResult(SHARE_MEDIA platform) {
Toast.makeText(ShareActivity.this,platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
}
@Override
public void one rror(SHARE_MEDIA platform, Throwable t) {
Toast.makeText(ShareActivity.this,platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel(SHARE_MEDIA platform) {
Toast.makeText(ShareActivity.this,platform + " 分享取消了", Toast.LENGTH_SHORT).show();
}
};
值得注意的是,分享也应该重写onActivityResult()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
UMShareAPI.get( this ).onActivityResult( requestCode, resultCode, data);
}
获取用户信息和好友列表
已授权的平台,可以获取用户信息(新浪微博可以获取用户好友列表)
mShareAPI = UMShareAPI.get(this);
初始化UMShareAPI,然后进行用户信息获取:
mShareAPI.getPlatformInfo(UserinfoActivity.this, platform, umAuthListener);
对于新浪微博好友列表的获取使用的接口是:
mShareAPI.getFriend(UserinfoActivity.this, SHARE_MEDIA.SINA, umGetfriendListener);
注意必须在调用授权接口 mShareAPI.doOauthVerify(this, platform, umAuthListener) 之后才可以获取用户信息,建议在doOauthVerify方法完成的onComplete方法中调用获取用户信息方法,返回的所有信息都在Map data)集合中获取
高级功能介绍:
对于以上介绍的基本功能已经可以实现一般用户的需求,对于特殊要求,我们进行了特别处理:
针对分享面板,我们进行了如下的处理:
针对不同平台使用不同监听器,以及分享不同内容
new ShareAction(this)
.setDisplayList( SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, )
.setContentList(shareContent1,shareContent2)
.setCallback(umShareListener)
.withText( "umeng" )
.withMedia( image )
.setListenerList(listener1,listener2)
.open();
分享面板点击回调
使用setShareboardclickCallback()可以进行分享面板不同按钮的点击
new ShareAction(this)
.setDisplayList( SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, )
.withText( "umeng" )
.withMedia( image )
setShareboardclickCallback(shareBoardlistener)
.open();
分享面板增加自定义按钮
如果需要对分享面板增加自定义的按钮,可以通过
`ShareAction(this).addButton(String showword,String Keyword,String icon,String Grayicon)`
private ShareBoardlistener shareBoardlistener = new ShareBoardlistener() {
@Override
public void onclick(SnsPlatform snsPlatform,SHARE_MEDIA share_media) {
if (share_media==null){
if (snsPlatform.mKeyword.equals("11")){
Toast.makeText(ShareActivity.this,"add button success",Toast.LENGTH_LONG).show();
}
}
else {
new ShareAction(ShareActivity.this).setPlatform(share_media).setCallback(umShareListener)
.withText("多平台分享")
.share();
}
}
};
是新增按钮的回调监听是通过判断当获取平台为空时,keyword的值来进行确定的,并在对应的条件中进行处理即可。
获取客户端安装信息
-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.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{*;}
-dontwarn twitter4j.**
-keep class twitter4j.** { *; }
-keep class com.tencent.** {*;}
-dontwarn com.tencent.**
-keep public class com.umeng.soexample.R$*{
public static final int *;
}
-keep public class com.umeng.soexample.R$*{
public static final int *;
}
-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.sina.** {*;}
-dontwarn com.sina.**
-keep class com.alipay.share.sdk.** {
*;
}
-keepnames class * implements android.os.Parcelable {
public static final ** CREATOR;
}
-keep class com.linkedin.** { *; }
-keepattributes Signature
具体实现:
登录:
private UMShareAPI umShareAPI;
//监听事件
private UMAuthListener umAuthListener = new UMAuthListener() {
//登录成功
@Override
public void onComplete(SHARE_MEDIA platform, int action,Map<String, String> data) {
Toast.makeText(MainActivity.this, "Authorize succeed", Toast.LENGTH_SHORT).show();
//获取用户信息
umShareAPI.getPlatformInfo(MainActivity.this, platform,new UMAuthListener() {
@Override
public void one rror(SHARE_MEDIA arg0, int arg1,Throwable arg2) {
}
//成功
@Override
public void onComplete(SHARE_MEDIA arg0, int arg1,Map<String, String> data) {
Set<String> set = data.keySet();
for (String string : set) {
Log.i("msg",
"============================Map=========================");
Log.i("msg", string + "::::" + data.get(string));
String str = data.get(string);
// 设置头像
if(str!=null){
signMode.setVisibility(View.GONE);
header.setVisibility(View.VISIBLE);
if (string.equals("profile_image_url")) {
DisplayImageOptions options = null;
ImageLoader.getInstance().displayImage(str,mineHeader, options);
ImageLoader.getInstance().displayImage(str,mine, options);
}
// 设置昵称
if (string.equals("screen_name")) {
mineName.setText(str);
}
}
}
}
@Override
public void onCancel(SHARE_MEDIA arg0, int arg1) {
}
});
}
@Override
public void one rror(SHARE_MEDIA platform, int action, Throwable t) {
Toast.makeText(MainActivity.this, "Authorize fail", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel(SHARE_MEDIA platform, int action) {
Toast.makeText(MainActivity.this, "Authorize cancel", Toast.LENGTH_SHORT).show();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PlatformConfig.setQQZone("100424468","c7394704798a158208a74ab60104f0ba");
umShareAPI = UMShareAPI.get(this);
}
重写onActivityResult()
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
umShareAPI.onActivityResult(requestCode, resultCode, data);
Log.e("data", resultCode + "");
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.qq_sign_button:
SHARE_MEDIA platform = SHARE_MEDIA.QQ;//此处是QQ授权
umShareAPI.doOauthVerify(this, platform, umAuthListener);
break;
}
}
清单文件配置:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.SET_DEBUG_APP" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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:screenOrientation="portrait"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<meta-data
android:name="UMENG_APPKEY"
android:value="4eaee02c527015373b000003" />
平台信息
final SHARE_MEDIA[] displaylist = new SHARE_MEDIA[]
{
SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.SINA,
SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE, SHARE_MEDIA.DOUBAN
};
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
11
//多平台分享,这次实现的是shareBoardlistener监听
new ShareAction(MainActivity.this).setDisplayList(displaylist)
.withText("呵呵")//可以没有
.setShareboardclickCallback(shareBoardlistener)
.open();
}
});
private ShareBoardlistener shareBoardlistener = new ShareBoardlistener() {
@Override
public void onclick(SnsPlatform snsPlatform, SHARE_MEDIA share_media) {
Log.e("snsPlatform", snsPlatform + "" + "SHARE_MEDIA" + share_media + "");
//调用单平台分享
new ShareAction(MainActivity.this)
//参数改成share_media
.setPlatform(share_media)
.setCallback(new UMShareListener() {
@Override
public void onResult(SHARE_MEDIA platform) {
Toast.makeText(MainActivity.this, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
}
@Override
public void one rror(SHARE_MEDIA platform, Throwable t) {
Toast.makeText(MainActivity.this, platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel(SHARE_MEDIA platform) {
Toast.makeText(MainActivity.this, platform + " 分享取消了", Toast.LENGTH_SHORT).show();
}
})
.withText("Hello 豆瓣")
.share();
}
};
标签:登录,友盟,class,SHARE,public,MEDIA,keep,com,第三方
From: https://blog.51cto.com/u_4387387/6319693