2011-06-21 36 views
21

es posible iniciar un servicio a partir de una notificación. La forma normal de comenzar una actividad funciona a la perfección, pero necesito algunos controles previos de datos antes de iniciar realmente la aplicación.Iniciar el servicio desde la notificación

Lo he probado incluyendo un servicio válido en el intento de notificación, pero no pasa nada.

Respuesta

65

Es posible poner en marcha un servicio de notificación.

Tienes que usar PendingIntent.getService en lugar de pendingIntent.getActivity.

Intent notificationIntent = new Intent(mContext, HandleNotificationClickService.class); 
PendingIntent pendingIntent = PendingIntent.getService(mContext, 0, notificationIntent, 0); 

Notification notification = new Notification(icon, tickerText,System.currentTimeMillis()); 
notification.setLatestEventInfo(mContext,contentTitle , contentText, pendingIntent); 
notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT; 

NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); 

notificationManager.notify(CALLER_ID_NOTIFICATION_ID, notification); 
+3

¡Definitivamente debería ser la respuesta aceptada! "PendingIntent.getService en vez de pendingIntent.getActivity" es la forma correcta de hacerlo. – Christian

+0

@keide cómo pausar el servicio usando pendingIntent en la notificación. necesitamos llamar al método de parada de la clase de servicio. –

+0

Cuando comienzo el servicio como se describió anteriormente recibo una excepción de tiempo de ejecución: Subproceso [<1> principal] (Suspendido (excepción RuntimeException)) - - ActivityThread.handleCreateService (ActivityThread $ CreateServiceData) línea: 2561 - ActivityThread .access $ 1600 (ActivityThread, ActivityThread $ CreateServiceData) línea: 141 – samo

5

Cree un receptor de difusión, reciba el mensaje de la notificación y luego inicie el servicio.

+0

Gracias por el Tipp con el BroadcastReceiver. Podría poner todos los cheques así que ya no necesito el servicio. – AlexVogel

1
private void createNotification(String message) 

{ 

    Intent intent = new Intent(this, Yourservice.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    PendingIntent pendingIntent = PendingIntent.getService(this, 0 /* Request code */, intent, 
     0); 

    NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this) 
     .setSmallIcon(R.drawable.icond) 
     .setContentTitle("Start Launcher") 
     .setContentText(message) 
     .setAutoCancel(true) 
     .setOngoing(true) 
     .setWhen(System.currentTimeMillis()) 
     .setContentIntent(pendingIntent); 

    NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); 

    notificationManager.notify(ID_NOTIFICATION , notificationBuilder.build()); 

} 
+3

Agregue la explicación también –

+0

simplemente pase su cadena de mensaje en el método llame a este createNotification ("prueba"); y .setContentTitle ("Start Launcher") es su título de notificación Yourservice.class es el nombre del servicio creado. –

Cuestiones relacionadas