因为项目需要,找了很多资料,也走了不少弯路,现在把友盟分享demo贴出,供大家参考。
源码下载:下载源码
1.MyApplication.java
//初始化参数
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//各个平台的配置,建议放在全局Application或者程序入口
//微信
PlatformConfig.setWeixin("xxx", "xxx");
//新浪
PlatformConfig.setSinaWeibo("xxx", "xxx");
//qq qqzone
PlatformConfig.setQQZone("xxx", "xxx");
}
}
2.MyActivity.java
/**
* 友盟分享:QQ测试通过,微信、新浪客户端分享需要根据生成正式签名的apk
* Created by Administrator on 2016/7/26 0026.
*/
public class MyActivity extends AppCompatActivity {
private Button btn;
private Context context = MyActivity.this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
btn = (Button) findViewById(R.id.share);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
show2();
}
});
}
/**
* 弹出分享对话框,根据需求改变分享内容
*/
private void show() {
//Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.info_icon_1);
UMImage image = new UMImage(context, "http://www.umeng.com/images/pic/social/integrated_3.png");
//UMImage image = new UMImage(ShareActivity.this,bitmap);
//UMImage image = new UMImage(ShareActivity.this,new File("/SDCARD/image_jpg.jpg"));
UMusic music = new UMusic("http://music.huoxing.com/upload/20130330/1364651263157_1085.mp3");
//UMusic music = new UMusic("http://y.qq.com/#type=song&mid=002I7CmS01UAIH&tpl=yqq_song_detail");
music.setTitle("This is music title");
music.setThumb("http://www.umeng.com/images/pic/social/chart_1.png");
music.setDescription("my description");
// share video
UMVideo video = new UMVideo("http://video.sina.com.cn/p/sports/cba/v/2013-10-22/144463050817.html");
video.setThumb("http://www.adiumxtras.com/images/thumbs/dango_menu_bar_icon_set_11_19047_6240_thumb.png");
// share URL
String url = "http://www.umeng.com";
new ShareAction(this).setDisplayList(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE, SHARE_MEDIA.SINA)
.withTitle("我的分享")
.withText("看一看")
.withMedia(image)
.withTargetUrl(url)
.setCallback(umShareListener)
//.withShareBoardDirection(view, Gravity.TOP|Gravity.LEFT)
.open();
// Config.REDIRECT_URL="http://sns.whalecloud.com/sina2/callback";
}
private UMShareListener umShareListener = new UMShareListener() {
@Override
public void onResult(SHARE_MEDIA platform) {
Log.d("plat", "platform" + platform);
if (platform.name().equals("WEIXIN_FAVORITE")) {
Toast.makeText(context, platform + " 收藏成功啦", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onError(SHARE_MEDIA platform, Throwable t) {
Toast.makeText(context, platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
if (t != null) {
Log.d("throw", "throw:" + t.getMessage());
}
}
@Override
public void onCancel(SHARE_MEDIA platform) {
Toast.makeText(context, platform + " 分享取消了", Toast.LENGTH_SHORT).show();
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
/** attention to this below ,must add this**/
UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
Log.d("result","onActivityResult");
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
}
3.MyShareActivity.java
自定义分享页面
public class MyShareActivity extends Activity implements View.OnClickListener {
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_share);
context = this;
RelativeLayout wechat = (RelativeLayout) findViewById(R.id.rl_share_wechat);
RelativeLayout wechat_circle = (RelativeLayout) findViewById(R.id.rl_share_wechat_circle);
RelativeLayout qq = (RelativeLayout) findViewById(R.id.rl_share_qq);
RelativeLayout qqzone = (RelativeLayout) findViewById(R.id.rl_share_qqzone);
RelativeLayout sina = (RelativeLayout) findViewById(R.id.rl_share_sina);
RelativeLayout others = (RelativeLayout) findViewById(R.id.rl_others);
TextView tv_cancle = (TextView) findViewById(R.id.tv_cancle);
wechat.setOnClickListener(this);
wechat_circle.setOnClickListener(this);
qq.setOnClickListener(this);
qqzone.setOnClickListener(this);
sina.setOnClickListener(this);
others.setOnClickListener(this);
tv_cancle.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.rl_share_wechat:
share(SHARE_MEDIA.WEIXIN);
break;
case R.id.rl_share_wechat_circle:
share(SHARE_MEDIA.WEIXIN_CIRCLE);
break;
case R.id.rl_share_qq:
share(SHARE_MEDIA.QQ);
break;
case R.id.rl_share_qqzone:
share(SHARE_MEDIA.QZONE);
break;
case R.id.rl_share_sina:
share(SHARE_MEDIA.SINA);
break;
case R.id.rl_others:
case R.id.tv_cancle:
finish();
overridePendingTransition(0,R.anim.out);
break;
}
}
private void share(SHARE_MEDIA qq) {
//开启自定义分享页面
UMImage image = new UMImage(context, "https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2042276435,923048600&fm=80");
new ShareAction(this)
.setPlatform(qq)
.setCallback(umShareListener)
.withTitle("自定义分享")
.withText("hello world!")
.withTargetUrl("http://www.baidu.com")
.withMedia(image)
.share();
}
private UMShareListener umShareListener = new UMShareListener() {
@Override
public void onResult(SHARE_MEDIA platform) {
Log.d("plat", "platform" + platform);
if (platform.name().equals("WEIXIN_FAVORITE")) {
Toast.makeText(context, platform + " 收藏成功啦", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
}
finish();
}
@Override
public void onError(SHARE_MEDIA platform, Throwable t) {
Toast.makeText(context, platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
if (t != null) {
Log.d("throw", "throw:" + t.getMessage());
}
finish();
}
@Override
public void onCancel(SHARE_MEDIA platform) {
Toast.makeText(context, platform + " 分享取消了", Toast.LENGTH_SHORT).show();
finish();
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
}
}
以上是主要部分,其他请下载demo参考,appkey和appid都已经更改,请更改填写。
下载地址 :http://download.csdn.net/detail/u011106915/9592889