Service implementation
This commit is contained in:
parent
565e9d6460
commit
97f6e24903
@ -12,11 +12,14 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
* Date: 3/5/12
|
* Date: 3/5/12
|
||||||
* Time: 10:23 PM
|
* Time: 10:23 PM
|
||||||
*/
|
*/
|
||||||
public class CalculationService extends Service {
|
public class CalculationServiceImpl extends Service implements ICalculationService {
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private ServiceHandler handler;
|
private ServiceHandler handler;
|
||||||
|
|
||||||
|
public CalculationServiceImpl() {
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
Toast.makeText(this, ".CalculationService.onStartCommand", Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, ".CalculationService.onStartCommand", Toast.LENGTH_SHORT).show();
|
||||||
@ -56,7 +59,7 @@ public class CalculationService extends Service {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IBinder onBind(Intent intent) {
|
public IBinder onBind(Intent intent) {
|
||||||
return null;
|
return new LocalBinder<ICalculationService>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private final class ServiceHandler extends Handler {
|
private final class ServiceHandler extends Handler {
|
||||||
@ -67,7 +70,7 @@ public class CalculationService extends Service {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleMessage(@NotNull Message msg) {
|
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);
|
stopSelf(msg.arg1);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package org.solovyev.android.calculator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User: serso
|
||||||
|
* Date: 3/5/12
|
||||||
|
* Time: 10:23 PM
|
||||||
|
*/
|
||||||
|
public interface ICalculationService {
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
</activity>
|
</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) -->
|
<!--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"/>
|
<activity android:configChanges="orientation|keyboardHidden" android:label="@string/c_app_settings" android:name=".CalculatorPreferencesActivity"/>
|
||||||
|
@ -11,3 +11,4 @@
|
|||||||
target=android-15
|
target=android-15
|
||||||
android.library.reference.1=../calculatorpp-service
|
android.library.reference.1=../calculatorpp-service
|
||||||
android.library.reference.2=gen-external-apklibs/org.solovyev.android_common_0.1
|
android.library.reference.2=gen-external-apklibs/org.solovyev.android_common_0.1
|
||||||
|
|
||||||
|
@ -7,9 +7,10 @@ package org.solovyev.android.calculator;
|
|||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.content.SharedPreferences;
|
import android.content.*;
|
||||||
import android.content.res.Configuration;
|
import android.content.res.Configuration;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.IBinder;
|
||||||
import android.os.Vibrator;
|
import android.os.Vibrator;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.text.ClipboardManager;
|
import android.text.ClipboardManager;
|
||||||
@ -53,14 +54,28 @@ import org.solovyev.common.utils.history.HistoryAction;
|
|||||||
import java.text.DecimalFormatSymbols;
|
import java.text.DecimalFormatSymbols;
|
||||||
import java.util.Locale;
|
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;
|
private static final int HVGA_WIDTH_PIXELS = 320;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private IBillingObserver billingObserver;
|
private IBillingObserver billingObserver;
|
||||||
|
|
||||||
public static enum Theme {
|
@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),
|
default_theme(ThemeType.other, R.style.default_theme),
|
||||||
violet_theme(ThemeType.other, R.style.violet_theme),
|
violet_theme(ThemeType.other, R.style.violet_theme),
|
||||||
@ -188,6 +203,8 @@ public class CalculatorActivity extends Activity implements FontSizeAdjuster, Sh
|
|||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setLayout(preferences);
|
setLayout(preferences);
|
||||||
|
|
||||||
|
bindService(new Intent(this, CalculationServiceImpl.class), this, Context.BIND_AUTO_CREATE);
|
||||||
|
|
||||||
if (customTitleSupported) {
|
if (customTitleSupported) {
|
||||||
try {
|
try {
|
||||||
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.calc_title);
|
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.calc_title);
|
||||||
|
Loading…
Reference in New Issue
Block a user