android端的cordova插件开发(基础篇)
2021-11-23 本文已影响0人
刘坤林
重要前提:
首先明确1件事情,那就是有2个项目,主项目(myapp1)和插件项目(pluginDemo),释义如下:
1.主项目(myapp1):这是需要导入插件的项目。
2.插件项目(pluginDemo):这是个新建插件的项目,可以理解为,插件都由这个项目来管理。
设定需求是:新建一个cordova插件,专门处理微信接口的功能。如打开微信,跳转到微信小程序等功能。
一、创建插件
1、创建插件并初始化
这里请注意,插件id命名不能含有横杠“-”,不然会导致编译不通过,安卓的包名不支持包含横杠的包名。
//切换到插件项目跟目录下
cd E:\cordovaProjects\pluginDemo
//创建插件plugman create --name [插件名] --plugin_id [插件id] --plugin_version [插件版本]
plugman create --name wxUt --plugin_id com.wx.ut --plugin_version 1.0.0
//进入插件目录,然后初始化插件
npm init
操作过程如下图

2、添加插件的平台
//为插件添加平台:android和ios
plugman platform add --platform_name android

3、编辑wxUt插件的配置文件(plugin.xml)

内容如下:
<?xml version='1.0' encoding='utf-8'?>
<plugin id="com.wx.ut" version="1.0.0" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<name>wxUt</name>
<!-- js调用模块:src为调用插件的js文件路径 -->
<js-module name="wxUt" src="www/wxUt.js">
<!-- target为插件安装后的调用方法前缀,如cordova.plugins.wxUt.[插件方法名] -->
<!-- 可以自定义,调用时:wxUt.[插件方法名] -->
<clobbers target="wxUt" />
</js-module>
<!-- android环境配置项 -->
<platform name="android">
<config-file parent="/*" target="res/xml/config.xml">
<feature name="wxUt">
<param name="android-package" value="com.wx.ut.wxUt" />
</feature>
</config-file>
<config-file parent="/*" target="AndroidManifest.xml" />
<!-- src为插件中需要拷贝的文件的插件中的路径-->
<!-- target-dir为插件安装到Android平台后对应的包路径 -->
<!-- 这一将最后一个wxUt去掉 因为我们指定的是文件夹,不删会多一wxUt层包名,导致编译不通过-->
<!-- <source-file src="src/android/wxUt.java" target-dir="src/com/wx/ut/wxUt" /> -->
<source-file src="src/android/wxUt.java" target-dir="src/com/wx/ut/" />
</platform>
<!-- ios环境配置项 -->
<platform name="ios">
<config-file parent="/*" target="config.xml">
<feature name="wxUt">
<param name="ios-package" value="wxUt" />
</feature>
</config-file>
<source-file src="src/ios/wxUt.m" />
</platform>
</plugin>
4、编辑插件js文件
var exec = require('cordova/exec');
//输入安卓日志(测试联调时最佳)
exports.log = function (arg0, success, error) {
exec(success, error, 'wxUt', 'log', [arg0]);
};
//吐司
exports.toast = function (arg0, success, error) {
exec(success, error, 'wxUt', 'toast', [arg0]);
};
//打开微信
exports.toWechat = function (arg0, success, error) {
exec(success, error, 'wxUt', 'toWechat', [arg0]);
};
//打开微信小程序
exports.toWechatApplets = function (arg0, success, error) {
exec(success, error, 'wxUt', 'toWechatApplets', [arg0]);
};
5、修改wxUt.class文件
package com.wx.ut;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.Toast;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
/**
* This class echoes a string called from JavaScript.
*
* @author Administrator
*/
public class wxUt extends CordovaPlugin {
@Override
public boolean execute ( String action, JSONArray args, CallbackContext callbackContext ) throws JSONException {
try {
switch ( action ) {
case "log":
log ( args.getString ( 0 ) );
return true;
case "toast":
toast ( args.getString ( 0 ) );
return true;
case "toWechat":
openWeChat ( cordova.getActivity ( ) );
return true;
case "openAppletLink":
String username = args.getString ( 0 );
String path = args.getString ( 1 );
openAppletLink ( cordova.getActivity ( ), username, path );
return true;
default:
return false;
}
} catch ( Exception e ) {
android.util.Log.e ( "excc", e.getMessage ( ) );
callbackContext.error ( e.getMessage ( ) );
return false;
}
}
private void log ( String s ) {
android.util.Log.i ( "excc", s );
}
private void toast ( String s ) {
Toast.makeText ( cordova.getContext ( ), s, Toast.LENGTH_SHORT ).show ( );
}
private void openWeChat ( Activity activity ) {
log ( "正在打开微信" );
Intent lan = activity.getPackageManager ( ).getLaunchIntentForPackage ( "com.tencent.mm" );
Intent intent = new Intent ( Intent.ACTION_MAIN );
intent.addCategory ( Intent.CATEGORY_LAUNCHER );
intent.addFlags ( Intent.FLAG_ACTIVITY_NEW_TASK );
intent.setComponent ( lan.getComponent ( ) );
activity.startActivity ( intent );
}
public static final String APP_ID = "wxfa842d455a2c1100";
private BroadcastReceiver broadcastReceiver;
private void openAppletLink ( Activity activity, String username, String path ) {
log ( "微信小程序用户名:" + username + ",路径:" + path );
IWXAPI api = WXAPIFactory.createWXAPI ( activity, APP_ID, true );
broadcastReceiver = new BroadcastReceiver ( ) {
@Override
public void onReceive ( Context context, Intent intent ) {
log ( "将该app注册到微信----------------------" );
// 将该app注册到微信
api.registerApp ( APP_ID );
}
};
activity.registerReceiver ( broadcastReceiver,
new IntentFilter ( ConstantsAPI.ACTION_REFRESH_WXAPP ) );
WXLaunchMiniProgram.Req req = new WXLaunchMiniProgram.Req ( );
// 填小程序原始id
req.userName = username;
//拉起小程序页面的可带参路径,不填默认拉起小程序首页,对于小游戏,可以只传入 query 部分,来实现传参效果,如:传入 "?foo=bar"。
req.path = path;
// 可选打开 开发版,体验版和正式版
req.miniprogramType = WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE;
api.sendReq ( req );
}
@Override
public void onDestroy ( ) {
if ( broadcastReceiver != null ) {
cordova.getActivity ( ).unregisterReceiver ( broadcastReceiver );
}
super.onDestroy ( );
}
}
二、添加到主项目
1、检查主项目是否已添加android平台支持
cordova platform ls

2、在住项目中添加插件
#在需要的项目中添加插件cordova plugins add [插件目录]
cordova plugins add E:\cordovaProjects\pluginDemo\wxUt

三、开始验证插件
直接在主项目的index.js文件的初始化方法下调用插件方法,来验证

大功告成
