Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d8d2c0e

Browse files
author
Elias Naur
committed
Use wakelock to keep upload running while device is sleeping
1 parent 3e00bcb commit d8d2c0e

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/com/alexbbb/uploadservice/UploadService.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import javax.net.ssl.HttpsURLConnection;
1515
import javax.net.ssl.SSLSession;
1616

17+
import android.os.PowerManager;
1718
import android.app.IntentService;
1819
import android.content.Intent;
1920
import android.support.v4.app.NotificationCompat;
@@ -29,6 +30,7 @@
2930
public class UploadService extends IntentService {
3031

3132
private static final String SERVICE_NAME = UploadService.class.getName();
33+
private static final String TAG = "AndroidUploadService";
3234

3335
private static final int UPLOAD_NOTIFICATION_ID = 1234; // Something unique
3436
private static final int BUFFER_SIZE = 4096;
@@ -55,6 +57,7 @@ public class UploadService extends IntentService {
5557
public static final String SERVER_RESPONSE_MESSAGE = "serverResponseMessage";
5658

5759
private Builder notification;
60+
private PowerManager.WakeLock wakeLock;
5861
private UploadNotificationConfig notificationConfig;
5962
private int lastPublishedProgress;
6063

@@ -97,6 +100,8 @@ public void onCreate() {
97100
super.onCreate();
98101
notification = new NotificationCompat.Builder(this)
99102
.setOngoing(true);
103+
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
104+
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
100105
}
101106

102107
@Override
@@ -113,12 +118,15 @@ protected void onHandleIntent(Intent intent) {
113118
final ArrayList<NameValue> parameters = intent.getParcelableArrayListExtra(PARAM_REQUEST_PARAMETERS);
114119

115120
lastPublishedProgress = 0;
121+
wakeLock.acquire();
116122
try {
117123
createNotification();
118124
handleFileUpload(uploadId, url, files, headers, parameters);
119125
} catch (Exception exception) {
120126
broadcastError(uploadId, exception);
121-
}
127+
} finally {
128+
wakeLock.release();
129+
}
122130
}
123131
}
124132
}

0 commit comments

Comments
 (0)