Một thay thế cho các deleteIntent
là sau đó đã chứng minh có lợi trong ứng dụng của riêng tôi:
Về cơ bản, bạn tạo ý định với thông báo bắt đầu IntentService (hoặc bất kỳ dịch vụ nào khác) và trong onHandleIntent
bạn có thể đặt một lá cờ cho biết liệu thông báo có đang hoạt động hay không.
Bạn có thể đặt mục đích này được kích hoạt khi người dùng nhấn vào thông báo (contentIntent) và/hoặc khi người dùng xóa nó khỏi danh sách (deleteIntent).
Để minh họa, đây là những gì tôi làm trong ứng dụng của riêng tôi. Khi xây dựng thông báo tôi đặt
Intent intent = new Intent(this, CleanupIntentService.class);
Notification n = NotificationCompat.Builder(context).setContentIntent(
PendingIntent.getActivity(this, 0, intent, 0)).build();
khi thông báo được khai thác, CleanupIntentService
của tôi được đưa ra, thiết lập một lá cờ (trong dịch vụ đã tạo ra thông báo):
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onCreate(); // If removed, onHandleIntent is not called
return super.onStartCommand(intent, flags, startId);
}
@Override
protected void onHandleIntent(Intent intent) {
OtherService.setNotificationFlag(false);
}
phải có điều gì đó sai. khi tôi viết trong phương thức '' 'onResume()' '' '' 'if (isNotificationVisible())' '' thông báo của tôi nên bị hủy bỏ với: '' '((NotificationManager) getSystemService (NOTIFICATION_SERVICE)). MY_ID); '' 'và sau đó' '' kết thúc(); startActivity (getIntent()); '' 'Tôi nhận được một vòng lặp dẫn đến khởi động lại điện thoại của tôi. vì vậy phương pháp này nên được xử lý cẩn thận .... – maysi
Không làm việc cho tôi. Điều này luôn luôn trở lại đúng với tôi. – Saket
Đã không hoạt động đối với tôi trong Android 4.3. 'PendingIntent' bị kẹt lại sau khi tôi loại bỏ thông báo, và thậm chí sau khi tôi đã giết toàn bộ ứng dụng. Chỉ khởi động lại điện thoại đã loại bỏ nó. Ngoài ra, 'setLatestEventInfo()' không còn tồn tại, vì vậy tôi chỉ sử dụng 'setContentIntent()' để thay thế. – Sam