cordova自定义一个简单的alert的插件(android平台)
官網(wǎng)的插件開發(fā)文檔:http://cordova.apache.org/docs/en/latest/guide/hybrid/plugins/index.html
插件文件布局:
比如我在d盤新建了如下的布局
--src
????--android
????????--alert.java
--www
????--alert.js
plugin.xml
插件功能:
在前端點擊一個按鈕,提示alert.
文件內容:
????關于plugin.xml文件內容設置可以參考這里http://cordova.apache.org/docs/en/latest/plugin_ref/spec.html
現(xiàn)在我的內容如下:
<?xml?version="1.0"?encoding="UTF-8"?> <!--Licensed?to?the?Apache?Software?Foundation?(ASF)?under?oneor?more?contributor?license?agreements.??See?the?NOTICE?filedistributed?with?this?work?for?additional?informationregarding?copyright?ownership.??The?ASF?licenses?this?fileto?you?under?the?Apache?License,?Version?2.0?(the"License");?you?may?not?use?this?file?except?in?compliancewith?the?License.??You?may?obtain?a?copy?of?the?License?athttp://www.apache.org/licenses/LICENSE-2.0Unless?required?by?applicable?law?or?agreed?to?in?writing,software?distributed?under?the?License?is?distributed?on?an"AS?IS"?BASIS,?WITHOUT?WARRANTIES?OR?CONDITIONS?OF?ANYKIND,?either?express?or?implied.??See?the?License?for?thespecific?language?governing?permissions?and?limitationsunder?the?License. --><plugin?xmlns="http://apache.org/cordova/ns/plugins/1.0"id="alert-plugin"version="1.0.0"><name>AlertPlugin</name><description>顯示alert的插件</description><license>Apache?2.0</license><keywords>cordova,AlertPlugin</keywords><engines><engine?name="cordova-android"?version=">=3.6.0"?/><!--?Requires?CordovaPlugin.preferences?--></engines><js-module?src="www/alert.js"?><clobbers?target="navigator.alert"?/></js-module><!--?android?--><platform?name="android"><config-file?target="res/xml/config.xml"?parent="/*"><feature?name="alert"><param?name="android-package"?value="com.alert"/><param?name="onload"?value="true"/></feature></config-file><source-file?src="src/android/alert.java"?target-dir="src/com"?/></platform> </plugin>基礎的不說,說下js-module,關于<clobbers>官網(wǎng)上是這樣說的
<clobbers?target="some.value"/>?indicates that the?module.exports?is inserted into the?windowobject as?window.some.value. You can have as many?<clobbers>?as you like. Any object not available on?window?is created.
意思是,比如我www下的alert.js文件如下
var?exec?=?require('cordova/exec');var?alertPlugin?=?{show:function(success,error,message)?{exec(success,?error,?"alert",?"show",?message);} };module.exports?=?alertPlugin;如果我這樣配置的話
<js-module?src="www/alert.js"?><clobbers?target="navigator.alert"?/></js-module>意味著
navigator.alert就代表了alertPlugin
當我們調用alertPlugin.show方法的時候就等同于調用navigator.alert.show方法就行。
<source-file?src="src/android/alert.java"?target-dir="src/com"?/>這個指明了核心類的名稱,以及當android項目執(zhí)行安裝的時候,alert.java是會出現(xiàn)在src/com路徑下的
value="com.alert"對應了android項目下的com.alert.java文件
<feature name="alert">中的alert和
?cordova.exec(function(winParam)?{},?function(error)?{},?"service",?"action",?["firstArgument",?"secondArgument",?42,?false]);中的service名字一樣
對于alert.java,內容如下
注意show是和
cordova.exec(function(winParam)?{},?function(error)?{},?"service",?"action",?["firstArgument",?"secondArgument",?42,?false]);中的action名字是一樣的。
插件分享在這個地址:
http://www.oschina.net/code/snippet_778987_53062
轉載于:https://my.oschina.net/liangzhenghui/blog/549018
總結
以上是生活随笔為你收集整理的cordova自定义一个简单的alert的插件(android平台)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jq 多个div从右向左依次显示_jqu
- 下一篇: 如何减少tomcat内存使用量?