第一步:
去友盟官网注册账号,创建一个应用(应用名与项目名保持一致),这个详细步骤可参考友盟QQ登录 此次略过,
正文
导入jar包
添加权限
<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.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
清单配置
<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.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
在Application中配置
{
//这里只做了QQ,微信,微博,其他平台同理
PlatformConfig.setWeixin("wx967daebe835fbeac", "5bb696d9ccd75a38c8a0bfe0675559b3");
PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba");
PlatformConfig.setSinaWeibo("3921700954", "04b48b094faeb16683c32669824ebdad", "http://sns.whalecloud.com");
}
@Override
public void onCreate() {
super.onCreate();
UMShareAPI.get(this);
}
需要调用分享面板从下载的SDK中拷贝socialize_share_menu_item布局文件
分享面板中应用名显示未知从value文件夹拷贝umeng_socialize_strings
布局文件中有自带的一些资源文件,需要从drowable中拷贝
吧SDK中的debug.keystore拷贝到工程目录下
java代码
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);
}
在点击分享时:调起分享面板
new ShareAction(getActivity()).setDisplayList(SHARE_MEDIA.SINA,SHARE_MEDIA.QQ,SHARE_MEDIA.QZONE,SHARE_MEDIA.WEIXIN)
.setContentList(new ShareContent(),new ShareContent())
.withText("title")
.setListenerList(shareListener,shareListener)
.setShareboardclickCallback(shareBoardlistener)
.open();
有两个监听,一个是分享监听成功/失败,一个是多平台的分享
所以创建这两个监听事件
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(getActivity(),"add button success",Toast.LENGTH_LONG).show();
}
}
else {
UMImage image = new UMImage(getActivity(),
BitmapFactory.decodeResource(getResources(), R.drawable.datu));
new ShareAction(getActivity()).setPlatform(share_media).withMedia(image).setCallback(shareListener)
.withText("多平台分享")
.share();
}
}
};
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) {
Toast.makeText(getActivity(),"成功了",Toast.LENGTH_LONG).show();
}
/**
* @descrption 分享失败的回调
* @param platform 平台类型
* @param t 错误原因
*/
@Override
public void onError(SHARE_MEDIA platform, Throwable t) {
Toast.makeText(getActivity(),"失败"+t.getMessage(),Toast.LENGTH_LONG).show();
}
/**
* @descrption 分享取消的回调
* @param platform 平台类型
*/
@Override
public void onCancel(SHARE_MEDIA platform) {
Toast.makeText(getActivity(),"取消了",Toast.LENGTH_LONG).show();
}
};
最后添加回掉方法
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
UMShareAPI.get(getActivity()).onActivityResult( requestCode, resultCode, data);
}
完美实现多平台分享,主意微信需要自己创建一个activity继承友盟微信activity