奇怪的问题,通知延迟下载

时间:2019-04-27 15:30:30

标签: android

我正在创建一个下载电影的应用程序,并显示下载进度的通知,但由于某种原因,发生了一些奇怪的事情。在onProgressUpdate函数中,我调用了另一个名为sendNotification的函数,该函数以新的进度更新通知,但是当我调用该函数时,下载会变得非常缓慢,并且在两者之间停止。

DownloadTask:

        public class DownloadTask extends AsyncTask<String, Integer, String> {

            private Context context;
            private PowerManager.WakeLock mWakeLock;

            public DownloadTask(Context context) {
                this.context = context;
            }

            @Override
            protected String doInBackground(String... sUrl) {
                InputStream input = null;
                OutputStream output = null;
                HttpURLConnection connection = null;
                try {
                    URL url = new URL(sUrl[0]);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.connect();

                    if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                        return "Server returned HTTP " + connection.getResponseCode()
                                + " " + connection.getResponseMessage();
                    }
                    int fileLength = connection.getContentLength();

                    // download the file
                    input = connection.getInputStream();
                    output = new FileOutputStream("/sdcard/cflix/filme.mp4");

                    byte data[] = new byte[4096];
                    long total = 0;
                    int count;
                    while ((count = input.read(data)) != -1) {
                        // allow canceling with back button
                        if (isCancelled()) {
                            input.close();
                            return null;
                        }
                        total += count;
                        if (fileLength > 0){
                            publishProgress((int) (total * 100 / fileLength));
                        }
                        output.write(data, 0, count);
                    }
                } catch (Exception e) {
                    return e.toString();
                } finally {
                    try {
                        if (output != null)
                            output.close();
                        if (input != null)
                            input.close();
                    } catch (IOException ignored) {
                    }

                    if (connection != null)
                        connection.disconnect();
                }
                return null;
            }
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                sendNotification("https://sm.ign.com/ign_pt/news/t/the-prison/the-prison-break-revival-probably-wouldnt-have-happened-with_zgm4.jpg", 0, false, context);
            }

            @Override
            protected void onProgressUpdate(Integer... progress) {
                super.onProgressUpdate(progress);
                //sendNotification("https://sm.ign.com/ign_pt/news/t/the-prison/the-prison-break-revival-probably-wouldnt-have-happened-with_zgm4.jpg", progress[0], false, context);
            }

            @Override
            protected void onPostExecute(String result) {
                mWakeLock.release();
                if (result != null){
                    Log.i("teste", result);
                }else{
                    sendNotification("https://sm.ign.com/ign_pt/news/t/the-prison/the-prison-break-revival-probably-wouldnt-have-happened-with_zgm4.jpg", 100, true, context);
                }

            }
        }

MainActivity:

    public class MainActivity extends AppCompatActivity {
        private String url = "http://192.168.1.66/vc/escape_room.mp4";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button btnnoti = findViewById(R.id.btnnoti);
            btnnoti.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final DownloadTask downloadTask = new DownloadTask(MainActivity.this);
                    downloadTask.execute(url);
                }
            });
        }
        public static void sendNotification(String urlImg, int percente, boolean complete, Context c){
            int progress = percente;
            String CHANNEL_ID = "immflix";
            CharSequence name = "Downloads";
            String description = "Downloads dos Filmes";
            int importance = NotificationManager.IMPORTANCE_LOW;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            channel.setDescription(description);
            NotificationManager notificationManager = (NotificationManager) c.getSystemService(NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(channel);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(c.getApplicationContext(), CHANNEL_ID);
            Bitmap bmp;
            try {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
                int color = 0xD91820;
                URL url = new URL(urlImg);
                bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                Intent snoozeIntent = new Intent(c.getApplicationContext(), MainActivity.class);
                PendingIntent stopDownload = PendingIntent.getBroadcast(c.getApplicationContext(), 0, snoozeIntent, 0);
                builder.setContentTitle("Prision Break - E5 T3")
                        .setLargeIcon(bmp)
                        .setContentText(progress + "%")
                        .setProgress(100, progress, false)
                        .setColor(color)
                        .setSmallIcon(R.drawable.notifications)
                        .addAction(R.drawable.stop, "Parar", stopDownload)
                        .setPriority(NotificationCompat.PRIORITY_LOW);
            } catch (IOException e) {
                e.printStackTrace();
            }
            int PROGRESS_MAX = 100;
            int PROGRESS_CURRENT = progress;
            builder.setProgress(PROGRESS_MAX, PROGRESS_CURRENT, false);
            int notificationId = 1;
            notificationManager.notify(notificationId, builder.build());
            if(complete == true){
                builder.setContentText("Download Completo!")
                        .setProgress(0,0,false);
                notificationManager.notify(notificationId, builder.build());
            }
        }

    }

0 个答案:

没有答案