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

phonggap微信分享

Android平台开发

下载此实例
  • 开发语言:Java
  • 实例大小:8.67M
  • 下载次数:14
  • 浏览次数:246
  • 发布时间:2015-10-08
  • 实例类别:Android平台开发
  • 发 布 人:azxcvbnml
  • 文件格式:.zip
  • 所需积分:2
 相关标签: 微信 分享

实例介绍

【实例简介】phonggap微信分享
【实例截图】

【核心代码】

package com.jiusem.cordova.wechat;

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.jiusem.cordova.wechat.wechatshare.Util;
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 WechatShare extends CordovaPlugin {
	private static final String SDCARD_ROOT = Environment.getExternalStorageDirectory().getAbsolutePath();
	private static WechatShare instance;
	//修改为你自己的appID
	public static String appID = "wxd97ca7d7a2fae4dd";
	public WechatShare(){
		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();
				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")) {
				this.register();  
				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.WechatShare.sendCallBack(" String.valueOf(result) ")");
	}
	
	private IWXAPI api;
	
	public void register() {
		Context Activity = this.cordova.getActivity().getApplicationContext();
		api = WXAPIFactory.createWXAPI(Activity, instance.appID, false);		
		api.registerApp(instance.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);
		} 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);
			}
			
			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;
		}
		
		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);
			}**/
			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);
			}*/
			
			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);
			}*/
			
			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);
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	//send webpage
	public void sendFile(String path, String title, String desc, String imgUrl, boolean isSendToTimeline) {
		WXFileObject appdata = new WXFileObject();
		
		if(path.startsWith(SDCARD_ROOT)){
			appdata.filePath = path;
		}else{
			appdata.filePath = SDCARD_ROOT   path;
		}	
		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);
			}*/
			
			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);
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
		
	//unregister
	public void unregister() {
		api.unregisterApp();
	}

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

标签: 微信 分享

实例下载地址

phonggap微信分享

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

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

网友评论

发表评论

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

查看所有0条评论>>

小贴士

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

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

关于好例子网

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

;
报警