¿Debe onHandleIntent ser llamado cuando IntentService se inicia con bindService?

Mi servicio extiende IntentService y cuando se inicia con startService , onHandleIntent es llamado. Sin embargo, cuando se inicia el servicio con bindService (necesito enlace), onHandleIntent no se llama.

¿Debe onHandleIntent ser llamado cuando IntentService se inicia con bindService ? ¿Es startService la única manera en que IntentService debería iniciarse?

La documentación de IntentService dice lo siguiente:

Los clientes envían solicitudes a través de llamadas startService (Intent); el servicio se inicia según sea necesario, maneja cada intento a su vez usando un hilo de trabajo y se detiene cuando se queda sin trabajo.

Actualmente bindService mi problema llamando a startService justo después de bindService pero lo encuentro feo. Me gustaría saber si hay una manera de hacerlo funcionar con una sola llamada.

Los fragmentos de código siguen, podría ser que me falta algo obvio.

ExampleService.java

 public class ExampleService extends IntentService { private class IncomingHandler extends Handler { @Override public void handleMessage(Message message) { if (message.replyTo != null) { outMessenger = message.replyTo; } } } private Messenger messenger = new Messenger(new IncomingHandler()); private Messenger outMessenger = null; public ExampleService() { super("ExampleService"); } @Override public IBinder onBind(Intent intent) { return messenger.getBinder(); } @Override protected void onHandleIntent(Intent arg0) { System.out.println("Service started"); for (int i = 0; i < 5; i++) { SystemClock.sleep(5000); if (outMessenger != null) { try { outMessenger.send(Message.obtain()); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } 

Service Manifest.xml

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.service" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".ExampleService"> <intent-filter> <action android:name="com.example.service.ExampleService" /> </intent-filter> </service> </application> </manifest> 

MainActivity.java (llamador)

 public class MainActivity extends Activity implements ServiceConnection { private class IncomingHandler extends Handler { @Override public void handleMessage(Message msg) { Toast.makeText(getApplicationContext(), "Message received", Toast.LENGTH_SHORT).show(); System.out.println("Message received!"); super.handleMessage(msg); } } private Messenger messenger = new Messenger(new IncomingHandler()); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent("com.example.service.ExampleService"); bindService(intent, MainActivity.this, Context.BIND_AUTO_CREATE); //startService(intent); } }); } @Override public void onServiceConnected(ComponentName name, IBinder binder) { Message message = Message.obtain(); message.replyTo = messenger; try { new Messenger(binder).send(message); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } } 

¿Debe onHandleIntent ser llamado cuando IntentService se inicia con bindService?

No.

¿Es startService la única manera en que IntentService debería iniciarse?

IMHO, sí. IMHO, IntentService no está diseñado para el patrón de encuadernación.

En su caso, usted puede:

  • Pasar un Messenger de la actividad en un Intent extra en el comando enviado por startService() , o
  • Utilice LocalBroadcastManager o
  • Utilice Otto , o
  • Utilice una transmisión ordenada, si el IntentService puede continuar más allá de la vida de la actividad y desea, por ejemplo, mostrar una Notification cuando el trabajo se hace en ese caso,
  • Etc.

Debe llamar a startService y bindService. Esto funcionó para mí.

  • Cómo obtener el progreso de un IntentService
  • IntentService no se está llamando
  • ¿Qué hacer si el sistema operativo ha matado a un IntentService?
  • Android la diferencia entre onHandleIntent & onStartCommand
  • Utilizar Parcelable con un objeto con Hashmap
  • ¿Es seguro el método onStartCommand (...) de IntentService?
  • ¿Cómo notificar la actividad sobre los cambios en la variable global en la aplicación?
  • Intentservice no hay constructor vacío, pero hay un constructor
  • START_STICKY para IntentService
  • Deteniendo un IntentService
  • WakefulIntentService aclaraciones de implementación
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.