关于oneKeyshare用法

由于项目需求,要加入分享功能,所以研究了一下oneKeyShare,下面是它的用法,方便大家使用;

首先引入onekeyshare工程,下载地址;

点击打开链接


第二步:选择你要分享的事件应该写到哪里,然后粘贴下面方法,

	private String  shareurl = RequestUrl.pathDownloadApk;
	/**
	 * 展示一键分享
	 * 
	 * @param silent
	 *            是否直接分享
	 * @param platform
	 *            平台
	 */
	private void showShare(boolean silent, String platform) {
		final OnekeyShare oks = new OnekeyShare();
		oks.setNotification(R.drawable.ic_launcher, view.getContext()
				.getString(R.string.app_name));
		// oks.setAddress("12345678901");
		// oks.setTitle(MainActivity.this.getString(R.string.evenote_title));
		// oks.setTitleUrl("http://sharesdk.cn");
		oks.setTitle(view.getContext().getString(R.string.app_name));

		 oks.setText("惠话网络电话下载地址"
		 + shareurl
		 + "     (分享自:"
		 + view.getContext()
		 .getString(R.string.app_name) + ")");
		oks.setUrl(shareurl);
		// oks.setImagePath(MainActivity.TEST_IMAGE);
		// oks.setImageUrl(MainActivity.TEST_IMAGE_URL);
		// oks.setUrl(url);
		// oks.setFilePath(MainActivity.TEST_IMAGE);
		// oks.setComment(MainActivity.this.getString(R.string.share));
		// oks.setSite(MainActivity.this.getString(R.string.app_name));
		// oks.setSiteUrl(url);
		// oks.setVenueName("ShareSDK");
		// oks.setVenueDescription("This is a beautiful place!");
		// oks.setLatitude(23.056081f);
		// oks.setLongitude(113.385708f);
		oks.setSilent(silent);
		if (platform != null) {
			oks.setPlatform(platform);
		}

		// 取消注释,可以实现对具体的View进行截屏分享
		// oks.setViewToShare(getPage());

		// 去除注释,可令编辑页面显示为Dialog模式
		// oks.setDialogMode();

		// 去除注释,在自动授权时可以禁用SSO方式
		// oks.disableSSOWhenAuthorize();

		// 去除注释,则快捷分享的操作结果将通过OneKeyShareCallback回调
		// oks.setCallback(new OneKeyShareCallback());
		// oks.setShareContentCustomizeCallback(new
		// ShareContentCustomizeDemo());

		// 去除注释,演示在九宫格设置自定义的图标
		// Bitmap logo = BitmapFactory.decodeResource(menu.getResources(),
		// R.drawable.ic_launcher);
		// String label = menu.getResources().getString(R.string.app_name);
		// OnClickListener listener = new OnClickListener() {
		// public void onClick(View v) {
		// String text = "Customer Logo -- ShareSDK " +
		// ShareSDK.getSDKVersionName();
		// Toast.makeText(menu.getContext(), text, Toast.LENGTH_SHORT).show();
		// oks.finish();
		// }
		// };
		// oks.setCustomerLogo(logo, label, listener);

		// 去除注释,则快捷分享九宫格中将隐藏新浪微博和腾讯微博
		// oks.addHiddenPlatform(SinaWeibo.NAME);
		// oks.addHiddenPlatform(TencentWeibo.NAME);
		oks.addHiddenPlatform(QZone.NAME);
		oks.addHiddenPlatform(WechatFavorite.NAME);
		oks.addHiddenPlatform(Facebook.NAME);
		oks.addHiddenPlatform(Twitter.NAME);
		oks.addHiddenPlatform(Renren.NAME);
		oks.addHiddenPlatform(KaiXin.NAME);
		oks.addHiddenPlatform(Email.NAME);
		oks.addHiddenPlatform(ShortMessage.NAME);
		oks.addHiddenPlatform(SohuMicroBlog.NAME);
		oks.addHiddenPlatform(NetEaseMicroBlog.NAME);
		oks.addHiddenPlatform(Douban.NAME);
		oks.addHiddenPlatform(YouDao.NAME);
		oks.addHiddenPlatform(SohuSuishenkan.NAME);
		oks.addHiddenPlatform(Evernote.NAME);
		oks.addHiddenPlatform(LinkedIn.NAME);
		oks.addHiddenPlatform(GooglePlus.NAME);
		oks.addHiddenPlatform(FourSquare.NAME);
		oks.addHiddenPlatform(Pinterest.NAME);
		oks.addHiddenPlatform(Flickr.NAME);
		oks.addHiddenPlatform(Tumblr.NAME);
		oks.addHiddenPlatform(Dropbox.NAME);
		oks.addHiddenPlatform(VKontakte.NAME);
		oks.addHiddenPlatform(Instagram.NAME);
		oks.addHiddenPlatform(Yixin.NAME);
		oks.addHiddenPlatform(YixinMoments.NAME);
		oks.addHiddenPlatform(Mingdao.NAME);
		oks.addHiddenPlatform(TencentWeibo.NAME);
		oks.addHiddenPlatform(WechatMoments.NAME);
		oks.setShareContentCustomizeCallback(new ShareContentCustomizeDemo());
		oks.show(view.getContext());
	}

	// 把图片从drawable复制到sdcard中
	// copy the picture from the drawable to sdcard
	private void initImagePath() {

		try {
			if (Environment.MEDIA_MOUNTED.equals(Environment
					.getExternalStorageState())
					&& Environment.getExternalStorageDirectory().exists()) {
				imagePath = Environment.getExternalStorageDirectory()
						.getAbsolutePath() + FILE_NAME;
			} else {
				imagePath = view.getContext().getApplicationContext()
						.getFilesDir().getAbsolutePath()
						+ FILE_NAME;
			}
			File file = new File(imagePath);
			if (!file.exists()) {
				file.createNewFile();
				pic = BitmapFactory.decodeResource(view.getResources(),
						R.drawable.ic_launcher);
				FileOutputStream fos = new FileOutputStream(file);
				pic.compress(CompressFormat.JPEG, 100, fos);
				fos.flush();
				fos.close();
			}
		} catch (Throwable t) {
			t.printStackTrace();
			imagePath = null;
		}
	}

	/**
	 * 快捷分享项目现在添加为不同的平台添加不同分享内容的方法。 本类用于演示如何区别Twitter的分享内容和其他平台分享内容。
	 */
	public class ShareContentCustomizeDemo implements
			ShareContentCustomizeCallback {

		@Override
		public void onShare(Platform platform,
				cn.sharesdk.framework.Platform.ShareParams paramsToShare) {

			if (SinaWeibo.NAME.equals(platform.getName())) {
				// paramsToShare.setImagePath(imagePath);
				// paramsToShare.setImageUrl(imageUrl);

			} else if (QQ.NAME.equals(platform.getName())) {
				paramsToShare.setImagePath(imagePath);
				// paramsToShare.setImageUrl(imageUrl);
				// paramsToShare.setText(articleTitle + shareurl);

			} else if (Wechat.NAME.equals(platform.getName())) {

			}

		}

	}
这几个方法粘到你所需要加入一键分享的类中之后,不要忘记在oncreat方法中加入

// 分享
		ShareSDK.initSDK(view.getContext());
		new Thread() {
			public void run() {
				initImagePath();
			}
		}.start();
然后再在你的监听事件中调用 
showShare(boolean silent, String platform)方法 两个参数第一个false,第二个null
第三步:

在AndroidManifest.xml中加入


最后在assets文件夹目录下加入

ShareSDK.xml

其中内容粘入如下代码:




    

    
 


    

    

     

    


按要求每个appid appkey都自己申请得到,微信时间可能稍微长点,需要七天,其他的都好申请。
将上面xml中的appid appkey按要求填写好,然后一键分享功能就大功告成。




你可能感兴趣的:(android学习笔记)