2017-05-04 4 views
-2

Je crée une démo de fichiers de téléchargement à partir du service de premier plan et montre la progression du téléchargement dans la barre de progression de notification. J'utilise asynctask en service et en asynctask je télécharge le fichier depuis internet en utilisant HttpUrlConnection et je mets à jour la progression dans onProgressUpdate(). Je montre l'erreur suivante Skipped 145 images! L'application peut faire trop de travail sur son thread principal.L'application peut faire trop de travail sur son thread principal tandis que la barre de progression de la notification de mise à jour d'Asynctask

et le téléphone se pendre.

Service.java 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    Log.d(TAG + "===", "onStartCommand"); 
    String downloadUrl = "", downloadFileName = ""; 
    Bundle bundle = intent.getExtras(); 
    if (bundle != null) { 
     downloadUrl = bundle.getString("download_url"); 
     downloadFileName = bundle.getString("download_filename"); 
    } 
    handler = new Handler(); 
    if (intent.getAction().equalsIgnoreCase(START_DOWNLOAD)) { 

     Intent notificationIntent = new Intent(mContext, MainActivity.class); 
     notificationIntent.setAction(MAIN_ACTION); 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
     PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0); 

     Intent pauseIntent = new Intent(mContext, DownloadService.class); 
     pauseIntent.setAction(PAUSE_DOWNLOAD); 
     PendingIntent pausePendingIntent = PendingIntent.getService(mContext, 0, pauseIntent, 0); 

     Intent stopIntent = new Intent(mContext, DownloadService.class); 
     stopIntent.setAction(STOP_DOWNLOAD); 
     PendingIntent stopPendingIntent = PendingIntent.getService(mContext, 0, stopIntent, 0); 

     mNotiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

     builder = new NotificationCompat.Builder(mContext) 
       .setContentTitle("Download demo") 
       .setTicker("downloading...") 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setOngoing(true) 
       .setContentIntent(pendingIntent) 
       .addAction(R.drawable.ic_pause_download, "pause", pausePendingIntent) 
       .addAction(R.drawable.ic_cancel_download, "stop", stopPendingIntent); 
     notification = builder.build(); 
     startForeground(DOWNLOAD_NOTIFICATION, notification); 

     new DownloadFileAsync(downloadUrl, downloadFileName).execute(); 

    } else if (intent.getAction().equalsIgnoreCase(PAUSE_DOWNLOAD)) { 
     Toast.makeText(mContext.getApplicationContext(), "Download Pause !", Toast.LENGTH_LONG).show(); 

    } else if (intent.getAction().equalsIgnoreCase(STOP_DOWNLOAD)) { 
     Toast.makeText(mContext.getApplicationContext(), "Download Stop !", Toast.LENGTH_LONG).show(); 
     mNotiManager.cancel(DOWNLOAD_NOTIFICATION); 
     stopForeground(true); 
     stopSelf(); 
    } 

    return START_STICKY; 
} 

et mon AsyncTask

private class DownloadFileAsync extends AsyncTask<Void, Integer, Void> { 
    private HttpURLConnection mUrlConnection; 
    private File downloadFile = null; 
    private String downloadUrl = ""; 
    private String downloadFileName = ""; 

    public DownloadFileAsync(String downloadUrl, String downloadFileName) { 
     this.downloadUrl = downloadUrl; 
     this.downloadFileName = downloadFileName; 
    } 

    @Override 
    protected Void doInBackground(Void... objects) { 
     try { 
      URL url = new URL(downloadUrl); 
      mUrlConnection = (HttpURLConnection) url.openConnection(); 
      mUrlConnection.setRequestMethod("GET"); 
      mUrlConnection.setDoOutput(true); 
      mUrlConnection.setReadTimeout(20 * 1000); 
      mUrlConnection.setConnectTimeout(20 * 1000); 
      mUrlConnection.connect(); 
      int responseCode = mUrlConnection.getResponseCode(); 
      Log.e("Response Code===", responseCode + ""); 
      if (responseCode == HttpURLConnection.HTTP_OK) { 
       mDownloadMaxSize = Long.parseLong(mUrlConnection.getHeaderField("content-length")); 
       downloadFile = new File(Environment.getExternalStorageDirectory(), downloadFileName); 
       if (!downloadFile.exists()) { 
        downloadFile.createNewFile(); 
       } else { 
        long length = downloadFile.length(); 
        mUrlConnection.setRequestProperty("Request", "bytes = " + length + "-"); 
       } 
       InputStream in = new BufferedInputStream(mUrlConnection.getInputStream()); 
       DataInputStream dis = new DataInputStream(in); 
       FileOutputStream fos = new FileOutputStream(downloadFile, true); 
       byte buffer[] = new byte[1024]; 
       int length; 

       while ((length = dis.read(buffer)) > 0) { 
        fos.write(buffer, 0, length); 
        mDownloadProgress += length; 
        publishProgress((int) (mDownloadProgress * 100/mDownloadMaxSize)); 
       } 
       in.close(); 
       fos.close(); 
      } 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      if (mUrlConnection != null) { 
       mUrlConnection.disconnect(); 
      } 
     } 
     return null; 
    } 

    @Override 
    protected void onProgressUpdate(Integer... values) { 
     super.onProgressUpdate(values); 

     builder.setProgress(100, values[0], false); 
     mNotiManager.notify(DOWNLOAD_NOTIFICATION, builder.build()); 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 
     builder.setProgress(0, 0, false); 
     mNotiManager.notify(DOWNLOAD_NOTIFICATION, builder.build()); 

    } 
} 

S'il vous plaît aidez-moi ce que je peux faire pour résoudre ce problème. Merci d'avance.

Répondre

0

Votre code fonctionne très bien. Il suffit de mettre quelques retards dans la méthode d'arrière-plan et de voir si vous pouvez voir les changements.