3

我已经查看了有关此问题的所有类似主题,但找不到解决问题的方法

我的 AsynkTask 应该从 dropbox 下载文件并在通知进度条中显示进度。doInBackground 下载文件但方法 publishProgress / onProgressUpdate 无法正常工作。onProgressUpdate 没有被调用。

异步任务

public class SongDownload extends AsyncTask<String, String, String> {

    DropboxAPI<AndroidAuthSession> dDBApi;
    Context context;
    Dropbox db;
    SharedPreferences prefs;
    NotificationManager mNotifyManager;
    Notification notification;
    String datei;

    public SongDownload(Context context, Dropbox db) {
        this.db = db;
        this.context = context;
        Log.v("mp3dropboxsync", "SongDownload");

        mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.ic_launcher, "Neuer DropBox Song!", System.currentTimeMillis());

        RemoteViews views = new RemoteViews(context.getApplicationContext()
                .getPackageName(), R.layout.notiprogressbar);
        views.setProgressBar(R.id.progressBar1, 100, 0, false);
        views.setTextViewText(R.id.status_text, "Song Downloading...");

        notification.contentView = views;

        notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT;

        mNotifyManager.notify(10, notification);

    }

@Override
protected String doInBackground(String... dropboxFile) {

    Log.v("mp3dropboxsync", "DoinBackground");
    File file = new File(Environment.getExternalStorageDirectory().getPath()+"/MP3/sonstiges/"+dropboxFile[0]);
    datei = dropboxFile[0];
    BufferedOutputStream out=null;
    try {

        if(!file.exists())
        {

        out = new BufferedOutputStream(new FileOutputStream(file));
        db.mDBApi.getFile("/MP3/"+dropboxFile[0], null, out, new ProgressListener() {

            @Override
            public void onProgress(long bytes, long total) {

                try {
                    Log.v("mp3dropboxsync", "onProgress");
                    String s = ""+(bytes/(float)total)*100;
                    publishProgress(s);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    Log.v("mp3dropboxsync", "onProgressERROR");
                }

            }
        });



        }
    } catch (DropboxException e) {
        Log.v("mp3dropboxsync", "Something went wrong while downloading.");
        file.delete();
    } catch (FileNotFoundException e) {
        Log.v("mp3dropboxsync", "File not found.");
    } catch (Exception e) {
        Log.v("mp3dropboxsync", "Unbekannter Fehler: "+e.getMessage());
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        else
            dropboxFile[0] = "";
    }
    return dropboxFile[0];
}

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

    try {
        int percent = Integer.parseInt(progress[0]);
        Log.v("mp3dropboxsync","Hi progressing - " + percent + "%");

        notification.contentView.setProgressBar(R.id.progressBar1, 100, percent, false);
        notification.contentView.setTextViewText(R.id.status_text, "Downloading... "+datei);

        mNotifyManager.notify(10, notification);

    } catch (NumberFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

@Override
protected void onPostExecute(String result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);

    Log.v("mp3dropboxsync","OnPostExecute");

    mNotifyManager.cancel(10);
}


}

日志猫

