在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Android平台开发 → android 微信分享插件 源码下载

android 微信分享插件 源码下载

Android平台开发

下载此实例
  • 开发语言:Java
  • 实例大小:0.19M
  • 下载次数:58
  • 浏览次数:478
  • 发布时间:2014-12-31
  • 实例类别:Android平台开发
  • 发 布 人:jiangmouren
  • 文件格式:.zip
  • 所需积分:0
 相关标签: 微信 插件 分享

实例介绍

【实例简介】调用phoneGap实现分享图片文字和视频等功能
【实例截图】暂无

【核心代码】

package com.phonegap.plugins.weixin;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL;

import com.tencent.mm.sdk.openapi.BaseReq;
import com.tencent.mm.sdk.openapi.BaseResp;
import com.tencent.mm.sdk.openapi.ConstantsAPI;
import com.tencent.mm.sdk.openapi.ShowMessageFromWX;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.mm.sdk.openapi.WXAppExtendObject;
import com.tencent.mm.sdk.openapi.SendMessageToWX;
import com.tencent.mm.sdk.openapi.WXEmojiObject;
import com.tencent.mm.sdk.openapi.WXImageObject;
import com.tencent.mm.sdk.openapi.WXMediaMessage;
import com.tencent.mm.sdk.openapi.WXMusicObject;
import com.tencent.mm.sdk.openapi.WXTextObject;
import com.tencent.mm.sdk.openapi.WXVideoObject;
import com.tencent.mm.sdk.openapi.WXWebpageObject;
import com.tencent.mm.sdk.openapi.SendAuth;
import com.tencent.mm.sdk.openapi.WXFileObject;
import com.phonegap.plugins.weixin.Util;

import com.example.weixin.R;
//import com.example.weixin.WeiXinMain;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.LOG;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.content.DialogInterface;
import android.content.ContextWrapper;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;


public class WeiXin extends CordovaPlugin /*implements IWXAPIEventHandler*/ {
	
	private static final String SDCARD_ROOT = Environment.getExternalStorageDirectory().getAbsolutePath();
	
	private static WeiXin instance;
	
