2016-04-26 8 views
0

Ich implementiere einen Download-Dienst, in dem ich ein Bild von der URL herunterlade. Sobald der Dienst gestartet wird, sollte eine Benachrichtigung angezeigt werden, die den Download mit Fortschrittsbalken anzeigt. Der Fortschrittsbalken sollte jedes Mal aktualisiert werden und nach Abschluss des Downloads sollte "Download abgeschlossen" angezeigt werden. Wie auch immer, sobald der Download abgeschlossen ist, verschwindet die Benachrichtigungsleiste. Außerdem kann ich das Fortschrittsupdate nicht anzeigen. Ich möchte dies mit IntentService implementieren. Bitte helfen Sie. Unten ist mein Code.Die Benachrichtigungsleiste verschwindet, nachdem der Download abgeschlossen ist.

public class DownloadService extends IntentService { 

private int result = Activity.RESULT_CANCELED; 
private String imageUrl = "http://onman.ir/colorfinder/sample.jpg"; 
private String folderName = "musafir downloads"; 
private String fileName = "sampleImage.png"; 
NotificationCompat.Builder notification; 
Notification noti; 
NotificationManager manager; 
private static final int NOTIFICATION_ID = 1; 
private File downloadedFile; 

public DownloadService() { 
    super("Download Service"); 

} 


@Override 
protected void onHandleIntent(Intent intent) { 

    Log.e("<><>", "Service started"); 
    createNotification("Download in progress"); 
    downloadedFile = new File(Environment.getExternalStorageDirectory()+"/"+folderName, fileName); 

    if(downloadedFile.exists()) 
    { 
     downloadedFile.delete(); 
    } 

    InputStream is = null; 
    FileOutputStream fos = null; 
    Bitmap bitmap = null; 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inSampleSize = 1; 

    try{ 
     Log.e("<><>", "Inside try"); 
     URL url = new URL(imageUrl); 
     /* HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     try{ 
      conn.setRequestMethod("GET"); 
      conn.connect(); 
      if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){ 
       is = conn.getInputStream(); 
      } 
     }catch (Exception ex) 
     { 

     }*/ 
     URLConnection urlConnection = url.openConnection(); 
     int fileSize = urlConnection.getContentLength(); 
     Log.e("size", fileSize+""); 
     is = urlConnection.getInputStream(); 
     fos = new FileOutputStream(downloadedFile); 

     byte[] data = new byte[fileSize]; 
     int incr = fileSize/100; 
     ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 
     int count = -1; 
     int progress = 0; 

     while((count = is.read(data, 0, incr))!=-1) 
     { 
      progress += count; 
      int per = (progress * 100/fileSize); 
      outStream.write(data, 0, count); 
      publishProgress(per); 


     } 
     bitmap = BitmapFactory.decodeByteArray(outStream.toByteArray(), 0, data.length); 

     bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); 
     fos.flush(); 

     //successfully finished 
     result = Activity.RESULT_OK; 

    }catch (Exception ex) 
    { 

    }finally { 
     if(is!=null) 
     { 
      try { 
       is.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     if(fos!=null) 
     { 
      try { 
       fos.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 


    publishResult("Download completed"); 

    Log.e("<><>", "Download finished"); 
} 

private void publishProgress(final int per) { 
    new Thread(new Runnable() { 
     @Override 
     public void run() { 
      notification.setContentText("Download in progress"); 
      notification.setProgress(100, per, false); 
      noti = notification.build(); 
      noti.flags |= Notification.FLAG_AUTO_CANCEL; 
      noti.flags |= Notification.FLAG_NO_CLEAR; 
      manager.notify(NOTIFICATION_ID, noti); 
      // startForeground(NOTIFICATION_ID, noti); 


     } 
    }); 
} 

private void publishResult(String status) { 
    if(result==Activity.RESULT_OK) 
    { 
     //notification.setProgress(0, 0, true); 
     /*notification.setContentText("Download completed");*/ 
     /*createNotification("Download completed");*/ 
     notification.setContentText(status); 
     notification.setProgress(0, 0, false); 
     noti = notification.build(); 
     noti.flags |= Notification.FLAG_AUTO_CANCEL; 
     noti.flags |= Notification.FLAG_NO_CLEAR; 
     manager.notify(NOTIFICATION_ID, noti); 
     // startForeground(NOTIFICATION_ID, noti); 
    } 
} 

private void createNotification(String status) { 
    Intent notifyintent = new Intent(DownloadService.this, NotificationProcessorActivity.class); 

    if(downloadedFile!=null) { 
     notifyintent.setAction(Intent.ACTION_VIEW); 
     notifyintent.setDataAndType(Uri.fromFile(downloadedFile), "image/*"); 
    } 
    PendingIntent pIntent = PendingIntent.getActivity(this, 1000, notifyintent, 0); 


    //Build notification 
    notification = (NotificationCompat.Builder) new NotificationCompat.Builder(this) 
      .setContentTitle("Image Download") 
      .setContentText(status).setSmallIcon(R.mipmap.ic_launcher) 
      .setContentIntent(pIntent) 
      .setProgress(0,0, true) 
      .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); 

    noti = notification.build(); 

    manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    noti.flags |= Notification.FLAG_AUTO_CANCEL; 
    noti.flags |= Notification.FLAG_NO_CLEAR; 
    noti.flags |= Notification.PRIORITY_HIGH; 
    startForeground(NOTIFICATION_ID, noti); 

} 

}

Antwort

0

Die Benachrichtigung, weil in Ihrer publishResult() Funktion entlassen wird immer Ihre setzen Fortschritt setProgress(0, 0, false) Versuchen Sie,:

setProgress(100, 100, false); 
+0

Ich habe versucht zu tun, wie Sie vorgeschlagen. Das Problem ist immer noch nicht gelöst –

+0

die Benachrichtigung wird immer noch abgewiesen? Versuchen Sie, false in true zu ändern: setProgress (100, 100, true); – Isj

Verwandte Themen