2009-12-09 34 views
28

Tôi muốn ứng dụng của mình tải hình ảnh lên máy chủ web. Phần đó hoạt động.Thanh tiến trình trong thanh thông báo khi tải lên hình ảnh?

Tôi tự hỏi liệu có thể bằng cách nào đó hiển thị tiến trình tải lên bằng cách nhập mục nhập trong "thanh thông báo" hay không. Tôi thấy ứng dụng Facebook thực hiện điều này.

Khi bạn chụp ảnh và chọn tải lên, ứng dụng cho phép bạn tiếp tục và bằng cách nào đó đặt thông báo tải lên hình ảnh trong thanh tiến trình trên thanh thông báo. Tôi nghĩ rằng đó là khá trơn. Tôi đoán họ sinh ra một dịch vụ mới hoặc một cái gì đó để xử lý việc tải lên và cập nhật thanh tiến trình trong thanh thông báo mỗi lần như vậy.

Cảm ơn mọi ý tưởng

+2

http://united-coders.com/nico-heid/show-progressbar-in- thông báo-khu vực-như-google-do-khi-tải-từ-android Tôi theo dõi này làm việc tuyệt vời – morty346

+0

[Hiển thị tiến độ trong một thông báo] (http://developer.android.com/guide/topics/ui/notifiers/notifications .html # Progress) cho bất kỳ ai khác tìm thấy điều này ... – aecl755

Trả lời

16

Bạn có thể thiết kế thông báo tùy chỉnh thay vì chỉ chế độ xem thông báo mặc định của tiêu đề và tiêu đề phụ.

gì bạn muốn là here

2

Tôi không phải là người dùng Facebook, vì vậy tôi không biết chính xác những gì bạn đang thấy.

Chắc chắn có thể tiếp tục cập nhật Notification, thay đổi biểu tượng để phản ánh tiến trình đã hoàn thành. Như bạn nghi ngờ, bạn sẽ thực hiện việc này từ một Service với chuỗi nền đang quản lý việc tải lên.

+0

Đồng ý, tôi đoán tôi đang tự hỏi API nào họ đã sử dụng để đặt thanh tiến trình của họ trong thanh thông báo - bảng giao diện người dùng toàn cầu mà bạn có thể trượt xuống bằng ngón tay của bạn hiển thị tiến trình tải xuống từ thị trường v.v. Có vẻ như bảng điều khiển nằm ngoài cho ứng dụng nhưng họ có thể đặt thanh tiến trình của họ trong đó? Cảm ơn – Mark

+0

Vâng, đó là cách thông báo hoạt động ... bạn gửi cho họ dịch vụ nền tảng thông báo và chúng sẽ xuất hiện trong bảng điều khiển đó. Như Klondike đã nói, bạn có thể hiển thị bất kỳ chế độ xem nào làm cơ quan thông báo, vì vậy không có phép thuật ở đây. Câu hỏi thú vị hơn là làm thế nào để có được thông tin tiến bộ từ việc tải ảnh lên. Bạn cần phải chia nhỏ quá trình tải lên thành từng phần và tải chúng lên từng cái một. Âm thanh như rất nhiều công việc cho một hiệu ứng khá vô dụng. Ý tôi là, những người trong tâm trí của họ sẽ tiếp tục nhìn chằm chằm vào một thanh tiến trình trong một thông báo ... – Matthias

+0

Đó là hướng dẫn @Mark – Matthias

12

Trong Android, để hiển thị một thanh tiến trình trong một Notification, bạn chỉ cần khởi setProgress(...) vào Notification.Builder.

Lưu ý rằng, trong trường hợp của bạn, có thể bạn sẽ muốn sử dụng ngay cả cờ setOngoing(true).

Integer notificationID = 100; 

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

//Set notification information: 
Notification.Builder notificationBuilder = new Notification.Builder(getApplicationContext()); 
notificationBuilder.setOngoing(true) 
        .setContentTitle("Notification Content Title") 
        .setContentText("Notification Content Text") 
        .setProgress(100, 0, false); 

//Send the notification: 
Notification notification = notificationBuilder.build(); 
notificationManager.notify(notificationID, notification); 

Sau đó, Dịch vụ của bạn sẽ phải thông báo cho tiến trình. Giả sử rằng bạn lưu trữ (phần trăm) tiến bộ của bạn thành một Integer gọi tiến (ví dụ tiến bộ = 10):

//Update notification information: 
notificationBuilder.setProgress(100, progress, false); 

//Send the notification: 
notification = notificationBuilder.build(); 
notificationManager.notify(notificationID, notification); 

Bạn có thể tìm thêm thông tin về Notifications API page: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Progress

-1

tải lớp công khaiVideo mở rộng AsyncTask {

int progress = 0; 
    Notification notification; 
    NotificationManager notificationManager; 
    int id = 10; 

    protected void onPreExecute() { 

    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     HttpURLConnection conn = null; 
     DataOutputStream dos = null; 
     DataInputStream inStream = null; 
     String lineEnd = "\r\n"; 
     String twoHyphens = "--"; 
     String boundary = "*****"; 
     int bytesRead; 
     int sentData = 0;    
     byte[] buffer; 
     String urlString = "http://xxxxx/xxx/xxxxxx.php"; 
     try { 
      UUID uniqueKey = UUID.randomUUID(); 
      fname = uniqueKey.toString(); 
      Log.e("UNIQUE NAME", fname); 
      FileInputStream fileInputStream = new FileInputStream(new File(
        selectedPath)); 
      int length = fileInputStream.available(); 
      URL url = new URL(urlString); 
      conn = (HttpURLConnection) url.openConnection(); 
      conn.setDoInput(true); 
      conn.setDoOutput(true); 
      conn.setUseCaches(false); 
      conn.setRequestMethod("POST"); 
      conn.setRequestProperty("Connection", "Keep-Alive"); 
      conn.setRequestProperty("Content-Type", 
        "multipart/form-data;boundary=" + boundary); 
      dos = new DataOutputStream(conn.getOutputStream()); 
      dos.writeBytes(twoHyphens + boundary + lineEnd); 
      dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" 
        + fname + "" + lineEnd); 
      dos.writeBytes(lineEnd); 
      buffer = new byte[8192]; 
      bytesRead = 0; 
      while ((bytesRead = fileInputStream.read(buffer)) > 0) { 
       dos.write(buffer, 0, bytesRead); 
       sentData += bytesRead; 
       int progress = (int) ((sentData/(float) length) * 100); 
       publishProgress(progress); 
      } 
      dos.writeBytes(lineEnd); 
      dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 
      Log.e("Debug", "File is written"); 
      fileInputStream.close(); 
      dos.flush(); 
      dos.close(); 

     } catch (MalformedURLException ex) { 
      Log.e("Debug", "error: " + ex.getMessage(), ex); 
     } catch (IOException ioe) { 
      Log.e("Debug", "error: " + ioe.getMessage(), ioe); 
     } 
     // ------------------ read the SERVER RESPONSE 
     try { 
      inStream = new DataInputStream(conn.getInputStream()); 
      String str; 
      while ((str = inStream.readLine()) != null) { 
       Log.e("Debug", "Server Response " + str); 
      } 
      inStream.close(); 

     } catch (IOException ioex) { 
      Log.e("Debug", "error: " + ioex.getMessage(), ioex); 
     } 

     return null; 
    } 

    @Override 
    protected void onProgressUpdate(Integer... progress) { 

     Intent intent = new Intent(); 
     final PendingIntent pendingIntent = PendingIntent.getActivity(
       getApplicationContext(), 0, intent, 0); 
     notification = new Notification(R.drawable.video_upload, 
       "Uploading file", System.currentTimeMillis()); 
     notification.flags = notification.flags 
       | Notification.FLAG_ONGOING_EVENT; 
     notification.contentView = new RemoteViews(getApplicationContext() 
       .getPackageName(), R.layout.upload_progress_bar); 
     notification.contentIntent = pendingIntent; 
     notification.contentView.setImageViewResource(R.id.status_icon, 
       R.drawable.video_upload); 
     notification.contentView.setTextViewText(R.id.status_text, 
       "Uploading..."); 
     notification.contentView.setProgressBar(R.id.progressBar1, 100, 
       progress[0], false); 
     getApplicationContext(); 
     notificationManager = (NotificationManager) getApplicationContext() 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(id, notification); 
    } 

    protected void onPostExecute(Void result) { 
     Notification notification = new Notification(); 
     Intent intent1 = new Intent(MultiThreadActivity.this, 
       MultiThreadActivity.class); 
     final PendingIntent pendingIntent = PendingIntent.getActivity(
       getApplicationContext(), 0, intent1, 0); 
     int icon = R.drawable.check_16; // icon from resources 
     CharSequence tickerText = "Video Uploaded Successfully"; // ticker-text 
     CharSequence contentTitle = getResources().getString(
       R.string.app_name); // expanded message 
     // title 
     CharSequence contentText = "Video Uploaded Successfully"; // expanded 
                    // message 
     long when = System.currentTimeMillis(); // notification time 
     Context context = getApplicationContext(); // application 
                // Context 
     notification = new Notification(icon, tickerText, when); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     notification.setLatestEventInfo(context, contentTitle, contentText, 
       pendingIntent); 
     String notificationService = Context.NOTIFICATION_SERVICE; 
     notificationManager = (NotificationManager) context 
       .getSystemService(notificationService); 
     notificationManager.notify(id, notification); 
    } 
} 

