Skip to content
This repository has been archived by the owner on Jan 14, 2018. It is now read-only.

Send arguments to SpiceService through Intent (2) #413

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;

import com.octo.android.robospice.SpiceService.SpiceServiceBinder;
Expand Down Expand Up @@ -96,6 +97,8 @@ public class SpiceManager implements Runnable {
/** The class of the {@link SpiceService} to bind to. */
private final Class<? extends SpiceService> spiceServiceClass;

/** Service arguments */
private Bundle args;
/** A reference on the {@link SpiceService} obtained by local binding. */
private SpiceService spiceService;
/** {@link SpiceService} binder. */
Expand Down Expand Up @@ -198,10 +201,25 @@ protected int getThreadCount() {
* {@link SpiceService}.
*/
public synchronized void start(final Context context) {
start(context, null);
}

/**
* Start the {@link SpiceManager}. It will bind asynchronously to the
* {@link SpiceService}.
* @param context
* a context that will be used to bind to the service. Typically,
* the Activity or Fragment that needs to interact with the
* {@link SpiceService}.
* @param args
* {@link android.os.Bundle} with parameters for the service
*/
public synchronized void start(final Context context, final Bundle args) {
this.contextWeakReference = new WeakReference<Context>(context);
if (isStarted()) {
throw new IllegalStateException("Already started.");
} else {
this.args = args;
executorService = Executors.newFixedThreadPool(getThreadCount(), new MinPriorityThreadFactory());
// start the binding to the service
runner = new Thread(this, SPICE_MANAGER_THREAD_NAME_PREFIX + spiceManagerThreadIndex++);
Expand Down Expand Up @@ -1188,6 +1206,10 @@ private void bindToService() {

if (spiceService == null) {
final Intent intentService = new Intent(context, spiceServiceClass);
if (args != null) {
intentService.putExtras(args);
args = null;
}
Ln.v("Binding to service.");
spiceServiceConnection = new SpiceServiceConnection();
boolean bound = context.getApplicationContext().bindService(intentService, spiceServiceConnection, Context.BIND_AUTO_CREATE);
Expand Down