cordova plugin实践

android res/xml/config.xml

        <feature name="XXXPlugin">
        <param name="android-package" value="com.yyy.plugins.XXXPlugin"/>
    </feature>

android src/com/yyy/plugins/XXXPlugin.java

此插件的作用是跳转到微信APP上。

package com.smbc.plugins;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.json.JSONArray;

import android.content.pm.PackageManager;
import android.net.Uri;
import android.util.Log;
import android.content.Context;
import android.content.Intent;

public class XXXPlugin extends CordovaPlugin{
	private final String pluginName = "XXXPlugin";

	@Override
	public boolean execute(final String action, final JSONArray data, final CallbackContext callbackContext) {

		boolean result = false;

		this.show(data, callbackContext);
		result = true;

		return result;
	}
	public synchronized void show(final JSONArray data, final CallbackContext callbackContext) {
		Context currentCtx = cordova.getActivity();

        try {
            PackageManager packageManager = currentCtx.getPackageManager();
            Intent intent = new Intent();
            intent = packageManager.getLaunchIntentForPackage("com.tencent.mm");
            currentCtx.startActivity(intent);
            callbackContext.success("success");

        } catch (Exception e) {
            e.printStackTrace();
            Intent viewIntent = new
                    Intent("android.intent.action.VIEW", Uri.parse("http://weixin.qq.com/"));
            currentCtx.startActivity(viewIntent);

            callbackContext.error("error");
        }

	}
}

GotoApp.js

var GotoApp = function() {
};
GotoApp.prototype.show= function(options,callback, errorCallback)  {

		var defaults = {
		};
	
		console.log('Invoke Plugin!');
		utility.invokePlugin(//不都这么写。。。
				"GotoAppPlugin",
				'showApp',
				[defaults],
				callback,
				errorCallback
	);
}
在HTML里写一个按钮 点击后跳转到微信APP 方法名是  jumpToApp

ctrl里这么写

		$scope.jumpToApp=function(){
			var options = {

				};
			var gotoApp = new GotoApp();
			gotoApp.show(options,onSuccess,onError);
		}
		function onSuccess(success){
			alert(success);
		}
		function onError(error){
			alert(error);
		}
index.html里需要配置下

            <script src="js/plugins/GotoApp.js"></script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值