整体来说,android开发编写java代码比较变态。
在使用phonegap开发插件是则会让你看到更变态的用法。
直接贴代码吧,为走弯路的朋友借鉴。
代码参考phonegap源码Notification.java类
- package com.p3p.android.plugin;
- import org.json.JSONArray;
- import org.json.JSONException;
- import android.app.ProgressDialog;
- import com.phonegap.api.PhonegapActivity;
- import com.phonegap.api.Plugin;
- import com.phonegap.api.PluginResult;
- public class HelloWord extends Plugin {
- public ProgressDialog progressDialog = null;
- @SuppressWarnings("static-access")
- @Override
- public PluginResult execute(String arg0, JSONArray arg1, String arg2) {
- // TODO Auto-generated method stub
- PluginResult.Status status = PluginResult.Status.OK;
- String result = "";
- try {
- if (this.progressDialog != null) {
- this.progressDialog.dismiss();
- this.progressDialog = null;
- }
- final HelloWord __this = this;
- final PhonegapActivity ctx = this.ctx;
- Runnable runnable = new Runnable() {
- public void run() {
- __this.progressDialog = new ProgressDialog(ctx);
- __this.progressDialog
- .setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
- __this.progressDialog.setTitle("提示信息");
- __this.progressDialog.setMessage("文件正在下载中,请稍微...");
- __this.progressDialog.setCancelable(false);
- //__this.progressDialog.setCancelable(true);
- __this.progressDialog.setMax(100);
- __this.progressDialog.setProgress(0);
- /*
- * __this.progressDialog .setOnCancelListener(new
- * DialogInterface.OnCancelListener() { public void
- * onCancel(DialogInterface dialog) { __this.progressDialog
- * = null; } });
- */
- __this.progressDialog.show();
- }
- };
- this.ctx.runOnUiThread(runnable);
- Thread.currentThread().sleep(2000l);
- this.progressDialog.setProgress(80);
- Thread.currentThread().sleep(3000l);
- this.progressDialog.setProgress(95);
- Thread.currentThread().sleep(1000l);
- this.progressDialog.setProgress(100);
- Thread.currentThread().sleep(1000l);
- this.progressDialog.dismiss();
- result = "result from my plugin:" + arg1.getString(0);
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return new PluginResult(status, result);
- }
- }