Skip to content

Commit

Permalink
Started to implement the main IntentService for background operations
Browse files Browse the repository at this point in the history
  • Loading branch information
tilmanginzel committed Sep 3, 2015
1 parent 3448fa4 commit 0daa4a2
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions app/src/main/java/de/mobcomp/grades/main/MainService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package de.mobcomp.grades.main;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

import java.util.HashSet;
import java.util.Set;

/**
* This service is used to handle ongoing operations in the background.
* It is mostly used for network operations.
*
* The incoming intent must specify two extra Strings (PROCESSOR_KEY, METHOD_KEY),
* so the service can decide which processor to create and which method to call.
*/
public class MainService extends IntentService {
private static final String TAG = MainService.class.getSimpleName();

// intent extra keys TODO: add corresponding values for processors and methods.
public static final String PROCESSOR_KEY = "processor_key";
public static final String METHOD_KEY = "method_key";
public static final String REQUEST_ID = "request_id";

// save request ids for pending request in this set, and remove them when its done.
private Set<Long> pendingRequest;

/**
* Creates an IntentService. Invoked by your subclass's constructor.
*/
public MainService() {
super(TAG);
pendingRequest = new HashSet<>();
}

@Override
public void onStart(Intent intent, int startId) {
long requestId = intent.getLongExtra(REQUEST_ID, -1);
if (pendingRequest.contains(requestId)) {
// ignore intent if it is already pending
return;
}

// add request to pending requests
pendingRequest.add(requestId);

// add intent to message queue
super.onStart(intent, startId);
}

@Override
protected void onHandleIntent(Intent intent) {
// get desired processor and method from extras
int processor = intent.getIntExtra(PROCESSOR_KEY, -1);
int method = intent.getIntExtra(METHOD_KEY, -1);

switch (processor) {
default:
Log.e(TAG, "Invalid processor call to MainService: " + processor);
break;
}

// remove request id from pending requests
long requestId = intent.getLongExtra(REQUEST_ID, -1);
pendingRequest.remove(requestId);
}
}

0 comments on commit 0daa4a2

Please sign in to comment.