예제 #1
0
 /**
  * 从服务器下载文件,文件路径在sd卡根目录下
  *
  * @param path,文件路径,即所在url
  * @param filename,下载文件保存的文件名
  * @param pd,ProgressDialog,用于显示下载进程
  * @return File,下载好的文件
  * @throws Exception
  */
 @SuppressLint("NewApi")
 public static File getFileFromServer(String path, String filename, ProgressDialog pd)
     throws Exception {
   // 如果相等的话表示当前的sdcard挂载在手机上并且是可用的
   if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
     URL url = new URL(path);
     HttpURLConnection conn = (HttpURLConnection) url.openConnection();
     conn.setConnectTimeout(5000);
     // 获取到文件的大小
     pd.setProgressNumberFormat("%1dkb/%2dkb");
     pd.setMax(conn.getContentLength() / 1024);
     InputStream is = conn.getInputStream();
     File file = new File(Environment.getExternalStorageDirectory().getPath(), filename);
     if (file.exists()) file.delete();
     file.createNewFile();
     FileOutputStream fos = new FileOutputStream(file);
     BufferedInputStream bis = new BufferedInputStream(is);
     byte[] buffer = new byte[1024];
     int len;
     int total = 0;
     while ((len = bis.read(buffer)) != -1) {
       fos.write(buffer, 0, len);
       total += len;
       // 获取当前下载量
       pd.setProgress(total / 1024);
     }
     fos.close();
     bis.close();
     is.close();
     return file;
   } else {
     return null;
   }
 }
예제 #2
0
  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    CharSequence title = getArguments().getCharSequence("title");
    CharSequence message = getArguments().getCharSequence("message");
    String progressNumberFormat = getArguments().getString("number-format");
    NumberFormat progressPercentFormat =
        (NumberFormat) getArguments().getSerializable("percent-format");
    boolean spinner = getArguments().getBoolean("spinner", true);

    ProgressDialog dialog = new ProgressDialog(getActivity());
    dialog.setOnKeyListener(
        new DialogInterface.OnKeyListener() {
          @Override
          public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            return keyCode == KeyEvent.KEYCODE_SEARCH;
          }
        });
    dialog.setProgressStyle(
        spinner ? ProgressDialog.STYLE_SPINNER : ProgressDialog.STYLE_HORIZONTAL);
    if (progressNumberFormat != null) {
      dialog.setProgressNumberFormat(progressNumberFormat);
    }
    if (progressPercentFormat != null) {
      dialog.setProgressPercentFormat(progressPercentFormat);
    }
    if (message != null) {
      dialog.setMessage(message);
    }
    if (title != null) {
      dialog.setTitle(title);
    }
    return dialog;
  }
 protected void onPreExecute() {
   _dialog.setTitle("Loading Schools...");
   _dialog.setMessage("This will only be done the first time");
   _dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
   _dialog.setProgressNumberFormat(null);
   _dialog.setCancelable(false);
   if (!(_dialog.isShowing())) _dialog.show();
 }
예제 #4
0
  public void createProgressDialog(String mediaName) {
    progressDialog = new ProgressDialog(WebViewActivity.this);

    progressDialog.setTitle(getString(R.string.notification_download_title_pending) + mediaName);
    progressDialog.setMessage(getString(R.string.notification_download_pending));
    progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL);
    progressDialog.setProgress(0);
    progressDialog.setMax(100);
    progressDialog.setProgressNumberFormat(null);
    progressDialog.show();
  }
예제 #5
0
 @Override
 protected void onPreExecute() {
   // setup the download dialog and display it
   downloadDialog = new ProgressDialog(getActivity());
   downloadDialog.setMessage(getString(R.string.downloading));
   downloadDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
   downloadDialog.setProgress(0);
   downloadDialog.setMax(100);
   downloadDialog.setCancelable(false);
   downloadDialog.setProgressNumberFormat(null);
   downloadDialog.show();
 }
  @Override
  protected Boolean doInBackground(String... params) {
    DatabaseHelper db = new DatabaseHelper(_context);
    try {
      JSONArray json = new JSONArray(params[0]);

      _dialog.setProgressNumberFormat("%1d / %2d");
      _dialog.setProgress(0);
      _dialog.setMax(json.length());

      for (int i = 0; i < json.length(); i++) {
        JSONObject jsonObject = json.getJSONObject(i);

        Directory directory =
            new Directory(
                jsonObject.getInt("Id"),
                jsonObject.getInt("SchoolId"),
                jsonObject.getString("Name"),
                jsonObject.getString("Telephone"),
                jsonObject.getString("Fax"),
                jsonObject.getString("Email"),
                jsonObject.getString("Principal"),
                jsonObject.getString("SchoolWebsite"),
                jsonObject.getString("Street"),
                jsonObject.getString("Suburb"),
                jsonObject.getString("City"),
                jsonObject.getString("RegionalCouncil"),
                jsonObject.getString("PostalAddress1"),
                jsonObject.getString("PostalAddress2"),
                jsonObject.getString("PostalAddress3"),
                jsonObject.getString("PostalCode"),
                jsonObject.getString("UrbanArea"),
                jsonObject.getString("SchoolType"),
                jsonObject.getString("Definition"),
                jsonObject.getString("Authority"),
                jsonObject.getString("GenderOfStudents"),
                jsonObject.getString("TerritorialAuthorityWithAucklandLocalBoard"),
                jsonObject.getString("MinistryOfEducationLocalOffice"),
                jsonObject.getString("EducationRegion"),
                jsonObject.getString("GeneralElectorate"),
                jsonObject.getString("MāoriElectorate"),
                jsonObject.getString("CensusAreaUnit"),
                jsonObject.getString("Ward"),
                jsonObject.getDouble("Latitude"),
                jsonObject.getDouble("Longitude"),
                jsonObject.getInt("Decile"),
                jsonObject.getInt("TotalSchoolRoll"),
                jsonObject.getInt("EuropeanPākehā"),
                jsonObject.getInt("Māori"),
                jsonObject.getInt("Pasifika"),
                jsonObject.getInt("Asian"),
                jsonObject.getInt("Melaa"),
                jsonObject.getInt("Other"),
                jsonObject.getInt("ChangeId"),
                jsonObject.getBoolean("Status"));
        db.addDirectory(directory);
        publishProgress(i);
      }

    } catch (JSONException e) {
      e.printStackTrace();
      return false;
    }

    return true;
  }