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();
}
}

View File

@ -19,7 +19,7 @@
</activity>
<service android:name="org.solovyev.android.calculator.CalculationService"/>
<service android:name=".CalculationServiceImpl"/>
<!--NOTE: a:configChanges="orientation|keyboardHidden" is needed to correct work of dialog windows (not to close them on orientation change) -->
<activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_app_settings" android:name=".CalculatorPreferencesActivity"/>

View File

@ -11,3 +11,4 @@
target=android-15
android.library.reference.1=../calculatorpp-service
android.library.reference.2=gen-external-apklibs/org.solovyev.android_common_0.1

View File

@ -7,9 +7,10 @@ package org.solovyev.android.calculator;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.SharedPreferences;
import android.content.*;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.text.ClipboardManager;
@ -53,13 +54,27 @@ import org.solovyev.common.utils.history.HistoryAction;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
public class CalculatorActivity extends Activity implements FontSizeAdjuster, SharedPreferences.OnSharedPreferenceChangeListener {
public class CalculatorActivity extends Activity implements FontSizeAdjuster, SharedPreferences.OnSharedPreferenceChangeListener, ServiceConnection {
private static final int HVGA_WIDTH_PIXELS = 320;
@Nullable
private IBillingObserver billingObserver;
@Nullable
private ICalculationService calculationService;
@Override
public void onServiceConnected(ComponentName componentName, IBinder binder) {
if (binder instanceof LocalBinder) {
calculationService = (ICalculationService)((LocalBinder) binder).getService();
}
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
public static enum Theme {
default_theme(ThemeType.other, R.style.default_theme),
@ -188,6 +203,8 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
super.onCreate(savedInstanceState);
setLayout(preferences);
bindService(new Intent(this, CalculationServiceImpl.class), this, Context.BIND_AUTO_CREATE);
if (customTitleSupported) {
try {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.calc_title);