kiểm tra điều này nếu nó có thể giúp u

0

Bạn this thư viện và tận hưởng ..! kiểm tra ví dụ để biết thêm chi tiết ..

1

Bạn có thể thử lớp này nó sẽ giúp bạn tạo thông báo

public class FileUploadNotification { 
public static NotificationManager mNotificationManager; 
static NotificationCompat.Builder builder; 
static Context context; 
static int NOTIFICATION_ID = 111; 
static FileUploadNotification fileUploadNotification; 

/*public static FileUploadNotification createInsance(Context context) { 
    if(fileUploadNotification == null) 
     fileUploadNotification = new FileUploadNotification(context); 

    return fileUploadNotification; 
}*/ 
public FileUploadNotification(Context context) { 
    mNotificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); 
    builder = new NotificationCompat.Builder(context); 
    builder.setContentTitle("start uploading...") 
      .setContentText("file name") 
      .setSmallIcon(android.R.drawable.stat_sys_upload) 
      .setProgress(100, 0, false) 
      .setAutoCancel(false); 
} 

public static void updateNotification(String percent, String fileName, String contentText) { 
    try { 
     builder.setContentText(contentText) 
       .setContentTitle(fileName) 
       //.setSmallIcon(android.R.drawable.stat_sys_download) 
       .setOngoing(true) 
       .setContentInfo(percent + "%") 
       .setProgress(100, Integer.parseInt(percent), false); 

     mNotificationManager.notify(NOTIFICATION_ID, builder.build()); 
     if (Integer.parseInt(percent) == 100) 
      deleteNotification(); 

    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     Log.e("Error...Notification.", e.getMessage() + "....."); 
     e.printStackTrace(); 
    } 
} 

public static void failUploadNotification(/*int percentage, String fileName*/) { 
    Log.e("downloadsize", "failed notification..."); 

    if (builder != null) { 
     /* if (percentage < 100) {*/ 
     builder.setContentText("Uploading Failed") 
       //.setContentTitle(fileName) 
       .setSmallIcon(android.R.drawable.stat_sys_upload_done) 
       .setOngoing(false); 
     mNotificationManager.notify(NOTIFICATION_ID, builder.build()); 
     /*} else { 
      mNotificationManager.cancel(NOTIFICATION_ID); 
      builder = null; 
     }*/ 
    } else { 
     mNotificationManager.cancel(NOTIFICATION_ID); 
    } 
} 

public static void deleteNotification() { 
    mNotificationManager.cancel(NOTIFICATION_ID); 
    builder = null; 
} 
} 
Các vấn đề liên quan