2017-08-24 33 views
0

私はビデオをfirebaseにアップロードしています。シークバーと通知の両方で進行状況を表示したいと思います。私のシークバーが適切に動作しますが、進捗状況と私の通知は、シークバーが正常に動作しますが、進行に通知が時々ファイルが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

答えて

1

あなたのスレッドのコードが主な問題である。この方法で動作している、なぜそれを把握することが手順に従ってではなく、参照してください。ループは5秒ごとに反復します。したがって、あなたの答えは "それはもうすぐ進行中の通知を示しています。" コードからスレッドを削除します。 Developer Guide

これを行うには、AsynTaskを使用できます。コードは以下の通りである:必要

new YourTaskLoader().execute(); 

とそのループではonProgress方法 のコード

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

にループが終わった後とき

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; 
    } 
} 

は、次のコードで、あなたのAsyncTaskを呼び出します通知バーの進行状況が削除されるように、次の行を呼び出します。
onSuccessメソッドにこれらの行を追加できます。

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

私はそれが役に立ちそうです。

+0

ありがとう、私はあなたの助言に従った。あなたは大部分の権利を得た。私はdoInBackgoundにも通知コードを含めた。今すぐ稼働中の細かい – Pritish

+0

@abuあなたは大歓迎です。 onCreate内でnotificationManagerとbuilderを初期化することもできます。このコードをdoInBackgroundに配置する必要はありません。 – Sahil

関連する問題