	public WeiXin(){
		instance = this;
	}
	@Override
	public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
		boolean result = false;
        try {
            if (action.equals("register")) {
            	String appId = args.getString(0); 
				this.register(appId);
				callbackContext.success();
                result = true;
            } else if (action.equals("unregister")) { 
				this.unregister();
				callbackContext.success();
                result = true;
            }
            else if (action.equals("openWeixin")) {
				this.open();
				callbackContext.success();
				result = true;
            }else if(action.equals("showToast")){
            	String txt = args.getString(0);
            	this.showToast(txt);
            }
			else if (action.equals("send")) {
				JSONObject cfg = args.getJSONObject(0);
				if (cfg.getString("type").equals("text")) {
					this.sendText(cfg.getString("text"), cfg.getBoolean("isSendToTimeline"));
				} else if (cfg.getString("type").equals("image")) {
					this.sendImage(cfg.getString("data"), cfg.getString("imageType"), cfg.getBoolean("isSendToTimeline"));
				} else if (cfg.getString("type").equals("music")) {
					this.sendMusic(cfg.getString("url"), cfg.getBoolean("isLowBand"), cfg.getString("title"), cfg.getString("desc"), cfg.getString("imgUrl"), cfg.getBoolean("isSendToTimeline"));
				} else if (cfg.getString("type").equals("video")) {
					this.sendVideo(cfg.getString("url"), cfg.getBoolean("isLowBand"), cfg.getString("title"), cfg.getString("desc"), cfg.getString("imgUrl"), cfg.getBoolean("isSendToTimeline"));
				} else if (cfg.getString("type").equals("webpage")) {
					this.sendWebPage(cfg.getString("url"), cfg.getString("title"), cfg.getString("desc"), cfg.getString("imgUrl"), cfg.getBoolean("isSendToTimeline"));
				} else if (cfg.getString("type").equals("file")) {
					this.sendFile(cfg.getString("path"), cfg.getString("title"), cfg.getString("desc"), cfg.getString("imgUrl"), cfg.getBoolean("isSendToTimeline"));
				}
				
				callbackContext.success();
				result = true;
            }
            else {
                result = false;
            }
        } catch (JSONException e) {
            callbackContext.error("JSON Exception");
			result = false;
        }
        
        
		return result;
    }
	
	public void showToast(String txt){
		Context Activity = this.cordova.getActivity().getApplicationContext();
		Toast.makeText(Activity, txt, Toast.LENGTH_LONG).show();
	}

	//回调
	public static void sendCallBack(int result){
		if (instance == null) {
			return;
		}
		instance.webView.sendJavascript("navigator.weixin.sendCallBack(" String.valueOf(result) ")");
	}
	
	private IWXAPI api;
	
	public void register(String appId) {
		Context Activity = this.cordova.getActivity().getApplicationContext();
		api = WXAPIFactory.createWXAPI(Activity, appId, false);		
		api.registerApp(appId);
	}
	
	public void open() {
		api.openWXApp();
		
	}

	
	//send text
	public void sendText(String text, boolean isSendToTimeline) {
		
		try{
			
			WXTextObject textObj = new WXTextObject();
			textObj.text = text;
			
			WXMediaMessage msg = new WXMediaMessage();
			msg.mediaObject = textObj;
			msg.description = text;

			SendMessageToWX.Req req = new SendMessageToWX.Req();
			req.transaction = String.valueOf(System.currentTimeMillis());//buildTransaction("text");
			req.message = msg;
			int wxSdkVersion = api.getWXAppSupportAPI();
			if (wxSdkVersion >= 0x21020001) {
				req.scene = isSendToTimeline ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
			} else {
				req.scene = SendMessageToWX.Req.WXSceneSession;
			}
			api.sendReq(req);
			//this.webView.sendJavascript("navigator.weixin.callback()");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	//send image
	public void sendImage(String data, String imageType, boolean isSendToTimeline) {
		
		File file = null;
		String path = "";
		Bitmap bmp = null;
		try{
			WXImageObject imgObj = new WXImageObject();
			if (imageType.equals("url")) {
				imgObj.imageUrl = data;
				bmp = BitmapFactory.decodeStream(new URL(data).openStream());
				
			} else if (imageType.equals("path")) {
				LOG.d("WeChat Plugin", "file path: "   SDCARD_ROOT   data);
				if(data.startsWith(SDCARD_ROOT)){
					path = data;
				}else{
					path = (SDCARD_ROOT   data).replaceAll(" ", "%20");		
				}
				file = new File(path);
				if (!file.exists()) {
					LOG.d("WeChat Plugin", "file not exists");
					return;
				} else {
					LOG.d("WeChat Plugin", "get file @"   path);
				}
				imgObj.setImagePath(path);
				
				FileInputStream fis = new FileInputStream(path);
				bmp = BitmapFactory.decodeStream(fis);
				//bmp = BitmapFactory.decodeFile(path);
			}
			
			WXMediaMessage msg = new WXMediaMessage();
			msg.mediaObject = imgObj;
			
			Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true);
			bmp.recycle();
			msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
			
			SendMessageToWX.Req req = new SendMessageToWX.Req();
			req.transaction = String.valueOf(System.currentTimeMillis());
			req.message = msg;
			int wxSdkVersion = api.getWXAppSupportAPI();
			if (wxSdkVersion >= 0x21020001) {
				req.scene = isSendToTimeline ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
			} else {
				req.scene = SendMessageToWX.Req.WXSceneSession;
			}
			api.sendReq(req);
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	//send music
	public void sendMusic(String url, boolean isLowBand, String title, String desc, String imgUrl, boolean isSendToTimeline) {
		WXMusicObject music = new WXMusicObject();
		if (isLowBand == false) {
			music.musicUrl = url;
		} else {
			music.musicLowBandUrl = url;
		}
		//"http://staff2.ustc.edu.cn/~wdw/softdown/index.asp/0042515_05.ANDY.mp3";
		//music.musicUrl="http://120.196.211.49/XlFNM14sois/AKVPrOJ9CBnIN556OrWEuGhZvlDF02p5zIXwrZqLUTti4o6MOJ4g7C6FPXmtlh6vPtgbKQ==/31353278.mp3";

		WXMediaMessage msg = new WXMediaMessage();
		msg.mediaObject = music;
		msg.title = title;
		msg.description = desc;
		
		Bitmap bmp = null;
		try{
			if (imgUrl.equals("")) {
				Context Activity = this.cordova.getActivity().getApplicationContext();
				Bitmap thumb = BitmapFactory.decodeResource(Activity.getResources(), R.drawable.music);
				msg.thumbData = Util.bmpToByteArray(thumb, true);
			} else {
				bmp = BitmapFactory.decodeStream(new URL(imgUrl).openStream());
				Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true);
				bmp.recycle();
				msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
			}

			SendMessageToWX.Req req = new SendMessageToWX.Req();
			req.transaction = buildTransaction("music");
			req.message = msg;
			int wxSdkVersion = api.getWXAppSupportAPI();
			if (wxSdkVersion >= 0x21020001) {
				req.scene = isSendToTimeline ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
			} else {
				req.scene = SendMessageToWX.Req.WXSceneSession;
			}
			api.sendReq(req);
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
		
	//send video
	public void sendVideo(String url, boolean isLowBand, String title, String desc, String imgUrl, boolean isSendToTimeline) {
 		WXVideoObject video = new WXVideoObject();
		if (isLowBand == false) {
			video.videoUrl = url;
		} else {
			video.videoLowBandUrl = url;
		}
		
		WXMediaMessage msg = new WXMediaMessage(video);
		msg.title = title;
		msg.description = desc;
		Bitmap bmp = null;
		try{
			if (imgUrl.equals("")) {
				Context Activity = this.cordova.getActivity().getApplicationContext();
				Bitmap thumb = BitmapFactory.decodeResource(Activity.getResources(), R.drawable.video);
				msg.thumbData = Util.bmpToByteArray(thumb, true);
			} else {
				bmp = BitmapFactory.decodeStream(new URL(imgUrl).openStream());
				Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true);
				bmp.recycle();
				msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
			}

			SendMessageToWX.Req req = new SendMessageToWX.Req();
			req.transaction = buildTransaction("video");
			req.message = msg;
			int wxSdkVersion = api.getWXAppSupportAPI();
			if (wxSdkVersion >= 0x21020001) {
				req.scene = isSendToTimeline ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
			} else {
				req.scene = SendMessageToWX.Req.WXSceneSession;
			}
			api.sendReq(req);
			//LOG.d("!!!!!!!!!!!!!!!!!!!!", video.videoUrl);
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	//send webpage
	public void sendWebPage(String url, String title, String desc, String imgUrl, boolean isSendToTimeline) {
		WXWebpageObject webpage = new WXWebpageObject();
		webpage.webpageUrl = url;
		WXMediaMessage msg = new WXMediaMessage(webpage);
		msg.title = title;
		msg.description = desc;
		Bitmap bmp = null;
		try{
			if (imgUrl.equals("")) {
				Context Activity = this.cordova.getActivity().getApplicationContext();
				Bitmap thumb = BitmapFactory.decodeResource(Activity.getResources(), R.drawable.webpage);
				msg.thumbData = Util.bmpToByteArray(thumb, true);
			} else {
				bmp = BitmapFactory.decodeStream(new URL(imgUrl).openStream());
				Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true);
				bmp.recycle();
				msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
			}
			
			SendMessageToWX.Req req = new SendMessageToWX.Req();
			req.transaction = buildTransaction("webpage");
			req.message = msg;
			int wxSdkVersion = api.getWXAppSupportAPI();
			if (wxSdkVersion >= 0x21020001) {
				req.scene = isSendToTimeline ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
			} else {
				req.scene = SendMessageToWX.Req.WXSceneSession;
			}
			api.sendReq(req);
			LOG.d("!!!!!!!!!!!!!!!!!!!!", webpage.webpageUrl);
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	//send webpage
	public void sendFile(String path, String title, String desc, String imgUrl, boolean isSendToTimeline) {
		WXFileObject appdata = new WXFileObject();
		//String pathStr = SDCARD_ROOT   path;
		
		//LOG.d("!!!!!!!!!!!!!!!!!!!!", pathStr);
		if(path.startsWith(SDCARD_ROOT)){
			appdata.filePath = path;
		}else{
			appdata.filePath = SDCARD_ROOT   path;
		}	
		//appdata.fileData = Util.readFromFile(pathStr, 0, -1);
		WXMediaMessage msg = new WXMediaMessage(appdata);
		
		Bitmap bmp = null;
		try{
			if (imgUrl.equals("")) {
				Context Activity = this.cordova.getActivity().getApplicationContext();
				Bitmap thumb = BitmapFactory.decodeResource(Activity.getResources(), R.drawable.file);
				msg.thumbData = Util.bmpToByteArray(thumb, true);
			} else {
				bmp = BitmapFactory.decodeStream(new URL(imgUrl).openStream());
				Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 150, 150, true);
				bmp.recycle();
				msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
			}
			
			msg.title = title;
			msg.description = desc;
			msg.mediaObject = appdata;
			
			SendMessageToWX.Req req = new SendMessageToWX.Req();
			req.transaction = buildTransaction("appdata");
			req.message = msg;
			int wxSdkVersion = api.getWXAppSupportAPI();
			if (wxSdkVersion >= 0x21020001) {
				req.scene = isSendToTimeline ? SendMessageToWX.Req.WXSceneTimeline : SendMessageToWX.Req.WXSceneSession;
			} else {
				req.scene = SendMessageToWX.Req.WXSceneSession;
			}
			api.sendReq(req);
			LOG.d("!!!!!!!!!!!!!!!!!!!!", appdata.filePath);
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
		
	//unregister
	public void unregister() {
		api.unregisterApp();
	}
	
	/**
	@Override
	public void onReq(BaseReq req) {
		LOG.d("req", "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
		switch (req.getType()) {
		case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
			LOG.d("onReq", "COMMAND_GETMESSAGE_FROM_WX");
			System.out.println(ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX);
			break;
		case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
			LOG.d("onReq", "COMMAND_SHOWMESSAGE_FROM_WX");
			System.out.println(ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX);
			break;
		default:
			break;
		}
		
		showToast("发送消息成功");
		
		//this.webView.sendJavascript("navigator.weixin.callback()");
	}

	@Override
	public void onResp(BaseResp resp) {
		LOG.d("resp", "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
		int result = 0;
		
		switch (resp.errCode) {
		case BaseResp.ErrCode.ERR_OK:
			result = 1;
			break;
		case BaseResp.ErrCode.ERR_USER_CANCEL:
			result = 2;
			break;
		case BaseResp.ErrCode.ERR_AUTH_DENIED:
			result = 3;
			break;
		default:
			result = 4;
			break;
		}
		
		showToast("测试信息");
		this.webView.sendJavascript("navigator.weixin.callback()");
		//LOG.d("onResp", "result");
		//LOG.d("WeChat Plugin", "weixin_info");
		//System.out.println(result);
	}
	
**/




	private String buildTransaction(final String type) {
		return (type == null) ? String.valueOf(System.currentTimeMillis()) : type   System.currentTimeMillis();
	}
}

标签: 微信 插件 分享

实例下载地址

android 微信分享插件 源码下载

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警