Cordova 自定义插件,包含有Jar包的插件

来源:互联网 发布:世界上第一位程序员 编辑:程序博客网 时间:2024/05/16 08:06

转载请标明pcaxb的博客:http://blog.csdn.net/pcaxb/article/details/51202150


1.我们来创建一个自定义的插件,在你的硬盘中如我的G盘创建一个目录如下(说明:可以任意盘)  这里我们新建了一个  org.apache.cordova.mtoast的文件夹 当然这个名字可以随意,不过我们可以仿照他的默认的插件命名规则来


2. org.apache.cordova.mtoast,这个目录就是存放你自定义插件的根目录,在这个目录下面我们先新建这么几个文件 src文件  www文件  和plugin.xml  如下图

3.新建完成以后我们打开 src目录,然后在下面新建android目录如下图


 

4.新建完成android目录以后我们打开 在里面新建一个 MToast.java   如下图(注意java类名的命名规则)MToast.java里面的代码如下  一个简单的调用返回代码,和以前开发中写法一样的

这里的代码就是需要执行的插件代码,作用是通过客户端传递的参数再返回给客户端,其中execute方法是必须的,供客户端调用。

package org.apach.cordova.mtoast;import org.apache.cordova.CallbackContext;import org.apache.cordova.CordovaPlugin;import org.json.JSONArray;import org.json.JSONException;import android.widget.Toast;public class MToast extends CordovaPlugin{//Java类中没有什么太多的限制,注意action和args参数就可以了,通过action判断需要执行哪里的代码@Overridepublic boolean execute(String action, JSONArray args,final CallbackContext callbackContext) throws JSONException {// TODO Auto-generated method stub//return super.execute(action, args, callbackContext);if ("showAction".equals(action)) {final String content = args.getString(0);cordova.getActivity().runOnUiThread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubmToast(content);callbackContext.success();}});return true;}else if ("showTest".equals(action)) {final String content = args.getString(0);cordova.getActivity().runOnUiThread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubmToastTest(content);callbackContext.success();}});return true;}else{callbackContext.error("这不是一个Toast操作");return false;}}private void mToast(String content){       Toast.makeText(webView.getContext(), content, Toast.LENGTH_SHORT).show();}private void mToastTest(String content){       Toast.makeText(webView.getContext(), content+"mToastTest", Toast.LENGTH_SHORT).show();}}

至于其他的图function变量,我们完全可以自己定义

这里是与html页面交互的js代码,mToastJs.js

/** * Created by panchuanai on 16/4/20. *///这个是固定的var cordova=require('cordova');//mToastJs可以随便取名var mToastJs = function(){}//showMToast随便取名,就是调用的时候的方法名mToastJs.prototype.showMToast = function(message,successCallback, errorCallback){    alert(message);    //cordova对应上面的cordova    //MToast对应Java的类名,showAction对应类中的Action,通过这个Action判断Java类中要执行的代码    cordova.exec(successCallback,errorCallback,"MToast","showAction",[message]);}//同理mToastJs.prototype.showMToastTest = function(message,successCallback, errorCallback){    alert(message);    cordova.exec(successCallback,errorCallback,"MToast","showAction",[message]);}//这里和plugin中的//    <js-module src="www/mToastJs.js" name="mToastJs">//    <clobbers target="cordova.plugins.mToastJs" />//    </js-module>//和plugin.xml中这里的代码对应if(!cordova.plugins){    cordova.plugins = {};}if(!cordova.plugins.mToastJs){    cordova.plugins.mToastJs = new mToastJs();}//module.exports = new mToastJs();

 


6. 下一步是要写一个plugin.xml文件,用于命令行去进行插件的注册,在org.apache.cordova.mtoast文件夹下建立一个plugin.xml文件,刚才我们已经新建好了,代码如下:

<?xml version="1.0" encoding="UTF-8"?><plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"        id="org.apach.cordova.mtoast"    version="0.0.1">    <name>MToast</name>    <description>MToast Plugin</description>    <license>Apache 2.0</license>    <keywords>cordova,mtoast</keywords>        <js-module src="www/mToastJs.js" name="mToastJs">        <clobbers target="cordova.plugins.mToastJs" />    </js-module>          <!-- android -->    <platform name="android"><source-file src="src/android/MToast.java" target-dir="src/org/apach/cordova/mtoast" />        <config-file target="res/xml/config.xml" parent="/*">            <feature name="MToast">                <param name="android-package" value="org.apach.cordova.mtoast.MToast"/>            </feature>        </config-file>        <!--<config-file target="AndroidManifest.xml" parent="/*">-->            <!--<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />-->        <!--</config-file>-->     </platform>   </plugin>        <!--        1.id="org.apach.cordova.mtoast" 这里对应Java类的包名        2.这里的是看你自己的写就可以了        version="0.0.1">        <name>MToast</name>        <description>MToast Plugin</description>        <license>Apache 2.0</license>        <keywords>cordova,mtoast</keywords>        3.        其中js-module指向的就是我们刚才所写的js文件的路径        src指向www/mToastJs.js,name属性自己定义        clobbers代表我们在客户端调用的时候的实例名,可以随便写,但是要和www中的Js中的名字对应        <js-module src="www/mToastJs.js" name="mToastJs">        <clobbers target="cordova.plugins.mToastJs" />        </js-module>        4.下面最重要的platform节点代表每个平台下的配置        <platform name="android"><source-file src="src/android/MToast.java" target-dir="src/org/apach/cordova/mtoast" />        src代表Java源文件的地址,target-dir代表你要拷贝到项目中的地址        <config-file target="res/xml/config.xml" parent="/*">            <feature name="MToast">                <param name="android-package" value="org.apach.cordova.mtoast.MToast"/>            </feature>        </config-file>        这个是使用命令行之后添加到config文件中的节点,其中的value代表从插件中Java文件的路径及类名(全路径)        这里是该插件的权限配置        <config-file target="AndroidManifest.xml" parent="/*">        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />        </config-file>        </platform><span style="font-size:18px;">        --></span>

这样整个插件就已经完成了

如果是涉及到Jar包的请参考下面的配置,这个比较全面

<?xml version="1.0" encoding="UTF-8"?><plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"        xmlns:android="http://schemas.android.com/apk/res/android"        id="cn.jpush.phonegap.JPushPlugin"        version="2.0.2">    <name>JPush Plugin</name>    <description>JPush for cordova plugin</description>    <author>JPush</author>    <keywords>JPush,push</keywords>    <license>Apache 2.0 License</license>    <engines>        <engine name="cordova" version=">=3.0"/>    </engines>    <js-module src="www/JPushPlugin.js" name="JPushPlugin">            <clobbers target="window.plugins.jPushPlugin"/>    </js-module>    <platform name="ios">            <config-file target="config.xml" parent="/*">                <feature name="JPushPlugin">                    <param name="ios-package" value="JPushPlugin"/>                </feature>            </config-file>            <header-file src="src/ios/Plugins/JPushPlugin.h"/>            <source-file src="src/ios/Plugins/JPushPlugin.m"/>            <header-file src="src/ios/lib/APService.h" />            <source-file src="src/ios/lib/libPushSDK-1.8.3.a" framework="true" />            <header-file src="src/ios/Plugins/AppDelegate+JPush.h"/>            <source-file src="src/ios/Plugins/AppDelegate+JPush.m"/>            <resource-file src="src/ios/PushConfig.plist" />            <framework src="CFNetwork.framework" weak="true" />            <framework src="CoreFoundation.framework" weak="true" />            <framework src="CoreTelephony.framework" weak="true" />            <framework src="SystemConfiguration.framework" weak="true" />            <framework src="CoreGraphics.framework" weak="true" />            <framework src="Foundation.framework" weak="true" />            <framework src="UIKit.framework" weak="true" />            <framework src="Security.framework" weak="true" />            <framework src="libz.dylib" weak="true" />        </platform>    <!-- android -->    <platform name="android">        <config-file target="res/xml/config.xml" parent="/*">            <feature name="JPushPlugin">                <param name="android-package" value="cn.jpush.phonegap.JPushPlugin"/>            </feature>        </config-file>        <config-file target="AndroidManifest.xml" parent="/manifest">            <!-- Required  一些系统要求的权限,如访问网络等-->            <uses-permission android:name="$PACKAGE_NAME.permission.JPUSH_MESSAGE"/>            <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT"/>            <uses-permission android:name="android.permission.INTERNET"/>            <uses-permission android:name="android.permission.WAKE_LOCK"/>            <uses-permission android:name="android.permission.READ_PHONE_STATE"/>            <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>            <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>            <uses-permission android:name="android.permission.VIBRATE"/>            <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>            <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>            <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/><uses-permission android:name="android.permission.WRITE_SETTINGS"/>            <permission                    android:name="$PACKAGE_NAME.permission.JPUSH_MESSAGE"                    android:protectionLevel="signature"/>        </config-file>        <config-file target="AndroidManifest.xml" parent="/manifest/application">            <!-- Required SDK核心功能-->            <activity                    android:name="cn.jpush.android.ui.PushActivity"                    android:theme="@android:style/Theme.Translucent.NoTitleBar"                    android:configChanges="orientation|keyboardHidden">                <intent-filter>                    <action android:name="cn.jpush.android.ui.PushActivity"/>                    <category android:name="android.intent.category.DEFAULT"/>                    <category android:name="$PACKAGE_NAME"/>                </intent-filter>            </activity>            <!-- Required  SDK核心功能-->            <service                    android:name="cn.jpush.android.service.DownloadService"                    android:enabled="true"                    android:exported="false">            </service>            <!-- Required SDK 核心功能-->            <service                    android:name="cn.jpush.android.service.PushService"                    android:enabled="true"                    android:exported="false">                <intent-filter>                    <action android:name="cn.jpush.android.intent.REGISTER"/>                    <action android:name="cn.jpush.android.intent.REPORT"/>                    <action android:name="cn.jpush.android.intent.PushService"/>                    <action android:name="cn.jpush.android.intent.PUSH_TIME"/>                </intent-filter>            </service>            <!-- Required SDK核心功能-->            <receiver                    android:name="cn.jpush.android.service.PushReceiver"                    android:enabled="true">                <intent-filter android:priority="1000">                    <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY"/>                    <!--Required  显示通知栏 -->                    <category android:name="$PACKAGE_NAME"/>                </intent-filter>                <intent-filter>                    <action android:name="android.intent.action.USER_PRESENT"/>                    <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>                </intent-filter>                <!-- Optional -->                <intent-filter>                    <action android:name="android.intent.action.PACKAGE_ADDED"/>                    <action android:name="android.intent.action.PACKAGE_REMOVED"/>                    <data android:scheme="package"/>                </intent-filter>            </receiver>            <!-- User defined.  For test only  用户自定义的广播接收器 -->            <receiver                    android:name="cn.jpush.phonegap.MyReceiver"                    android:enabled="true">                <intent-filter android:priority="1000">                    <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY"/>                    <!-- Required  显示通知栏 -->                    <category android:name="$PACKAGE_NAME"/>                </intent-filter>                <intent-filter>                    <action android:name="cn.jpush.android.intent.REGISTRATION"/>                    <!-- Required  用户注册SDK的intent -->                    <action android:name="cn.jpush.android.intent.UNREGISTRATION"/>                    <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED"/>                    <!-- Required  用户接收SDK消息的intent -->                    <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED"/>                    <!-- Required  用户接收SDK通知栏信息的intent -->                    <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED"/>                    <!-- Required  用户打开自定义通知栏的intent -->                    <action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK"/>                    <!-- Optional 用户接受Rich Push Javascript 回调函数的intent -->                    <category android:name="$PACKAGE_NAME"/>                </intent-filter>            </receiver>            <!-- Required SDK核心功能-->            <receiver android:name="cn.jpush.android.service.AlarmReceiver"/>            <!-- Required  . Enable it you can get statistics data with channel -->            <meta-data android:name="JPUSH_CHANNEL" android:value="developer-default"/>            <meta-data android:name="JPUSH_APPKEY" android:value="your appkey"/>        </config-file>        <source-file src="src/android/jpush-sdk-release1.7.5.jar" target-dir="libs"/>        <source-file src="src/android/armeabi/libjpush175.so" target-dir="libs/armeabi"/>        <source-file src="src/android/armeabi-v7a/libjpush175.so" target-dir="libs/armeabi-v7a"/>        <source-file src="src/android/arm64-v8a/libjpush175.so" target-dir="libs/arm64-v8a"/>        <source-file src="src/android/JPushPlugin.java" target-dir="src/cn/jpush/phonegap"/>        <source-file src="src/android/MyReceiver.java" target-dir="src/cn/jpush/phonegap"/>      <source-file src="src/android/test_notification_layout.xml" target-dir="res/layout"/>      <source-file src="src/android/jpush_notification_icon.png" target-dir="res/drawable"/>    </platform></plugin>


配置之后可以到工程中看到:



转载请标明pcaxb的博客:http://blog.csdn.net/pcaxb/article/details/51202150

测试插件下载地址:http://download.csdn.net/detail/pcaxb/9498030


0 0
原创粉丝点击