我遇到了progressdialog的问题,我希望在连接的持续时间内更新我的progressdialog。
这是我的progressdialog
Pro = new ProgressDialog(getParent());
Pro.setMessage("Enviando");
Pro.setIndeterminate(false);
Pro.setMax(100);
Pro.setCancelable(false);
Pro.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
这是我的asynctask
private class NuevoPostVideo extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... params) {
objAcceso = new AccesoBd();
String jsRes= "";
SharedPreferences userDetails = getParent().getSharedPreferences("MisPreferencias", MODE_PRIVATE);
try{
File f = new File(sDireccionFoto);
FileBody bin = new FileBody(f);
StringBody from = new StringBody( Integer.toString(userDetails.getInt("Id",0)));
StringBody id_recurso = new StringBody(Integer.toString(idEvento));
StringBody titulo_body = new StringBody(txtVTitulo.getText().toString());
StringBody ficha = new StringBody("null");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("imageFile", bin);
reqEntity.addPart("from",from);
reqEntity.addPart("idrecurso",id_recurso);
reqEntity.addPart("titulo", titulo_body);
reqEntity.addPart("ficha", ficha);
objAcceso.conxDatosInPost(params[0],reqEntity);
jsRes="ok";
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{return jsRes;}
}
@Override
protected void onPostExecute( String result) {
if (result.equals("ok")){
SharedPreferences userDetails = getParent().getSharedPreferences("MisPreferencias", MODE_PRIVATE);
DetalleEvento objd = new DetalleEvento();
objd.setTipo(1);
objd.setFecha(objAcceso.FechaActualinglesa());
objd.setTituloPost(sTituloPost);
objd.setImagen(btmImagenElegida);
objd.setUrl(sDireccionFoto);
objd.setAutor(userDetails.getString("Nombre","Usted"));
objAdapterDetalles.arrayDatos.add(0,objd);
objAdapterDetalles.notifyDataSetChanged();
lstDatos.setSelection(1);
dOpciones.dismiss();
dHacerVideo.dismiss();
}
Pro.dismiss();
prgVProgreso.setVisibility(View.INVISIBLE);
}
@Override
protected void onPreExecute() {
//prgVProgreso.setVisibility(View.VISIBLE);
Pro.show();
//Pro.incrementProgressBy(10);
}
@Override
public void onProgressUpdate(Integer... args){
Pro.setProgress(args[0]);
}
}
这是我的conexion函数“看看我的doinbhackgroud的这一行”( objAcceso.conxDatosInPost(params [0],reqEntity); )
public String conxDatosInPost(String Direccion, MultipartEntity Parametros) throws Exception {
BufferedReader in = null;
try {
HttpClient client = ClienteHttp();
HttpPost request = new HttpPost(Direccion);
request.setEntity(Parametros);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
Log.d("El resultado de cnxposin es :----------- ", result +"fn");
return result;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
¿我可以这样做来更新我的进度对话框,但连接会持续吗?
更新
我有问题要计算要插入的文件的大小(在这种情况下是视频),并且每次都要查看上传到服务器的进度
找不到这样做的方法
我想知道是否有一些侦听器progresslistener风格的可能性?
请帮助我或给我一个提示
答案 0 :(得分:2)
从official java doc查看以下示例:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
答案 1 :(得分:0)
您可以根据ProgressDialog
doInBackground(..)
中发生的事件的状态,使用处理程序更新AsyncTask
。我认为example对你来说是完美的。只需使用Handler
发送Message
handler.sendmsg(msg)
,以便更新ProgressDialog
。
答案 2 :(得分:0)
最终解决我的问题如下
这就是我的代码
public class NuevoPostVideoConProgreso extends AsyncTask<String, Integer, String>{
ProgressDialog dialogProgreso;
long totalSize;
@Override
protected String doInBackground(String... params) {
objAcceso = new AccesoBd();
String res = "";
SharedPreferences userDetails = getParent().getSharedPreferences("MisPreferencias", MODE_PRIVATE);
try{
CMultipartContador objconSub = new CMultipartContador(new CMultipartContador.ProgressListener() {
@Override
public void transferred(long num) {
// TODO Auto-generated method stub
float antes = (float)(num /(float) totalSize);
float despues =(float)(antes * 100);
int numero = (int) despues;
publishProgress(numero);//(int) ((num /(float) totalSize) * 100));
}
} );
File f = new File(sDireccionFoto);
FileBody bin = new FileBody(f);
StringBody from = new StringBody( Integer.toString(userDetails.getInt("Id",0)));
StringBody id_recurso = new StringBody(Integer.toString(idEvento));
StringBody titulo_body = new StringBody(txtVTitulo.getText().toString());
StringBody ficha = new StringBody("null");
// MultipartEntity reqEntity = new MultipartEntity();
objconSub.addPart("imageFile", bin);
objconSub.addPart("from",from);
objconSub.addPart("idrecurso",id_recurso);
objconSub.addPart("titulo", titulo_body);
objconSub.addPart("ficha", ficha);
totalSize= objconSub.getContentLength();
// dialogProgreso.setMax((int)totalSize);
objAcceso.conxDatosInPostAsynctask(params[0],objconSub);
dialogProgreso.setProgress(100);
res= "ok";
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{return res;}
}
@Override
protected void onPostExecute( String result) {
dialogProgreso.setProgress(100);
dialogProgreso.dismiss();
dOpciones.dismiss();
dHacerVideo.dismiss();
LlenarEvento();
}
@Override
protected void onPreExecute() {
dialogProgreso = new ProgressDialog(getParent());
dialogProgreso.setMessage("Subiendo...");
dialogProgreso.setIndeterminate(false);
//dialogProgreso.setMax(100);
dialogProgreso.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//dialogProgreso.setProgress(0);
dialogProgreso.setCancelable(false);
dialogProgreso.show();
//dialogProgreso.setProgress(3);
}
@Override
public void onProgressUpdate(Integer... args){
dialogProgreso.setProgress(args[0]);
}
}
private ImageView aniadirImagen(Bitmap btmi){
ImageView img= new ImageView(getParent());
img.setImageBitmap(btmi);
btmi.recycle();
return img;
}
这是我的新班级
public class CMultipartContador extends MultipartEntity {
private final ProgressListener listener;
public CMultipartContador(final ProgressListener listener)
{
super();
this.listener = listener;
}
public CMultipartContador(final HttpMultipartMode mode, final ProgressListener listener)
{
super(mode);
this.listener = listener;
}
public CMultipartContador(HttpMultipartMode mode, final String boundary, final Charset charset, final ProgressListener listener)
{
super(mode, boundary, charset);
this.listener = listener;
}
@Override
public void writeTo(final OutputStream outstream) throws IOException
{
super.writeTo(new CountingOutputStream(outstream, this.listener));
}
public static interface ProgressListener
{
void transferred(long num);
}
public static class CountingOutputStream extends FilterOutputStream
{
private final ProgressListener listener;
private long transferred;
public CountingOutputStream(final OutputStream out, final ProgressListener listener)
{
super(out);
this.listener = listener;
this.transferred = 0;
}
public void write(byte[] b, int off, int len) throws IOException
{
out.write(b, off, len);
this.transferred += len;
this.listener.transferred(this.transferred);
}
public void write(int b) throws IOException
{
out.write(b);
this.transferred++;
this.listener.transferred(this.transferred);
}
}