0

我对下面的代码有疑问,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);
    }
}

}

4

2 回答 2

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的文档,因为您似乎对它的用法有点困惑。该页面很好地解释了这些问题。
于 2012-06-17T06:10:55.040 回答
0

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

于 2012-06-17T06:00:14.027 回答