1.导入黄油刀依赖
初始化控件
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_notshare"
android:text="不带面板的分享"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_share"
android:text="带面板的分享"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_shareThread"
android:text="第三方分享"/>
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@mipmap/ic_launcher_round"
android:id="@+id/image_icon"/>
2.下载社会化分享SDK
3.在清代文件中粘贴 粘贴完后会报错 需要导包
<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"/>
4.导包
5.添加权限
6.在当前Activity的onCreat()方法中复制权限配置
Android6.0权限配置
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);
}![在这里插入图片描述](https://img-blog.csdnimg.cn/20190109172044138.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80Mzg2MDQ0Mg==,size_16,color_FFFFFF,t_70)
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
}
7.在Appliction的实例化类中 复制平台秘钥
PlatformConfig.setSinaWeibo("3921700954","04b48b094faeb16683c32669824ebdad","http://sns.whalecloud.com");
PlatformConfig.setYixin("yxc0614e80c9304c11b0391514d09f13bf");
PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba");
PlatformConfig.setTwitter("3aIN7fuF685MuZ7jtXkQxalyi","MK6FEYG63eWcpDFgRYw4w9puJhzDl0tyuqWjZ3M7XJuuG7mMbO");
PlatformConfig.setAlipay("2015111700822536");
PlatformConfig.setLaiwang("laiwangd497e70d4", "d497e70d4c3e4efeab1381476bac4c5e");
PlatformConfig.setPinterest("1439206");
PlatformConfig.setKakao("e4f60e065048eb031e235c806b31c70f");
PlatformConfig.setDing("dingoalmlnohc0wggfedpk");
PlatformConfig.setVKontakte("5764965","5My6SNliAaLxEm3Lyd9J");
PlatformConfig.setDropbox("oz8v5apet3arcdy","h7p2pjbzkkxt02a");
8.复制分享 不带面板和带面板
@OnClick({R.id.btn_notshare, R.id.btn_share, R.id.btn_shareThread, R.id.image_icon})
public void onViewClicked(View view) {
UMImage image = new UMImage(QQActivity.this, R.drawable.jia);//网络图片
switch (view.getId()) {
case R.id.btn_notshare:
new ShareAction(QQActivity.this)
.setPlatform(SHARE_MEDIA.QQ)//传入平台
.withText("hello")//分享内容
.setCallback(umShareListener)//回调监听器
.share();
break;
case R.id.btn_share:
new ShareAction(QQActivity.this)
.withText("hello")
.setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN)
.withMedia(image)
.setCallback(umShareListener).open();
break;
case R.id.btn_shareThread:
UMShareAPI mShareAPI=UMShareAPI.get(this);
mShareAPI.getPlatformInfo(QQActivity.this, SHARE_MEDIA.QQ, authListener);
break;
case R.id.image_icon:
break;
}
}
9.设置回调监听
UMShareListener umShareListener = new UMShareListener() {
/**
* @descrption 分享开始的回调
* @param platform 平台类型
*/
@Override
public void onStart(SHARE_MEDIA platform) {
}
/**
* @descrption 分享成功的回调
* @param platform 平台类型
*/
@Override
public void onResult(SHARE_MEDIA platform) {
Toast.makeText(QQActivity.this, "成功了", Toast.LENGTH_LONG).show();
}
/**
* @descrption 分享失败的回调
* @param platform 平台类型
* @param t 错误原因
*/
@Override
public void onError(SHARE_MEDIA platform, Throwable t) {
Toast.makeText(QQActivity.this, "失败" + t.getMessage(), Toast.LENGTH_LONG).show();
}
/**
* @descrption 分享取消的回调
* @param platform 平台类型
*/
@Override
public void onCancel(SHARE_MEDIA platform) {
Toast.makeText(QQActivity.this, "取消了", Toast.LENGTH_LONG).show();
}
};
10.添加图片对象
11.第三方登录
创建友盟分享的封装对象
12.接口回调
UMAuthListener authListener = new UMAuthListener() {
/**
* @desc 授权开始的回调
* @param platform 平台名称
*/
@Override
public void onStart(SHARE_MEDIA platform) {
}
/**
* @desc 授权成功的回调
* @param platform 平台名称
* @param action 行为序号,开发者用不上
* @param data 用户资料返回
*/
@Override
public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
Toast.makeText(QQActivity.this, "成功了", Toast.LENGTH_LONG).show();
}
/**
* @desc 授权失败的回调
* @param platform 平台名称
* @param action 行为序号,开发者用不上
* @param t 错误原因
*/
@Override
public void onError(SHARE_MEDIA platform, int action, Throwable t) {
Toast.makeText(QQActivity.this, "失败:" + t.getMessage(),Toast.LENGTH_LONG).show();
}
/**
* @desc 授权取消的回调
* @param platform 平台名称
* @param action 行为序号,开发者用不上
*/
@Override
public void onCancel(SHARE_MEDIA platform, int action) {
Toast.makeText(QQActivity.this, "取消了", Toast.LENGTH_LONG).show();
}
};