如何在Android平臺上建立自定義的Cordova外掛並使用SAP UI5消費
Suppose we have packaged our UI5 application into mobile platform and need to consume native API provided by mobile platform, and it is time for Cordova plugin to come on the stage. A Cordova plugin is a package of injected code that allows the Cordova webview within which the app renders to communicate with the native platform on which it runs.
In this blog, I still use the UI5 application mentioned in my previous blog Step by step to package a Fiori application into your Android device using Cordova for demo.
I will show the steps how to create a dummy plugin for Android platform, which simply performs the calculation between two integers implemented in Java, and then could be consumed by JavaScript code in UI5. There is indeed a page in Cordova website talking about how to create a new plugin, however I fail to create a plugin just simply by following it.
In my opinion some information is missing in the document, so it is the reason why I decide to document my detailed step here for future reference.
Note
The steps might vary with different version of Cordova. This blog is made based on version 7.0.1:
Detail steps to create plugin
(1) Install plugman via npm:
Then create a new plugin via the following command:
Once done, in the root folder of project there is a new folder generated, whose name is equal to the option specified via command -name:
Some artifacts are automatically generated and stored within the plugin folder. We don’t need to touch them at the moment.
(2) Since I need to develop a plugin used for Android platform, so enable the created plugin with Android platform via command: “plugman platform add –platform_name android”
Once done, a .java source file is created, which is used to implement the logic for example performing some native API call.
Copy the following source code to this Adder.java:
package jerry.adder;import org.apache.cordova.CordovaPlugin;import org.apache.cordova.CallbackContext;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;public class Adder extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("performAdd")) {
int arg1 = args.getInt(0);
int arg2 = args.getInt(1);
/* Indicating success is failure is done by calling the appropriate method on the
callbackContext.*/
int result = arg1 + arg2;
callbackContext.success("result calculated in Java: " + result);
return true;
}
return false;
}}
(3) Perform the following command to automatically generate a descriptor file, package.json for created plugin. Just press enter key again and again to simply use the default value, which are enough for this exercise.
Once done, the package.json file is generated within plugin folder.
(4) Install this plugin via command “cordova plugin add Adder”. If everything works fine, you should see message “BUILD SUCCESSFUL”.
Now just perform cordova compile, and the plugin will be built into the final APK file.
A list of artifacts generated for the created plugin
Let’s review what artifacts / configuration finally we have regarding this created plugin:
(1) in config.xml in the root folder, our plugin is added:
(2) The implementation Adder.java file for plugin in plugin folder.
(3) In the plugin folder there is a plugin.xml file, which defines the path the plugin implementation will be located in the platform specific folder.
In my project the target-dir points to this folder as below:
Consume the plugin in UI5 application
In order to test this plugin, paste the following source code to index.js in path: /platforms/android/assets/www/js:
var app = {
initialize: function() {
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
},
// deviceready Event Handler // // Bind any cordova events here. Common events are: // 'pause', 'resume', etc. onDeviceReady: function() {
this.receivedEvent('deviceready');
},
receivedEvent: function(id) {
function success(result){
debugger;
alert("Jerry plugin result: " + result);
};
setTimeout( function(){
debugger;
Cordova.exec(success, null, "Adder", "performAdd", [10,20]);
}, 10000);
}};app.initialize();
The consumption is done in line 38, here I use a 10 seconds delay just in order to ease my debugging. ( Java Plugin debugging is worth another blog )
Execute the application in my mobile phone, the plugin will be executed in Java layer and result is passed back to my UI5 application:
If you would like to know how JavaScript code is passed to Java code, please read this blog: How is JavaScript code in OData offline plugin delegated to native Java code in Android.
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2717098/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Cordova學習--iOS自定義外掛iOS
- 如何在 SAP Business Application Studio 裡建立 SAP UI5 應用並部署到 BTP 平臺上APPUI
- 快速自定義Cordova外掛(-配置檔案)
- 如何建立Cordova外掛
- 使用SAP雲平臺的destination消費Internet上的OData service
- 如何在 SAP BTP 上建立以及消費 Business Rule Service
- Cordova開發app——使用外掛錄音並上傳伺服器APP伺服器
- Cordova應用的JavaScript程式碼和自定義外掛程式碼的除錯JavaScript除錯
- 如何在SAP CRM裡建立和消費Web serviceWeb
- 如何在SAP雲平臺上使用MongoDB服務MongoDB
- 【pytest】如何使用 pytest-rerunfailures 外掛並自定義重跑操作AI
- apisix-dashboard上新增自定義外掛API
- Apache Maven Assembly自定義打包外掛的使用ApacheMaven
- gradle自定義外掛Gradle
- mybatis 自定義外掛MyBatis
- 最簡單的SAP雲平臺開發教程 - 如何開發UI5應用並執行在SAP雲平臺上UI
- apisix~自定義外掛的部署API
- 如何在SAP Cloud for Customer自定義BO中建立訪問控制Cloud
- 【Maven實戰技巧】「外掛使用專題」Maven-Archetype外掛建立自定義maven專案骨架Maven
- 使用SAP iRPA Studio建立的本地專案,如何部署到SAP雲平臺上?
- 如何在 SAP BTP 上使用 Integration Suite 消費 S/4HANA Cloud APIUICloudAPI
- Kube-Scheduler外掛的自定義
- vue自定義全域性元件(或自定義外掛)Vue元件
- 使用nodejs消費SAP Cloud for Customer上的Web serviceNodeJSCloudWeb
- 在 SAP BAS 裡使用 SAP UI5 應用消費 OData 的 Create 和 Delete 操作UIdelete
- 自定義Gradle-Plugin 外掛GradlePlugin
- es 自定義分詞外掛分詞
- cordova列印外掛備註
- 如何在 SAP BTP 平臺 ABAP 程式設計環境裡消費基於 SOAP 的 Web Service程式設計Web
- SAP UI5 應用開發教程之三十二 - 如何建立一個自定義 SAP UI5 控制元件UI控制元件
- 如何使用 Cordova 將 SAP UI5 應用生成一個能在 Android 手機上安裝的混合應用試讀版UIAndroid
- 使用程式碼給 SAP UI5 XML 檢視新增自定義 CSSUIXMLCSS
- 使用OData服務將SAP C4C自定義BO的TextCollection暴露給外部消費者
- 【django學習-24】自定義外掛Django
- gradle自定義任務和外掛Gradle
- APISIX Ingress 如何支援自定義外掛API
- Flutter 外掛的建立及使用Flutter
- 如何使用 Chrome 除錯執行在手機上的 SAP UI5 Cordova 混合應用試讀版Chrome除錯UI