0

我的问题是我无法从我的AsyncTask.

我在这个问题上花了很长时间,但没有找到解决方案。我的目标是只能更改对话框进度条和文本。

我怎样才能使这个通知进度条工作?

这是我的代码:

private class excuteUploadFile extends
        AsyncTask<HttpResponse, Integer, HttpResponse> {

    long totalSize;
    int NOTIFICATION_ID;
    String filepath, fileName;
    String serverResponse = null;
    String urlServer = "http://myurl";
    NotificationManager mNotifyManager;
    Builder mBuilder;
    ProgressDialog pd;
    Boolean nofti_appended = false;
    JSONObject jsonfile = null;

    public excuteUploadFile(String filepath, String fileName) {
        this.filepath = filepath;
        this.fileName = fileName;

        Log.i("uploader", "Selected file name : " + fileName);
        Log.i("uploader", "Selected file path : " + filepath);

    }

    @Override
    protected void onPreExecute() {
        pd = new ProgressDialog(BabupMain.this);
        // pd.setMessage(getResources().getString(R.string.uploading_file));
        pd.setTitle(fileName);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setCancelable(false);
        pd.setIcon(android.R.drawable.ic_menu_upload);
        pd.setButton(DialogInterface.BUTTON_NEGATIVE,
                getResources().getString(R.string.cancel),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        cancel(true);
                    }
                });
        // hide button
        pd.setButton(DialogInterface.BUTTON_NEUTRAL,
                getResources().getString(R.string.hide),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();
                        Random r = new Random();
                        NOTIFICATION_ID = r.nextInt(80 - 65) + 1;
                        NOTIFICATION_ID++;
                        mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                        mBuilder = new NotificationCompat.Builder(
                                BabupMain.this);
                        mBuilder.setContentTitle(fileName)
                                .setContentText(
                                        getResources().getString(
                                                R.string.uploading_file))
                                .setOngoing(true)
                                .setSmallIcon(android.R.drawable.ic_menu_upload);
                        mBuilder.setProgress(100, 100, true);
                        mNotifyManager.notify(NOTIFICATION_ID, mBuilder.build());
                        nofti_appended = true;
                    }
                });

        pd.show();
    }

    @Override
    protected HttpResponse doInBackground(HttpResponse... arg0) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext httpContext = new BasicHttpContext();
        HttpPost httpPost = new HttpPost(urlServer);

        try {
            Log.i("uploader", "Started " + fileName);

            if (isCancelled()) {
                og.i("uploader", "uploading has been canceled");
            }

            CustomMultiPartEntity multipartContent = new CustomMultiPartEntity(
                    new ProgressListener() {
                        @Override
                        public void transferred(long num) {
                            publishProgress((int) ((num / (float) totalSize) * 100));
                            Log.i("uploader", "uploading " + num + " OF "
                                    + totalSize);
                        }
                    });

            multipartContent.addPart("file", new FileBody(new File(filepath)));
            totalSize = multipartContent.getContentLength();
            Log.i("uploader", "Bytes to Send " + totalSize);
            // Send it
            httpPost.setEntity(multipartContent);
            HttpResponse response = httpClient.execute(httpPost, httpContext);
            serverResponse = EntityUtils.toString(response.getEntity());

            Log.i("uploader", "Server Response : " + serverResponse);

            return null;

        } catch (Exception e) {

            Log.i("uploader", "Uploading Error : " + e.getMessage());
        }
        return null;
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();

        if (nofti_appended) {
            mNotifyManager.cancel(NOTIFICATION_ID);
        } else {
            pd.dismiss();
        }
    }

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

        if (nofti_appended == true) {
            mBuilder.setProgress(100, progress[0], false);
        } else {
            pd.setProgress((int) (progress[0]));
        }

    }

    @SuppressWarnings("deprecation")
    @Override
    protected void onPostExecute(HttpResponse ui) {

        Log.i("uploader", "Done: " + fileName);

    }

}
4

0 回答 0