android,onProgressUpdate没有运行

时间:2012-06-17 05:31:12

标签: android android-asynctask

我的下面的代码有问题,onProgressUpdate没有运行...但是onPreExecute,doInBackground和onPostExecute相应地执行。还是我需要在doInBackground中返回?请告诉我错过了什么。 我以前删除了

notification.contentView.setProgressBar(R.id.pbStatus, 100, progress, false);
notificationManager = (NotificationManager) getApplicationContext().getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
notificationManager.notify(42, notification);

在onCreate中,然后通知根本没有出现。以下是完整代码:

package com.android.MaxApps;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.RemoteViews;
import android.widget.Toast;

public class StaffChoice extends Activity { 
    ProgressBar progressBar;
    private int progress;

    Intent MyI;
    PendingIntent MyPI;
    NotificationManager MyNM;
    Notification notification;
NotificationManager notificationManager;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.staffchoices);       

    MyI = new Intent(getApplicationContext(), MaxAppsAct.class);
    MyPI = PendingIntent.getActivity(getApplicationContext(), 0, MyI, 0);
    MyNM = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent = new Intent(getApplicationContext(), MaxAppsAct.class);
    final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);

    notification = new Notification(R.drawable.logo, "Downloading...", System.currentTimeMillis());
    notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
    notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.staffchoices);
    notification.contentIntent = pendingIntent;
    notification.contentView.setImageViewResource(R.id.imgIcon, R.drawable.save);
    notification.contentView.setTextViewText(R.id.tvText, "Downloading...");
    notification.contentView.setProgressBar(R.id.pbStatus, 100, progress, false);
    notificationManager = (NotificationManager) getApplicationContext().getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
    notificationManager.notify(42, notification);

    String url = "http://www.domainURL.com/3d.png"; 
    new DownloadFileAsync().execute(url);
    }

public class DownloadFileAsync extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute(){
        super.onPreExecute();
        }

    @Override
    protected String doInBackground(String... aurl) {
        int count;
        try {
            URL url = new URL(aurl[0]);
            URLConnection conexion = url.openConnection();
            conexion.connect();

            int lenghtOfFile = conexion.getContentLength();
            Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

            File folder = new File(Environment.getExternalStorageDirectory() + "/MaxApps");
            boolean success = false;
            if (!folder.exists()) {
                success = folder.mkdirs();
            }
            if (!success) {
            } else {
            }

            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream("/sdcard/MaxApps/3d.png");

            byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data)) != -1) {
            total += count;
            publishProgress(""+(int)((total*100)/lenghtOfFile));
            output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();
            } catch (Exception e) {}

        return null;
    }

    protected void onProgressUpdate(Integer... progress) {          
        notification.contentView.setProgressBar(R.id.pbStatus, 100, progress[0], false);
        notificationManager.notify(42, notification);
        }

    @Override
    protected void onPostExecute(String unused) {           
        notificationManager.cancel(42);

        Notification MyN = new Notification(); MyN.icon = R.drawable.logo1;
        MyN.tickerText = "Download Complete"; 
        MyN.number = 1; 
        MyN.setLatestEventInfo (getApplicationContext(), "Application Title", "Application Description", MyPI); 

        MyNM.notify(1, MyN);
    }
}

}

2 个答案:

答案 0 :(得分:10)

您声明您的DownloadFileAsync课程将进度更新发布为字符串:

public class DownloadFileAsync extends AsyncTask<String, String, String>

然而,你定义onProgressUpdate()采取整数:

protected void onProgressUpdate(Integer... progress) {

因此,它不是onProgressUpdate()使用的AsyncTask。假设你想使用字符串,它应该成为

@Override
protected void onProgressUpdate(String... progress) {

我的猜测是你真的想把你的班级声明为:

public class DownloadFileAsync extends AsyncTask<String, Integer, Void>

在这种情况下,除了添加onProgressUpdate()之外,您可以保持@Override不变。 onPostExecute()将成为

@Override
protected void onPostExecute(Void unused) {

哪个比(String unused)更清晰,因为它没有被使用。

一般建议:

  1. @Override是你的朋友,因为它有助于发现此类错误。
  2. 请务必阅读AsyncTask的文档,因为您似乎对其用法感到有些困惑。该页面很好地解释了这些问题。

答案 1 :(得分:0)

我认为你传递一个字符串作为参数publishProgress(""+(int)((total*100)/lenghtOfFile));,而protected void onProgressUpdate(Integer... progress)期待一个整数。我想你在这种情况下出错了。