簡體   English   中英

Android通知欄不會更改進度

[英]Android Notification Bar Doesn't Change Progress

我正在嘗試在上傳過程中更新通知欄。 通知顯示但其進度不會更改,並且在上載完成時,通知會終止。 通知進度的可能原因沒有改變?

我的代碼:

`

 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);

    }
}

`

控制轉到onProgressUpdate但通知不會更改

你還需要通知AsyncTask的onProgressUpdate

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

您需要將build()調用到構建器。 之后,通知您的通知經理。

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

請注意,您還需要為構建器設置圖標或其他人員。 此SDK代碼可能對您有所幫助。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM