android,onProgressUpdate没有运行

mel*_*tcs 0 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);
Run Code Online (Sandbox Code Playgroud)

在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);
    }
}
Run Code Online (Sandbox Code Playgroud)

}

Dar*_*tle 10

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

public class DownloadFileAsync extends AsyncTask<String, String, String>
Run Code Online (Sandbox Code Playgroud)

然而你定义onProgressUpdate()为采用整数:

protected void onProgressUpdate(Integer... progress) {
Run Code Online (Sandbox Code Playgroud)

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

@Override
protected void onProgressUpdate(String... progress) {
Run Code Online (Sandbox Code Playgroud)

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

public class DownloadFileAsync extends AsyncTask<String, Integer, Void>
Run Code Online (Sandbox Code Playgroud)

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

@Override
protected void onPostExecute(Void unused) {
Run Code Online (Sandbox Code Playgroud)

哪个更清楚(String unused),因为它没有被使用.

一般建议:

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