Use Service.onStartCommand() instead of deprecated Service.onStart()

This commit is contained in:
cketti 2011-11-10 06:36:14 +01:00
parent 8c8b3eea9e
commit be70120ee5
6 changed files with 27 additions and 13 deletions

View File

@ -199,9 +199,7 @@ public abstract class CoreService extends Service {
}
@Override
public final void onStart(Intent intent, int startId) {
// deprecated method but still used for backwards compatibility with Android version <2.0
public final int onStartCommand(Intent intent, int flags, int startId) {
/*
* When a process is killed due to low memory, it's later restarted and services that were
* started with START_STICKY are started with the intent being null.
@ -213,7 +211,7 @@ public abstract class CoreService extends Service {
*/
if (intent == null) {
stopSelf(startId);
return;
return START_NOT_STICKY;
}
// Acquire new wake lock
@ -253,9 +251,9 @@ public abstract class CoreService extends Service {
// Run the actual start-code of the service
mImmediateShutdown = true;
int startFlag;
try {
super.onStart(intent, startId);
startService(intent, startId);
startFlag = startService(intent, startId);
} finally {
try {
// Release the wake lock acquired at the start of this method
@ -267,9 +265,12 @@ public abstract class CoreService extends Service {
// this service.
if (mAutoShutdown && mImmediateShutdown && startId != -1) {
stopSelf(startId);
startFlag = START_NOT_STICKY;
}
} catch (Exception e) { /* ignore */ }
}
return startFlag;
}
/**
@ -371,7 +372,7 @@ public abstract class CoreService extends Service {
}
/**
* Subclasses need to implement this instead of overriding {@link #onStart(Intent, int)}.
* Subclasses need to implement this instead of overriding {@link #onStartCommand(Intent, int, int)}.
*
* <p>
* This allows {@link CoreService} to manage the service lifecycle, incl. wake lock management.
@ -382,8 +383,12 @@ public abstract class CoreService extends Service {
* @param startId
* A unique integer representing this specific request to start. Use with
* {@link #stopSelfResult(int)}.
*
* @return The return value indicates what semantics the system should use for the service's
* current started state. It may be one of the constants associated with the
* {@link Service#START_CONTINUATION_MASK} bits.
*/
public abstract void startService(Intent intent, int startId);
public abstract int startService(Intent intent, int startId);
@Override
public void onLowMemory() {

View File

@ -84,7 +84,7 @@ public class MailService extends CoreService {
}
@Override
public void startService(Intent intent, int startId) {
public int startService(Intent intent, int startId) {
long startTime = System.currentTimeMillis();
boolean oldIsSyncDisabled = isSyncDisabled();
ConnectivityManager connectivityManager = (ConnectivityManager)getApplication().getSystemService(Context.CONNECTIVITY_SERVICE);
@ -165,6 +165,8 @@ public class MailService extends CoreService {
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "MailService.onStart took " + (System.currentTimeMillis() - startTime) + "ms");
return START_NOT_STICKY;
}
@Override

View File

@ -42,7 +42,7 @@ public class PollService extends CoreService {
}
@Override
public void startService(Intent intent, int startId) {
public int startService(Intent intent, int startId) {
if (START_SERVICE.equals(intent.getAction())) {
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "PollService started with startId = " + startId);
@ -68,6 +68,7 @@ public class PollService extends CoreService {
stopSelf();
}
return START_NOT_STICKY;
}
@Override

View File

@ -27,7 +27,8 @@ public class PushService extends CoreService {
}
@Override
public void startService(Intent intent, int startId) {
public int startService(Intent intent, int startId) {
int startFlag = START_STICKY;
if (START_SERVICE.equals(intent.getAction())) {
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "PushService started with startId = " + startId);
@ -35,8 +36,10 @@ public class PushService extends CoreService {
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "PushService stopping with startId = " + startId);
stopSelf(startId);
startFlag = START_NOT_STICKY;
}
return startFlag;
}
@Override

View File

@ -34,7 +34,7 @@ public class RemoteControlService extends CoreService {
public static final int REMOTE_CONTROL_SERVICE_WAKE_LOCK_TIMEOUT = 20000;
@Override
public void startService(final Intent intent, final int startId) {
public int startService(final Intent intent, final int startId) {
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "RemoteControlService started with startId = " + startId);
final Preferences preferences = Preferences.getPreferences(this);
@ -155,6 +155,8 @@ public class RemoteControlService extends CoreService {
}
, RemoteControlService.REMOTE_CONTROL_SERVICE_WAKE_LOCK_TIMEOUT, startId);
}
return START_NOT_STICKY;
}
}

View File

@ -113,12 +113,13 @@ public class SleepService extends CoreService {
}
@Override
public void startService(Intent intent, int startId) {
public int startService(Intent intent, int startId) {
try {
if (intent.getAction().startsWith(ALARM_FIRED)) {
Integer id = intent.getIntExtra(LATCH_ID, -1);
endSleep(id);
}
return START_NOT_STICKY;
}
finally {
stopSelf(startId);