2013-02-03 38 views
66

Tôi đang sử dụng thông báo android để cảnh báo cho người dùng khi dịch vụ kết thúc (thành công hay thất bại) và tôi muốn xóa tệp cục bộ sau khi quá trình được thực hiện.bắt trên swipe để loại bỏ sự kiện

Vấn đề của tôi là trong trường hợp không thành công - tôi muốn cho người dùng tùy chọn "thử lại". và nếu anh ta chọn không thử lại và loại bỏ thông báo tôi muốn xóa các tệp cục bộ được lưu cho mục đích xử lý (hình ảnh ...).

Có cách nào để nắm bắt sự kiện vuốt-để-loại bỏ thông báo không?

Trả lời

104

DeleteIntent: DeleteIntent là một đối tượng PendingIntent có thể được liên kết với một thông báo và bị sa thải khi thông báo bị xóa, ether bởi: hành động

  • dùng cụ thể
  • tài Xóa tất cả các thông báo .

Bạn có thể đặt Mục đích đang chờ xử lý thành Người phát sóng và sau đó thực hiện bất kỳ hành động nào bạn muốn.

Intent intent = new Intent(this, MyBroadcastReceiver.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0); 
    Builder builder = new Notification.Builder(this): 
..... code for your notification 
    builder.setDeleteIntent(pendingIntent); 

MyBroadcastReceiver

public class MyBroadcastReceiver extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      .... code to handle cancel 
     } 

    } 
+0

Hoạt động :) thankyou rất nhiều! –

+5

Điều này là muộn. Tôi chỉ tự hỏi nếu có một cách tiếp cận tương tự cho các thông báo có 'builder.setAutoCancel (true);' bởi vì khi người dùng nhấp vào thông báo và nó bị hủy, xóa-Intent không được kích hoạt –

+1

@dev_android checkout http://developer.android.com/reference/android/app/Notification.Builder.html#setContentIntent(android.app.PendingIntent) –

66

Một hoàn toàn xả ra ngoai câu trả lời (với nhờ ông nhớ cho câu trả lời):

1) Tạo một máy thu để xử lý các swipe-to- loại bỏ sự kiện:

public class NotificationDismissedReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     int notificationId = intent.getExtras().getInt("com.my.app.notificationId"); 
     /* Your code to handle the event here */ 
    } 
} 

2) Thêm mục nhập cho bạn r manifest:

<receiver 
    android:name="com.my.app.receiver.NotificationDismissedReceiver" 
    android:exported="false" > 
</receiver> 

3) Tạo mục đích cấp phát sử dụng một id duy nhất cho mục đích cấp phát (id thông báo được sử dụng ở đây) như không có điều này các tính năng bổ sung tương tự sẽ được tái sử dụng cho mỗi sự kiện sa thải:

private PendingIntent createOnDismissedIntent(Context context, int notificationId) { 
    Intent intent = new Intent(context, NotificationDismissedReceiver.class); 
    intent.putExtra("com.my.app.notificationId", notificationId); 

    PendingIntent pendingIntent = 
      PendingIntent.getBroadcast(context.getApplicationContext(), 
             notificationId, intent, 0); 
    return pendingIntent; 
} 

4) Xây dựng thông báo của bạn:

Notification notification = new NotificationCompat.Builder(context) 
       .setContentTitle("My App") 
       .setContentText("hello world") 
       .setWhen(notificationTime) 
       .setDeleteIntent(createOnDismissedIntent(context, notificationId)) 
       .build(); 

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
notificationManager.notify(notificationId, notification); 
+0

Anh chàng tuyệt vời! –

+0

Đã không làm việc cho tôi, luôn luôn dẫn đến lỗi "Không thể khởi tạo bộ nhận .... không có hàm tạo đối số 0". Chỉ được giải quyết sau khi tôi đã triển khai một giải pháp tương tự khác nhưng với đăng ký bộ thu phát sóng: http://stackoverflow.com/questions/13028122/how-to-use-delete-intent-to-perform-some-action-on -clear-notification –

+0

Điều này làm việc cho tôi. Nhưng sự kiện không thể được gọi khi bạn nhấp vào thông báo. Làm cách nào tôi có thể nghe sự kiện nhấp chuột? –

0

Một Idea:

i f bạn tạo một thông báo bình thường, bạn cũng cần các hành động một, hai hoặc 3 trong số chúng. Tôi đã tạo ra một "NotifyManager" nó tạo ra tất cả các thông báo tôi cần và cũng nhận được tất cả các cuộc gọi Intent. Vì vậy, tôi có thể quản lý tất cả các hành động VÀ cũng bắt sự kiện sa thải tại MỘT địa điểm.

public class NotifyPerformService extends IntentService { 

@Inject NotificationManager notificationManager; 

public NotifyPerformService() { 
    super("NotifyService"); 
    ...//some Dagger stuff 
} 

@Override 
public void onHandleIntent(Intent intent) { 
    notificationManager.performNotifyCall(intent); 
} 

để tạo ra các deleteIntent sử dụng này (trong NotificationManager):

private PendingIntent createOnDismissedIntent(Context context) { 
    Intent   intent   = new Intent(context, NotifyPerformMailService.class).setAction("ACTION_NOTIFY_DELETED"); 
    PendingIntent pendingIntent = PendingIntent.getService(context, SOME_NOTIFY_DELETED_ID, intent, 0); 

    return pendingIntent; 
} 

và mà tôi sử dụng để thiết lập tiếp cận mục đích xóa như thế này (trong NotificationManager):

private NotificationCompat.Builder setNotificationStandardValues(Context context, long when){ 
    String       subText = "some string"; 
    NotificationCompat.Builder  builder = new NotificationCompat.Builder(context.getApplicationContext()); 


    builder 
      .setLights(ContextUtils.getResourceColor(R.color.primary) , 1800, 3500) //Set the argb value that you would like the LED on the device to blink, as well as the rate 
      .setAutoCancel(true)             //Setting this flag will make it so the notification is automatically canceled when the user clicks it in the panel. 
      .setWhen(when)               //Set the time that the event occurred. Notifications in the panel are sorted by this time. 
      .setVibrate(new long[]{1000, 1000})          //Set the vibration pattern to use. 

      .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher)) 
      .setSmallIcon(R.drawable.ic_white_24dp) 
      .setGroup(NOTIFY_GROUP) 
      .setContentInfo(subText) 
      .setDeleteIntent(createOnDismissedIntent(context)) 
    ; 

    return builder; 
} 

và cuối cùng trong cùng một NotificationManager là chức năng thực hiện:

public void performNotifyCall(Intent intent) { 
    String action = intent.getAction(); 
    boolean success = false; 

    if(action.equals(ACTION_DELETE)) { 
     success = delete(...); 
    } 

    if(action.equals(ACTION_SHOW)) { 
     success = showDetails(...); 
    } 

    if(action.equals("ACTION_NOTIFY_DELETED")) { 
     success = true; 
    } 


    if(success == false){ 
     return; 
    } 

    //some cleaning stuff 
} 
Các vấn đề liên quan