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);
Nguồn
2011-12-05 14:29:06
Đ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
@ 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ụ. –
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