Service implementation

This commit is contained in:
Sergey Solovyev
2012-03-13 23:23:44 +04:00
parent 565e9d6460
commit 97f6e24903
6 changed files with 70 additions and 7 deletions

View File

@@ -12,11 +12,14 @@ import org.jetbrains.annotations.NotNull;
* Date: 3/5/12
* Time: 10:23 PM
*/
public class CalculationService extends Service {
public class CalculationServiceImpl extends Service implements ICalculationService {
@NotNull
private ServiceHandler handler;
public CalculationServiceImpl() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, ".CalculationService.onStartCommand", Toast.LENGTH_SHORT).show();
@@ -56,7 +59,7 @@ public class CalculationService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
return new LocalBinder<ICalculationService>(this);
}
private final class ServiceHandler extends Handler {
@@ -67,7 +70,7 @@ public class CalculationService extends Service {
@Override
public void handleMessage(@NotNull Message msg) {
Toast.makeText(CalculationService.this, "Doing job!", Toast.LENGTH_SHORT).show();
Toast.makeText(CalculationServiceImpl.this, "Doing job!", Toast.LENGTH_SHORT).show();
stopSelf(msg.arg1);
}
}

View File

@@ -0,0 +1,9 @@
package org.solovyev.android.calculator;
/**
* User: serso
* Date: 3/5/12
* Time: 10:23 PM
*/
public interface ICalculationService {
}

View File

@@ -0,0 +1,33 @@
package org.solovyev.android.calculator;
import android.os.Binder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.ref.WeakReference;
/**
* A generic implementation of Binder to be used for local services
*
* @param <S> The type of the service being bound
* @author Geoff Bruckner 12th December 2009
*/
public class LocalBinder<S> extends Binder {
@NotNull
private static String TAG = "LocalBinder";
@NotNull
private WeakReference<S> serviceReference;
public LocalBinder(@NotNull S service) {
serviceReference = new WeakReference<S>(service);
}
@Nullable
public S getService() {
return serviceReference.get();
}
}