08-07 15:14:54.716: I/System.out(29125): waiting for debugger to settle...
08-07 15:14:54.926: I/System.out(29125): debugger has settled (1366)
08-07 15:14:55.496: D/libEGL(29125): loaded /system/lib/egl/libGLES_android.so
08-07 15:14:55.527: D/libEGL(29125): loaded /system/lib/egl/libEGL_adreno200.so
08-07 15:14:55.557: D/libEGL(29125): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
08-07 15:14:55.567: D/libEGL(29125): loaded /system/lib/egl/libGLESv2_adreno200.so
08-07 15:14:55.577: V/mp3dropboxsync(29125): OnStartCommand
08-07 15:14:55.577: V/mp3dropboxsync(29125): In Auth
08-07 15:14:55.577: V/mp3dropboxsync(29125): Nach Auth - linked: true
08-07 15:14:55.577: V/mp3dropboxsync(29125): Dropbox GetListe
08-07 15:14:55.677: I/Adreno200-EGLSUB(29125): <ConfigWindowMatch:2078>: Format RGBA_8888.
08-07 15:14:55.717: D/memalloc(29125): ashmem: Mapped buffer base:0x5266b000 size:1536000 fd:63
08-07 15:14:55.727: D/OpenGLRenderer(29125): Enabling debug mode 0
08-07 15:14:55.967: I/global(29125): In close() at SocketHttpClientConnection
08-07 15:14:55.987: D/memalloc(29125): ashmem: Mapped buffer base:0x52e0c000 size:1536000 fd:67
08-07 15:15:03.464: V/mp3dropboxsync(29125): SongDownload
08-07 15:15:03.484: V/mp3dropboxsync(29125): DoinBackground
08-07 15:15:03.624: I/global(29125): In close() at SocketHttpClientConnection
08-07 15:15:06.517: V/mp3dropboxsync(29125): onProgress
08-07 15:15:07.108: V/mp3dropboxsync(29125): onProgress
08-07 15:15:07.628: V/mp3dropboxsync(29125): onProgress
08-07 15:15:08.149: V/mp3dropboxsync(29125): onProgress
08-07 15:15:08.659: V/mp3dropboxsync(29125): onProgress
08-07 15:15:09.200: V/mp3dropboxsync(29125): onProgress
08-07 15:15:09.740: V/mp3dropboxsync(29125): onProgress
08-07 15:15:10.261: V/mp3dropboxsync(29125): onProgress
08-07 15:15:10.801: V/mp3dropboxsync(29125): onProgress
08-07 15:15:11.402: V/mp3dropboxsync(29125): onProgress
08-07 15:15:11.943: V/mp3dropboxsync(29125): onProgress
08-07 15:15:12.463: V/mp3dropboxsync(29125): onProgress
08-07 15:15:12.974: V/mp3dropboxsync(29125): onProgress
08-07 15:15:13.494: V/mp3dropboxsync(29125): onProgress
08-07 15:15:14.005: V/mp3dropboxsync(29125): onProgress
08-07 15:15:14.585: V/mp3dropboxsync(29125): onProgress
08-07 15:15:15.146: V/mp3dropboxsync(29125): onProgress
08-07 15:15:15.696: V/mp3dropboxsync(29125): onProgress
08-07 15:15:16.247: V/mp3dropboxsync(29125): onProgress
08-07 15:15:16.847: V/mp3dropboxsync(29125): onProgress
08-07 15:15:17.398: V/mp3dropboxsync(29125): onProgress
08-07 15:15:17.928: V/mp3dropboxsync(29125): onProgress
08-07 15:15:18.439: V/mp3dropboxsync(29125): onProgress
08-07 15:15:19.009: V/mp3dropboxsync(29125): onProgress
08-07 15:15:19.550: V/mp3dropboxsync(29125): onProgress
08-07 15:15:20.090: V/mp3dropboxsync(29125): onProgress
08-07 15:15:20.621: V/mp3dropboxsync(29125): onProgress
08-07 15:15:21.242: V/mp3dropboxsync(29125): onProgress
08-07 15:15:21.742: V/mp3dropboxsync(29125): onProgress
08-07 15:15:22.283: V/mp3dropboxsync(29125): onProgress
08-07 15:15:22.793: V/mp3dropboxsync(29125): onProgress
08-07 15:15:23.304: V/mp3dropboxsync(29125): onProgress
08-07 15:15:23.954: V/mp3dropboxsync(29125): onProgress
08-07 15:15:24.545: V/mp3dropboxsync(29125): onProgress
08-07 15:15:25.075: V/mp3dropboxsync(29125): onProgress
08-07 15:15:25.606: V/mp3dropboxsync(29125): onProgress
08-07 15:15:25.936: I/global(29125): In close() at SocketHttpClientConnection
08-07 15:15:25.936: I/global(29125): call socket close in SocketHttpClientConnection, socket=SSL socket over Socket[address=/199.47.219.158,port=443,localPort=36480]
08-07 15:15:25.976: I/global(29125): I/O error closing connection
08-07 15:15:25.976: I/global(29125): java.net.SocketException: Socket is closed
08-07 15:15:25.976: I/global(29125):    at java.net.Socket.checkOpenAndCreate(Socket.java:671)
08-07 15:15:25.976: I/global(29125):    at java.net.Socket.getSoLinger(Socket.java:434)
08-07 15:15:25.976: I/global(29125):    at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImplWrapper.getSoLinger(OpenSSLSocketImplWrapper.java:156)
08-07 15:15:25.976: I/global(29125):    at org.apache.http.impl.conn.tsccm.AbstractConnPool.closeConnection(AbstractConnPool.java:327)
08-07 15:15:25.976: I/global(29125):    at org.apache.http.impl.conn.tsccm.ConnPoolByRoute.deleteEntry(ConnPoolByRoute.java:530)
08-07 15:15:25.976: I/global(29125):    at org.apache.http.impl.conn.tsccm.ConnPoolByRoute.deleteClosedConnections(ConnPoolByRoute.java:653)
08-07 15:15:25.976: I/global(29125):    at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager.closeExpiredConnections(ThreadSafeClientConnManager.java:301)
08-07 15:15:25.976: I/global(29125):    at com.dropbox.client2.session.AbstractSession$IdleConnectionCloserThread.run(AbstractSession.java:530)
08-07 15:15:25.996: W/IdleConnectionHandler(29125): Removing a connection that never existed!
08-07 15:15:26.116: V/mp3dropboxsync(29125): onProgress
08-07 15:15:26.687: V/mp3dropboxsync(29125): onProgress
08-07 15:15:27.197: V/mp3dropboxsync(29125): onProgress
08-07 15:15:27.758: V/mp3dropboxsync(29125): onProgress
08-07 15:15:28.258: V/mp3dropboxsync(29125): onProgress
08-07 15:15:29.019: V/mp3dropboxsync(29125): onProgress
08-07 15:15:29.430: V/mp3dropboxsync(29125): SongDownload
08-07 15:15:29.700: V/mp3dropboxsync(29125): DoinBackground
08-07 15:15:32.272: V/mp3dropboxsync(29125): onProgress
08-07 15:15:32.773: V/mp3dropboxsync(29125): onProgress
08-07 15:15:33.283: V/mp3dropboxsync(29125): onProgress
08-07 15:15:33.784: V/mp3dropboxsync(29125): onProgress
08-07 15:15:34.304: V/mp3dropboxsync(29125): onProgress
08-07 15:15:34.835: V/mp3dropboxsync(29125): onProgress
08-07 15:15:35.345: V/mp3dropboxsync(29125): onProgress
08-07 15:15:35.866: V/mp3dropboxsync(29125): onProgress
08-07 15:15:36.466: V/mp3dropboxsync(29125): onProgress
08-07 15:15:36.967: V/mp3dropboxsync(29125): onProgress
08-07 15:15:37.487: V/mp3dropboxsync(29125): onProgress
08-07 15:15:38.068: V/mp3dropboxsync(29125): onProgress
08-07 15:15:38.569: V/mp3dropboxsync(29125): onProgress
08-07 15:15:39.079: V/mp3dropboxsync(29125): onProgress
08-07 15:15:39.580: V/mp3dropboxsync(29125): onProgress
08-07 15:15:40.150: V/mp3dropboxsync(29125): onProgress
08-07 15:15:40.751: V/mp3dropboxsync(29125): onProgress
08-07 15:15:41.311: V/mp3dropboxsync(29125): onProgress
08-07 15:15:41.822: V/mp3dropboxsync(29125): onProgress
08-07 15:15:42.402: V/mp3dropboxsync(29125): onProgress
08-07 15:15:42.903: V/mp3dropboxsync(29125): onProgress
08-07 15:15:43.453: V/mp3dropboxsync(29125): onProgress
08-07 15:15:43.954: V/mp3dropboxsync(29125): onProgress
08-07 15:15:44.464: V/mp3dropboxsync(29125): onProgress
08-07 15:15:44.985: V/mp3dropboxsync(29125): onProgress
08-07 15:15:45.485: V/mp3dropboxsync(29125): onProgress
08-07 15:15:45.996: V/mp3dropboxsync(29125): onProgress
08-07 15:15:46.496: V/mp3dropboxsync(29125): onProgress
08-07 15:15:47.007: V/mp3dropboxsync(29125): onProgress
08-07 15:15:47.567: V/mp3dropboxsync(29125): onProgress
08-07 15:15:48.258: V/mp3dropboxsync(29125): onProgress
08-07 15:15:48.839: V/mp3dropboxsync(29125): onProgress
08-07 15:15:49.439: V/mp3dropboxsync(29125): onProgress
08-07 15:15:49.990: V/mp3dropboxsync(29125): onProgress
08-07 15:15:50.210: V/mp3dropboxsync(29125): SongDownload
08-07 15:15:50.240: V/mp3dropboxsync(29125): DoinBackground //file already exist

我已经尝试了一切。还有一些其他效果,例如在 DoinBackground 完成后多次调用 onProgressUpdate 。但目前它没有被调用。我也无法解释套接字关闭异常。=/

希望您能够帮助我。我已经尝试重新制作整个项目并重新安装。但它没有用。

谢谢!

4

1 回答 1

0

请尝试使用我的 POC,它对您有帮助。

公共类 MainActivity 扩展 Activity { Handler handler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new BackGroundTask().execute("");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

class BackGroundTask extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub

        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                for (int i = 0; i < 10; i++) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    publishProgress("" + i);
                }
            }
        });
        t.start();

        return null;
    }

    @Override
    protected void onProgressUpdate(String... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText("T:" + values[0]);
    }

    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

    }
}

}

于 2013-08-07T13:44:41.733 回答