PDA

View Full Version : returns های onStartCaommand برای Service



harani
شنبه 25 آذر 1391, 18:54 عصر
سلام دوستان در متد OnStartCommand یک Service چه Return هایی وجود داره ؟؟؟

مثل return Service.START_STICKY;//c در کد زیر .


@Override public int onStartCommand(Intent i, int flags , int startId){ return Service.START_STICKY; }



با سپاس از همگی

Modernidea
شنبه 25 آذر 1391, 20:05 عصر
سلام

راهنمای گوگل:


public int onStartCommand (Intent intent, int flags, int startId)

Added in API level 5
Called by the system every time a client explicitly starts the service by calling startService(Intent), providing the arguments it supplied and a unique integer token representing the start request. Do not call this method directly.

For backwards compatibility, the default implementation calls onStart(Intent, int) and returns either START_STICKY or START_STICKY_COMPATIBILITY.

If you need your application to run on platform versions prior to API level 5, you can use the following model to handle the older onStart(Intent, int) callback in that case. The handleCommand method is implemented by you as appropriate:


// This is the old onStart method that will be called on the pre-2.0
// platform. On 2.0 or later we override onStartCommand() so this
// method will not be called.
@Override
public void onStart(Intent intent, int startId) {
handleCommand(intent);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleCommand(intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}


Note that the system calls this on your service's main thread. A service's main thread is the same thread where UI operations take place for Activities running in the same process. You should always avoid stalling the main thread's event loop. When doing long-running operations, network calls, or heavy disk I/O, you should kick off a new thread, or use AsyncTask.

Parameters
intent The Intent supplied to startService(Intent), as given. This may be null if the service is being restarted after its process has gone away, and it had previously returned anything except START_STICKY_COMPATIBILITY.
flags Additional data about this start request. Currently either 0, START_FLAG_REDELIVERY, or START_FLAG_RETRY.
startId A unique integer representing this specific request to start. Use with stopSelfResult(int).
Returns
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 START_CONTINUATION_MASK bits.


منبع: http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent , int, int)