我是Android開發新手,需要實現類似於ChildBrowser plugin中的自定義對話框插件。訪問在Phonegap插件的UI線程上運行的對話框
儘管我已經設法實現了此對話框的大部分功能,包括某些對話框動作中的JS回調事件,但我無法實現從JS發回數據到對話框中的數據。
場景如下:當對話框的Spinner發生變化時,JS回調被調用,JS代碼執行一些處理(訪問sqlStorage數據等),之後我需要更新對話框的一些視圖。我當前的代碼(不包括非相關的東西):
CustomDialogPlugin.java:
public class CustomDialogPlugin extends Plugin {
@Override
public PluginResult execute(String action, JSONArray args, String callbackId) {
PluginResult.Status status = PluginResult.Status.OK;
String result = "";
try {
if (action.equals("show")) {
result = this.showDialog(args.optJSONObject(0), callbackId);
// ...
} else if (action.equals("update")) {
this.updateData(args.optJSONObject(0));
}
} catch(JSONException e) {
// ...
}
}
// Show the dialog
public String showDialog(JSONObject options, final String callbackId) {
if (options != null) {
// Handle options
}
// Create the child dialog in new thread
Runnable runnable = new Runnable() {
public void run() {
mDialog = new Dialog(ctx);
// Dialog layouts, views and listeners setup
// ...
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView adapter, View v, int i, long lng) {
// ...
// Prepare JSON data to send and call a callback
sendUpdate(CHANGED_EVENT, obj, true);
}
@Override
public void onNothingSelected(AdapterView adapter) {
// ...
}
});
// ...
mDialog.show();
}
};
this.ctx.runOnUiThread(runnable);
return "";
}
// Create a new plugin result and send it back to JavaScript
private void sendUpdate(int eventType, JSONObject obj, boolean keepCallback) {
if (this.savedCallbackId != null) {
// ...
PluginResult result = new PluginResult(PluginResult.Status.OK, obj);
Result.setKeepCallback(keepCallback);
this.success(result, this.savedCallbackId);
}
}
// Parse data received from JS and upate dialog
protected String updateData(JSONObject data) {
// Here I need to update some views of mDialog
// Can't access them from here
}
}
customdialog.js:
var CustomDialog = function() {};
CustomDialog.CHANGED_EVENT = 1;
CustomDialog.prototype.show = function(data) {
return PhoneGap.exec(this._onEvent, this._onError, 'CustomDialogPlugin', 'show', [data]);
};
CustomDialog.prototype.update = function(data) {
return PhoneGap.exec(this._onEvent, this._onError, 'CustomDialogPlugin', 'update', [data]);
};
CustomDialog.prototype._onEvent = function(data) {
if (data.type == CustomDialog.CHANGED_EVENT && typeof window.plugins.CustomDialog.onChange === "function") {
window.plugins.CustomDialog.onChange(data);
}
// ...
};
CustomDialog.prototype._onError = function(data) {
// ...
};
PhoneGap.addConstructor(function() {
PhoneGap.addPlugin('customDialog', new CustomDialog());
});
的test.html:
// ...
window.plugins.customDialog.onChange = function(data) {
// ...
window.plugins.customDialog.update(some_other_data);
}
我試圖在Runnable
裏面創建一個Handler
,並且調用它來處理來自updateData
的消息,但顯然我做錯了什麼。
也許我是過於複雜的東西,有一個更簡單的方法來完成從JS回調數據更新?
預先感謝您。
感謝您的回覆,「ChildBrowser」不允許在啓動後從外部更新對話框。當然,我試圖在主插件類中保存視圖引用,然後在updateData方法中使用它,但是我得到「只有創建視圖層次結構的原始線程才能觸及其視圖。」錯誤。 – Tiger 2012-01-27 10:39:28
此鏈接已損壞:( – 2014-03-12 05:43:01