PhoneGap có API plugin phong nha. Bạn sẽ viết plugin trong Java bằng cách triển khai giao diện IPlugin. Hầu hết phép thuật là trong hàm execute().
public interface IPlugin {
/**
* Executes the request and returns PluginResult.
*
* @param action The action to execute.
* @param args JSONArry of arguments for the plugin.
* @param callbackId The callback id used when calling back into JavaScript.
* @return A PluginResult object with a status and message.
*/
PluginResult execute(String action, JSONArray args, String callbackId);
// ... more ...
}
Cách tốt nhất để bắt đầu viết plugin là viết javascript API trước. Bạn sẽ bắt đầu điển hình bằng cách viết một lớp javascript tùy chỉnh, và trong mỗi phương thức trên lớp javascript, sắp xếp các biến và gọi vào plugin bạn đã phát triển bằng cách sử dụng phương thức Phonegap.exec(). Đây là chữ ký phương thức để bạn tham khảo.
/* src/com/phonegap/api/PluginManager.java */
/**
* Receives a request for execution and fulfills it by finding the appropriate
* Java class and calling it's execute method.
*
* PluginManager.exec can be used either synchronously or async. In either case, a JSON encoded
* string is returned that will indicate if any errors have occurred when trying to find
* or execute the class denoted by the clazz argument.
*
* @param service String containing the service to run
* @param action String containt the action that the class is supposed to perform. This is
* passed to the plugin execute method and it is up to the plugin developer
* how to deal with it.
* @param callbackId String containing the id of the callback that is execute in JavaScript if
* this is an async plugin call.
* @param args An Array literal string containing any arguments needed in the
* plugin execute method.
* @param async Boolean indicating whether the calling JavaScript code is expecting an
* immediate return value. If true, either PhoneGap.callbackSuccess(...) or
* PhoneGap.callbackError(...) is called once the plugin code has executed.
*
* @return JSON encoded string with a response message and status.
*/
@SuppressWarnings("unchecked")
public String exec(final String service, final String action,
final String callbackId, final String jsonArgs,
final boolean async)
Bạn cũng cần phải đăng ký Plugin. Bạn làm điều này bằng cách thêm mã đăng ký ở cuối thư viện javascript tùy chỉnh của bạn.
Trong ví dụ bên dưới, tác giả đã xác định lớp BarcodeScanner javascript và đăng ký nó bằng phương thức addConstructor.
Hai bước được thực hiện trong addConstructor:
Tạo một thể hiện mới của BarcodeScanner trong javascript và đăng ký nó. Điều này có thể truy cập được bằng javascript dưới dạng window.plugins.barcodeScanner
Đăng ký lớp Plugin tùy chỉnh có tên dịch vụ. Tên dịch vụ này được chuyển thành đối số đầu tiên cho PhoneGap.exec để PhoneGap có thể khởi tạo lớp plugin java và gọi phương thức execute() trên đó.mã số đăng ký
mẫu:
PhoneGap.addConstructor(function() {
/* The following registers an instance of BarcodeScanner in window.plugins.barcodeScanner */
PhoneGap.addPlugin('barcodeScanner', new BarcodeScanner());
/* The following associates a service name BarcodeScanner with a class com.beetight.barcodescanner.BarcodeScanner */
/* The service name is the first argument passed into PhoneGap.exec */
PluginManager.addService("BarcodeScanner","com.beetight.barcodescanner.BarcodeScanner");
});
Nguồn
2011-05-22 08:33:36
hmm, ví dụ này là rất tốt, nhưng tôi nhận được một ngoại lệ nullpointer trên appView, nó có vẻ là null. Nơi nào bạn intialise đối tượng? – dan
Nó không hoạt động trừ khi các phương thức được công khai! lam tôt lăm! – dan
thêm super.init(); như @Karfield được đề cập bên dưới đã sửa ngoại lệ nullpointer cho tôi. –