奇怪的通知进度条问题

时间:2018-02-15 07:18:45

标签: java android push-notification progress-bar

开发应用,当用户从我的应用下载内容时,会显示带有进度条的推送通知。在kitkat中它工作得很好,同时下载显示进度条,下载完成后进度条完成,同时在Android 7.0和8.0中测试相同,这里下载内容后,进度条也永远不会完成。下面是我的裁剪代码进度条 -

// background task to download file
    private class BackTask extends AsyncTask<String,Integer,Void> {
        NotificationManager mNotifyManager;
        NotificationCompat.Builder mBuilder;

        protected void onPreExecute() {
            super.onPreExecute();
            mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT);

                // Configure the notification channel.
                notificationChannel.setDescription("Channel description");
                notificationChannel.enableLights(true);
                notificationChannel.setLightColor(Color.RED);
                notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
                notificationChannel.enableVibration(true);
                mNotifyManager.createNotificationChannel(notificationChannel);
            }

            mBuilder = new NotificationCompat.Builder(VideoPlayActivity.this, NOTIFICATION_CHANNEL_ID);
            mBuilder.setContentTitle("File Download")
                    .setContentText("Download in progress")
                    .setSmallIcon(R.drawable.doupnowlogo);

            Toast.makeText(getApplicationContext(), "Downloading file...", Toast.LENGTH_SHORT).show();

        }

        protected Void doInBackground(String...params){
            URL url;
            int count;
            try {
                url = new URL(params[0].replaceAll(" ", "%20"));
                String pathl="";
                try {
                    File f=new File(storeDir);
                    if(f.exists()){
                        HttpURLConnection con=(HttpURLConnection)url.openConnection();
                        InputStream is=con.getInputStream();
                        String pathr=url.getPath();
                        String filename=pathr.substring(pathr.lastIndexOf('/')+1);
                        pathl=storeDir+"/"+filename;
                        FileOutputStream fos=new FileOutputStream(pathl);
                        int lenghtOfFile = con.getContentLength();
                        byte data[] = new byte[1024];
                        long total = 0;
                        while ((count = is.read(data)) != -1) {
                            total += count;
                            // publishing the progress
                            publishProgress((int)((total*100)/lenghtOfFile));
                            // writing data to output file
                            fos.write(data, 0, count);
                        }

                        is.close();
                        fos.flush();
                        fos.close();
                    }
                    else{
                        Log.e("Error","Not found: "+storeDir);

                    }

                } catch (Exception e) {
                    e.printStackTrace();

                }

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

            return null;

        }

        protected void onProgressUpdate(Integer... progress) {
            mBuilder.setProgress(100, progress[0], false);
            // Displays the progress bar on notification
            mNotifyManager.notify(NOTIFICATION_ID, mBuilder.build());
        }

        protected void onPostExecute(Void result){
            mBuilder.setContentText("Download complete");
            // Removes the progress bar
            //mBuilder.setProgress(0,0,false);
            mNotifyManager.notify(NOTIFICATION_ID, mBuilder.build());
        }
    }

如果需要更多代码,请告诉我,我会在此处更新。

0 个答案:

没有答案
相关问题