在通知中显示进度

问题描述:

我正在将视频上传到firebase,并且希望在搜索栏和我的通知中都显示进度状态。我的搜索条的行为不当,但我与进度通知一直在进展状况在通知中显示进度

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) { 
     Uri selectedImageUri = data.getData(); 

     // Get a reference to store file at chat_photos/<FILENAME> 
     StorageReference photoRef = mChatPhotosStorageReference.child(selectedImageUri.getLastPathSegment()); 

     // Upload file to Firebase Storage 
     photoRef.putFile(selectedImageUri) 
       .addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() { 
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { 
         // When the image has successfully uploaded, we get its download URL 
         // progressBar.setVisibility(View.VISIBLE); 
         Uri downloadUrl = taskSnapshot.getDownloadUrl(); 

         // Set the download URL to the message box, so that the user can send it to the database 
         Video video = new Video(downloadUrl.toString()); 
         mMessagesDatabaseReference.push().setValue(video); 

        } 
       }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() { 
      @Override 
      public void onProgress(UploadTask.TaskSnapshot taskSnapshot) { 
       final int progress = (int) ((100 * taskSnapshot.getBytesTransferred())/taskSnapshot.getTotalByteCount()); 

       seekBar.setProgress(progress); 
       final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext()) 
         .setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary)) 
         .setSmallIcon(R.mipmap.ic_launcher) 
         .setContentText("Download in progress") 
         .setAutoCancel(true); 

       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 
        notificationBuilder.setPriority(Notification.PRIORITY_HIGH); 
       } 

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

       new Thread(new Runnable() { 
        @Override 
        public void run() { 
         int incr; 
         // Do the "lengthy" operation 20 times 
         for (incr = progress; incr <= 100; incr++) { 
          // Sets the progress indicator to a max value, the 
          // current completion percentage, and "determinate" 
          // state 
          notificationBuilder.setProgress(100, incr, false); 
          // Displays the progress bar for the first time. 
          notificationManager.notify(id, notificationBuilder.build()); 
          // Sleeps the thread, simulating an operation 
          // that takes time 
          try { 
           // Sleep for 5 seconds 
           Thread.sleep(5*1000); 
          } catch (InterruptedException e) { 
           Log.d(TAG, "sleep failure"); 
          } 
         } 
         notificationBuilder.setContentText("Download complete") 
           // Removes the progress bar 
           .setProgress(0,0,false); 
         notificationManager.notify(id, notificationBuilder.build()); 
         } 

        } 
       // Starts the thread by calling the run() method in its Runnable 
       ).start(); 
      } 
     }); 

    } 
} 

搜索栏能正常工作,但进度通知有时会显示下载完成,即使文件没有被上传到火力点,并显示下载完成后显示的下载,它再次显示正在进行的通知。我做错了什么?请帮忙。我已经请参考以下文档&遵循的步骤,但无法弄清楚,为什么我的进度通知行为这样 https://developer.android.com/training/notify-user/display-progress.html

你的线程代码是主要问题。循环每5秒钟迭代一次。因此,你的答案是“它再次显示进行中的通知”。 从您的代码中删除线程。这只是例如Developer Guide

您可以使用AsynTask来做到这一点。代码如下:

private class YourTaskLoader extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     notificationManager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     builder = new NotificationCompat.Builder(this); 
     builder.setContentTitle("Picture upload") 
       .setContentText("Uploading in progress") 
       .setSmallIcon(R.drawable.ic_backup); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     UploadPicture(); 
     return null; 
    } 
} 

通过时需要

new YourTaskLoader().execute(); 

onProgress方法 代码在这个循环

builder.setProgress(100,progress,false); 
notificationManager.notify(1001,builder.build()); 

循环结束后下面的代码调用您的AsyncTask调用以下行,以便删除通知栏中的进度。
您可以在onSuccess方法中添加这些行。

builder.setContentText("Upload Complete") 
     .setProgress(0,0,false); 
notificationManager.notify(1001,builder.build()); 

我希望它有帮助。

+0

谢谢,我遵循你的建议。你大部分都是正确的。我在doInBackgound中也包含了通知代码。现在它的工作很好 – Pritish

+0

@abu欢迎您。您还可以在onCreate中初始化notificationManager和构建器,然后不需要将此代码放入doInBackground中。 – Sahil