ShareSDK Android 使用

 

 

社交分享组件有很多 介绍一下sharesdk 的使用

官网:http://sharesdk.cn/

 

1、先上效果图

 

ShareSDK Android 使用_第1张图片

2、主要代码:

 

public class TestShare extends Activity {

	private RelativeLayout sina;
	private RelativeLayout sms;
	private RelativeLayout qq;
	private RelativeLayout chat;
	private TextView invite_code;

	private Context context;
	private String[] items = new String[] { "分享给好友", "分享到朋友圈" };
	private String share_content;
	private String user_id;

	private static final int SHARE_SUCCESS = 30;
	private static final int SHARE_FAIL = 31;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.test_share);
		context = TestShare.this;

		ShareSDK.initSDK(this);

		sina = (RelativeLayout) this.findViewById(R.id.share_sina);
		qq = (RelativeLayout) this.findViewById(R.id.share_qq);
		chat = (RelativeLayout) this.findViewById(R.id.share_chat);
		sms = (RelativeLayout) this.findViewById(R.id.share_sms);
		share_content = this.getResources().getString(R.string.share_content);
		sina.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 分享到新浪微博
				// showShare(true, SinaWeibo.NAME,TestShare.this);
				share(share_content, null, SinaWeibo.NAME);
			}
		});

		qq.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 分享到腾讯微博
				// showShare(true, TencentWeibo.NAME, TestShare.this);
				share(share_content, null, TencentWeibo.NAME);
			}
		});

		chat.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				showChatDialog();

			}
		});

		sms.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sendSms(share_content, "");

			}
		});
	}

	// 新浪微博分享 腾讯微博等只需修改 NAME
	public void share(String text, String photopath, String sharename) {

		Platform.ShareParams sp = new SinaWeibo.ShareParams();
		sp.text = text;

		if (photopath != null) {
			// sp.imagePath = "/mnt/sdcard/测试分享的图片.jpg";
			sp.imagePath = photopath;

		}

		Platform weibo = ShareSDK.getPlatform(context, sharename);

		// 设置分享事件回调
		weibo.setPlatformActionListener(new PlatformActionListener() {

			public void onError(Platform platform, int action, Throwable t) {
				// 操作失败的处理代码
				Message m = handler.obtainMessage();
				m.what = SHARE_FAIL;
				TestShare.this.handler.sendMessage(m);
			}

			public void onComplete(Platform platform, int action,
					HashMap<String, Object> res) {
				// 操作成功的处理代码
				Message m = handler.obtainMessage();
				m.what = SHARE_SUCCESS;
				TestShare.this.handler.sendMessage(m);
			}

			public void onCancel(Platform platform, int action) {
				// 操作取消的处理代码
			}

		});

		// 执行图文分享
		weibo.share(sp);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		ShareSDK.stopSDK(this);
		super.onDestroy();
	}

	// 发送短信
	public void sendSms(final String content, final String to) {
		Uri smsToUri = Uri.parse("smsto:".concat(to));
		Intent intent = new Intent(android.content.Intent.ACTION_SENDTO,
				smsToUri);
		intent.putExtra("sms_body", content);
		startActivity(intent);
	}

	/**
	 * 显示选择对话框
	 */
	private void showChatDialog() {

		new AlertDialog.Builder(this).setTitle("分享到")
				.setItems(items, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						switch (which) {
						case 0:
							share(share_content, null, Wechat.NAME);
							break;
						case 1:

							share(share_content, null, WechatMoments.NAME);
							break;
						}
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				}).show();

	}

	private Handler handler = new Handler() {
		/*
		 * (non-Javadoc)
		 * 
		 * @see android.os.Handler#handleMessage(android.os.Message)
		 */
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub

			switch (msg.what) {
			case SHARE_SUCCESS:

				Toast.makeText(TestShare.this, "分享成功", Toast.LENGTH_SHORT)
						.show();
				break;
			case SHARE_FAIL:

				Toast.makeText(TestShare.this, "分享失败", Toast.LENGTH_SHORT)
						.show();
				break;

			}
		}

	};

}


 


3、  需要的jar包  ShareSDK-Core.jar 是sdk核心包 ,Wechat是微信核心包,Wechat分享到好友,Wechat-Moments 分享到朋友圈。

       另 android-support-v4.jar 需要使用官网下载的。

 

4、代码混淆

    混淆文件在demo中说明,直接更改就可以使用

   

 

 

 

 

你可能感兴趣的:(ShareSDK Android 使用)