2011-06-21 16 views
21

Bir bildirimden servis başlatmak mümkün mü? Bir aktiviteyi başlatmanın normal yolu mükemmel bir şekilde çalışıyor, ancak uygulamaya başlamadan önce bazı ön kontrollere ihtiyacım var.Bildirimden Servise Başlamak

ben bildirim niyet geçerli bir hizmet dahil test ettik, ama hiçbir şey olmuyor.

cevap

65

Bildirimden bir hizmet başlatmak mümkündür.

pendingIntent.getActivity yerine PendingIntent.getService kullanmanız gerekir. BroadCastReceiver ile tipp için

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

Bu kesinlikle kabul edilen cevap olmalı! "PendingIntent.getActivity yerine PendingIntent.getService" bunu yapmanın doğru yolu! – Christian

+0

@keide Bildirimde pendingIntent kullanarak hizmeti duraklatma. Hizmet sınıfının durma yöntemini aramalıyız. –

+0

Bir çalışma zamanı özel olsun yukarıda açıklandığı gibi hizmet başlattığınızda: Konu [<1> ana] (Asma (istisna RuntimeException)) - - ActivityThread.handleCreateService (ActivityThread $ CreateServiceData) çizgi: 2561 - ActivityThread .Geçiş 1600 $ (ActivityThread, ActivityThread $ CreateServiceData) hattı: 141 – samo

5

Yayın alıcısı oluşturun, bildirimi iletiden alın ve ardından hizmeti başlatın.

+0

teşekkürler. Bütün çekleri koyabilirsiniz, böylece artık Servis'e ihtiyacım yok. – 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

Ayrıca açıklama ekleyin –

+0

sadece mesaj dizgisini iletin, bu createNotification yöntemine ("test"); ve .setContentTitle ("Başlat Launcher") Bildirim Yourservice.class senin başlığı oluşturulan hizmetin hizmet adı olmasıdır. –

İlgili konular