Created
February 2, 2018 06:03
-
-
Save piyush-malaviya/379c8f5d308a34aa80b6a32bf668f8bd to your computer and use it in GitHub Desktop.
Example for uploading file using foreground service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Service; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.graphics.BitmapFactory; | |
import android.os.Handler; | |
import android.os.IBinder; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.NotificationCompat; | |
import android.support.v4.app.NotificationManagerCompat; | |
import android.text.TextUtils; | |
import android.util.Log; | |
import java.util.ArrayList; | |
import java.util.concurrent.ArrayBlockingQueue; | |
import java.util.concurrent.BlockingQueue; | |
public class FileUploadService extends Service { | |
public static final String START_UPLOAD = "START_UPLOAD"; | |
public static final String CANCEL_UPLOAD = "CANCEL_UPLOAD"; | |
private static final String FILE_LIST = "FILE_LIST"; | |
private static final String TAG = FileUploadService.class.getSimpleName(); | |
private static final String CHANNEL_ID = "FILE_UPLOAD"; | |
private static final int NOTIFICATION_ID = 111; | |
private BlockingQueue<String> fileQueue = new ArrayBlockingQueue<>(100); | |
private Handler mFileUploadHandler = new Handler(); | |
private FileUploadTask mFileUploadTask; | |
private boolean isFileUploadRunning; | |
public static void startUpload(Context context, ArrayList<String> fileList) { | |
Intent intent = new Intent(context, FileUploadService.class); | |
intent.setAction(START_UPLOAD); | |
intent.putStringArrayListExtra(FILE_LIST, fileList); | |
context.startService(intent); | |
} | |
public static void cancelUpload(Context context) { | |
Intent intent = new Intent(context, FileUploadService.class); | |
intent.setAction(CANCEL_UPLOAD); | |
context.startService(intent); | |
} | |
@Nullable | |
@Override | |
public IBinder onBind(Intent intent) { | |
return null; | |
} | |
@Override | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
String action = intent.getAction(); | |
if (!TextUtils.isEmpty(action)) { | |
switch (action) { | |
case START_UPLOAD: | |
ArrayList<String> fileList = intent.getStringArrayListExtra(FILE_LIST); | |
if (fileList != null) { | |
startFileUpload(fileList); | |
} | |
break; | |
case CANCEL_UPLOAD: | |
cancelFileUpload(); | |
break; | |
} | |
} | |
return super.onStartCommand(intent, flags, startId); | |
} | |
private void cancelFileUpload() { | |
fileQueue.clear(); | |
if (mFileUploadTask != null) { | |
mFileUploadHandler.removeCallbacks(mFileUploadTask); | |
isFileUploadRunning = false; | |
mFileUploadTask = null; | |
hideNotification(); | |
} | |
} | |
private void startFileUpload(ArrayList<String> fileList) { | |
for (String filePath : fileList) { | |
if (!fileQueue.contains(filePath)) { | |
fileQueue.add(filePath); | |
} | |
} | |
startUploadingFile(); | |
} | |
private void startUploadingFile() { | |
if (mFileUploadTask == null) { | |
mFileUploadTask = new FileUploadTask(); | |
} | |
if (!isFileUploadRunning) { | |
isFileUploadRunning = true; | |
mFileUploadHandler.post(mFileUploadTask); | |
} | |
} | |
class FileUploadTask implements Runnable { | |
@Override | |
public void run() { | |
if (fileQueue.isEmpty()) { | |
isFileUploadRunning = false; | |
Log.e(TAG, "File Upload Complete."); | |
hideNotification(); | |
return; | |
} | |
try { | |
String filePath = fileQueue.take(); | |
Log.e(TAG, "Upload File: " + filePath); | |
callUploadFileApi(filePath); | |
showUploadNotification(filePath); | |
Thread.sleep(2000); | |
mFileUploadHandler.post(this); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
private void callUploadFileApi(String filePath) { | |
} | |
private void hideNotification() { | |
NotificationManagerCompat.from(this).cancel(NOTIFICATION_ID); | |
stopForeground(true); | |
} | |
private void showUploadNotification(String fileName) { | |
String messageText = ""; | |
if (fileQueue.size() > 0) { | |
messageText = messageText + "\n" + fileQueue.size() + " is remaining."; | |
} | |
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID); | |
builder.setContentTitle("File Upload") | |
.setContentText(messageText) | |
//.setSmallIcon(R.drawable.ic_checkmark) | |
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) | |
.setAutoCancel(true); | |
startForeground(NOTIFICATION_ID, builder.build()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment