简体   繁体   中英

Android Notification Bar Doesn't Change Progress

I am trying to update the notification bar as the upload is in progress. The notification displays but its progress doesn't change and in the end when the upload is complete, notification destroys. Possible reasons of notification progress not changing ?

My code :

`

 class UploadFile extends AsyncTask<File, Integer, String> {

    @Override
    protected String doInBackground(File... params) {

        String fileName = params[0].getName().replaceAll(" ", "_");
        fileName = "gursahib".replaceAll(" ", "_") + ".mp4";
        long currentTimeMillis = System.currentTimeMillis();

        if (uploadFile(params[0], currentTimeMillis + "_" + fileName).equals("200")) {
            /*
             * if(postRequest("http://www.phando.com/user/media",
             * getMediaRequestParameters(fileName, currentTimeMillis)) ==
             * 200){
             * 
             * }else { return "post media failed."; }
             */
        } else
            return "File Not Uploaded Successfully.";

        // postRequest("http://www.phando.com/user/transcoding",
        // getTranscodingRequest());
        // postRequest("http://www.phando.com/user/ticket",
        // getTicketRequest());
        // Log.d("gursahib", "done");
        return fileName;

    }

    @SuppressWarnings("deprecation")
    private String uploadFile(File file, String fileName) {
        String responseString = null;

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(Config.FILE_UPLOAD_URL);
        int statusCode = 0;
        try {
            AndroidMultiPartEntity entity = new AndroidMultiPartEntity(new ProgressListener() {

                @Override
                public void transferred(long num) {
                    publishProgress((int) ((num / (float) totalSize) * 100));
                }
            });

            File sourceFile = file;

            // Adding file data to http body
            entity.addPart("image", new FileBody(sourceFile, ContentType.DEFAULT_BINARY, fileName));
            totalSize = entity.getContentLength();
            httppost.setEntity(entity);

            // Making server call
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity r_entity = response.getEntity();

            statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                responseString = EntityUtils.toString(r_entity);
            } else {
                responseString = "Error occurred! Http Status Code: " + statusCode;
            }

        } catch (ClientProtocolException e) {
            responseString = e.toString();
        } catch (IOException e) {
            responseString = e.toString();
        }

        return String.valueOf(statusCode);

    }

    private int postRequest(String url, List<NameValuePair> nameValuePairs) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);

        try {
            // Add your data
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

            return response.getStatusLine().getStatusCode();

        } catch (ClientProtocolException e) {
            Log.e("error", e.getMessage());
        } catch (IOException e) {
            Log.e("error", e.getMessage());
        }
        return 0;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        notificationBuilder.setProgress(100, progress[0], false);
        super.onProgressUpdate();
    }

    @Override
    protected void onPostExecute(String result) {
        notificationManager.cancel(notificationID);
        super.onPostExecute(result);

    }
}

`

Control goes to onProgressUpdate but the notification doesn't change

U need to notify also onProgressUpdate of AsyncTask

notificationBuilder.setProgress(100, values[0], false);     
    notificationBuilder.notify(id,notificationBuilder.build());

You need to call build() to your builder. After that, notify to your notification manager.

Notification notification = 
    notificationBuilder.setProgress(100, progress[0], false).build();
notificationManager.notify(notificationID, notification);

Note that you need to set icon or other staff to your builder as well. This SDK code might be helpful to you.

http://tools.oesf.biz/android-5.0.1_r1.0/xref/packages/providers/DownloadProvider/src/com/android/providers/downloads/DownloadNotifier.java#233

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM