2011-06-21 55 views
21

là có thể bắt đầu một dịch vụ từ một thông báo. Cách bình thường để bắt đầu một hoạt động đang hoạt động hoàn hảo, nhưng tôi cần một số kiểm tra trước dữ liệu trước khi thực sự bắt đầu ứng dụng.Dịch vụ Bắt đầu từ Thông báo

Tôi đã thử nghiệm nó với bao gồm một dịch vụ hợp lệ trong mục đích thông báo, nhưng không có gì xảy ra.

Trả lời

65

Có thể bắt đầu dịch vụ từ thông báo.

Bạn phải sử dụng PendingIntent.getService thay vì 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

Điều đó chắc chắn phải là câu trả lời được chấp nhận! "PendingIntent.getService thay vì pendingIntent.getActivity" là cách làm đúng! – Christian

+0

@ tạm dừng cách tạm dừng dịch vụ bằng cách sử dụng pendingIntent trong thông báo. chúng ta cần gọi phương thức stop của lớp dịch vụ. –

+0

Khi tôi bắt đầu dịch vụ như mô tả ở trên, tôi nhận được ngoại lệ thời gian chạy: Chủ đề [<1> main] (Bị treo (ngoại lệ RuntimeException)) - - Dòng ActivityThread.handleCreateService (ActivityThread $ CreateServiceData): 2561 - ActivityThread .access $ 1600 (Dòng ActivityThread, ActivityThread $ CreateServiceData): 141 – samo

5

Tạo bộ thu phát sóng, nhận tin nhắn từ thông báo và sau đó khởi động dịch vụ.

+0

Cảm ơn tipp với BroadCastReceiver. Có thể đặt tất cả các kiểm tra vào đó vì vậy tôi không cần Dịch vụ nữa. – 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

Thêm giải thích quá –

+0

chỉ cần vượt qua chuỗi tin nhắn của bạn trong phương thức gọi createNotification này ("test"); và .setContentTitle ("Start Launcher") là tiêu đề của thông báo của bạn Yourservice.class là tên dịch vụ của dịch vụ được tạo của bạn. –

Các vấn đề liên quan