在sencha touch(worklight)中调用Android代码

HangMine 2014-01-23

worklight项目调原生方法

在senchatouch开发中有些功能实现比较困难,或是某些功能必须使用Android原生代码。这时就需要用senchatouch代码调用Android代码了,可以通过phonegap实现。举例如下(微信分享功能):

首先在Android项目src下添加包com.share,包中新建文件ShareWeiXin.java

文件代码如下

package com.share;

//import org.apache.cordova.CallbackContext;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.net.URL;


import org.apache.cordova.CordovaArgs;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
//import org.apache.cordova.CordovaPlugin;
import org.json.JSONException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.widget.Toast;

import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.SendMessageToWX;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.mm.sdk.openapi.WXImageObject;
import com.tencent.mm.sdk.openapi.WXMediaMessage;
import com.tencent.mm.sdk.openapi.WXTextObject;
import com.tencent.mm.sdk.openapi.WXWebpageObject;

public class ShareWeiXin extends CordovaPlugin {

	private IWXAPI api;
	private static String APP_ID = "wx6cc5eb610de5f0b4";

	private static final int THUMBWIDTH_SIZE = 100;
	private static final int THUMBHEIGHT_SIZE = 150;

	private static final String SDCARD_ROOT = Environment
			.getExternalStorageDirectory().getAbsolutePath();

	@Override
	public boolean execute(String action, CordovaArgs args,
			CallbackContext callbackContext) throws JSONException {
		// TODO Auto-generated method stub
		if (action.equals("init")) {
			init();
			return true;
		} else if (action.equals("openWX")) {
			openWX();
			return true;
		}else if (action.equals("share")) {
			String message = args.getString(0);
			String description = args.getString(1);
			String url = args.getString(2);
			share(message, description, url);
			return true;
		}
		return false;
	}

	private void init() {
		System.out.println("微信初始化");
		api = WXAPIFactory.createWXAPI(this.cordova.getActivity(), APP_ID,
				false);
		api.registerApp(APP_ID);
	}
	
	private void openWX() {
		api.openWXApp();
	}

	private void share(String text, String description, String url) {
		System.out.println("微信分享...");
		System.out.println("微信分享..." + url);
//		String url = "http://images.liqucn.com/h004/h01/img201108170900460.jpg";
		try {
			WXImageObject imgObj = new WXImageObject();
			imgObj.setImagePath(url);
			
			WXMediaMessage msg = new WXMediaMessage();
			msg.mediaObject = imgObj;
			
			Bitmap bmp = BitmapFactory.decodeFile(url);
			bmp = compressImage(bmp);
			Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMBWIDTH_SIZE, THUMBHEIGHT_SIZE, true);
			bmp.recycle();
			msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
			
			SendMessageToWX.Req req = new SendMessageToWX.Req();
			req.transaction = buildTransaction("img");
			req.message = msg;
			req.scene = SendMessageToWX.Req.WXSceneSession;
			api.sendReq(req);
			

		} catch (Exception e) {
			System.out.println(e);
		}

	}


	private String buildTransaction(final String type) {
		return (type == null) ? String.valueOf(System.currentTimeMillis())
				: type + System.currentTimeMillis();
	}
	
	//图片大小压缩
	public static Bitmap compressImage(Bitmap image) {

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
		int options = 100;
		while ( baos.toByteArray().length / 1024>100) {	//循环判断如果压缩后图片是否大于100kb,大于继续压缩		
			baos.reset();//重置baos即清空baos
			image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
			options -= 10;//每次都减少10
		}
		ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
		Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片
		return bitmap;
	}

}

在res-xml下的config.xml中添加:

<plugin name="share" value="com.share.ShareWeiXin" />

然后在sencha代码中调用,这里只例句下调用微信初始化方法:

//微信初始化
		cordova.exec(function(){
			//调用微信分享
			console.log("初始化微信成功...");
		}, function(error){
			console.log("初始化微信失败...");
		}, 'share', 'init', []);

worklight项目调原生页面

相关推荐