android开发 phonegap插件开发中ProgressDialog的使用 转

整体来说,android开发编写java代码比较变态。

在使用phonegap开发插件是则会让你看到更变态的用法。

 

直接贴代码吧,为走弯路的朋友借鉴。

代码参考phonegap源码Notification.java类

 

 

Java代码  收藏代码
  1. package com.p3p.android.plugin;  
  2.   
  3. import org.json.JSONArray;  
  4. import org.json.JSONException;  
  5.   
  6. import android.app.ProgressDialog;  
  7.   
  8. import com.phonegap.api.PhonegapActivity;  
  9. import com.phonegap.api.Plugin;  
  10. import com.phonegap.api.PluginResult;  
  11.   
  12. public class HelloWord extends Plugin {  
  13.  public ProgressDialog progressDialog = null;  
  14.   
  15.  @SuppressWarnings("static-access")  
  16.  @Override  
  17.  public PluginResult execute(String arg0, JSONArray arg1, String arg2) {  
  18.   // TODO Auto-generated method stub  
  19.   PluginResult.Status status = PluginResult.Status.OK;  
  20.   String result = "";  
  21.   try {  
  22.   
  23.    if (this.progressDialog != null) {  
  24.     this.progressDialog.dismiss();  
  25.     this.progressDialog = null;  
  26.    }  
  27.    final HelloWord __this = this;  
  28.    final PhonegapActivity ctx = this.ctx;  
  29.    Runnable runnable = new Runnable() {  
  30.     public void run() {  
  31.      __this.progressDialog = new ProgressDialog(ctx);  
  32.      __this.progressDialog  
  33.        .setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  34.      __this.progressDialog.setTitle("提示信息");  
  35.      __this.progressDialog.setMessage("文件正在下载中,请稍微...");  
  36.      __this.progressDialog.setCancelable(false);  
  37.      //__this.progressDialog.setCancelable(true);  
  38.      __this.progressDialog.setMax(100);  
  39.      __this.progressDialog.setProgress(0);  
  40.      /* 
  41.       * __this.progressDialog .setOnCancelListener(new 
  42.       * DialogInterface.OnCancelListener() { public void 
  43.       * onCancel(DialogInterface dialog) { __this.progressDialog 
  44.       * = null; } }); 
  45.       */  
  46.      __this.progressDialog.show();  
  47.     }  
  48.    };  
  49.   
  50.    this.ctx.runOnUiThread(runnable);  
  51.    Thread.currentThread().sleep(2000l);  
  52.    this.progressDialog.setProgress(80);  
  53.    Thread.currentThread().sleep(3000l);  
  54.    this.progressDialog.setProgress(95);  
  55.    Thread.currentThread().sleep(1000l);  
  56.    this.progressDialog.setProgress(100);  
  57.    Thread.currentThread().sleep(1000l);  
  58.    this.progressDialog.dismiss();  
  59.     result = "result from my plugin:" + arg1.getString(0);  
  60.   } catch (JSONException e) {  
  61.    // TODO Auto-generated catch block  
  62.    e.printStackTrace();  
  63.   } catch (InterruptedException e) {  
  64.    // TODO Auto-generated catch block  
  65.    e.printStackTrace();  
  66.   }  
  67.   return new PluginResult(status, result);  
  68.   
  69.  }  
  70.   
  71. }  

 

posted on 2012-06-16 21:29  民谣  阅读(566)  评论(0编辑  收藏  举报

导航