2013-10-17 23 views
5

Làm cách nào để đóng thanh trạng thái sau khi nhấp vào nút thông báo?Đóng thanh trạng thái khi thông báo nút được nhấp

tôi đã cố gắng this, nhưng tôi có một ngoại lệ:

java.lang.NoSuchMethodException: collapse [] 
    at java.lang.Class.getConstructorOrMethod(Class.java:460) 
    at java.lang.Class.getMethod(Class.java:915) 
    ... 

Mã của tôi:

NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
    .setSmallIcon(R.drawable.icon) 
    .setContentTitle("Sync Failed") 
    .setContentText("Lorem ipsum dolor sit amet") 
    .setStyle(new NotificationCompat.BigTextStyle().bigText("Lorem ipsum dolor sit amet")) 
    .addAction(R.drawable.change, "Change Pass", pChangePass) 
    .addAction(R.drawable.remove, "Ignore", pIgnore) 
    .setAutoCancel(false); 
mNotificationManager.notify(accountUnique, builder.build()); 

Tại lớp NotificationIntent

@Override 
public void onReceive(Context context, Intent intent) { 
    int notificationID = intent.getExtras().getInt("NOT_ID"); 
    this.callbackContext = StatusBarNotification.getCallback(); 
    this.mNotificationManager = StatusBarNotification.getNotificationManager(); 

    this.mNotificationManager.cancel(notificationID); 
    this.callbackContext.success(returnJSON(intent)); 
} 

Trả lời

0

Ok, tôi giải quyết nó.

private int currentApiVersion = android.os.Build.VERSION.SDK_INT; 
... 

Object sbservice = context.getSystemService("statusbar"); 
try { 
    Class<?> statusbarManager = Class.forName("android.app.StatusBarManager"); 
    if (currentApiVersion <= 16) { 
     Method collapse = statusbarManager.getMethod("collapse"); 
     collapse.invoke(sbservice); 
    } else { 
     Method collapse2 = statusbarManager.getMethod("collapsePanels"); 
     collapse2.invoke(sbservice); 
    } 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
+4

Bạn thực sự nên chấp nhận bình luận Sam Lu - giải pháp của bạn có thể ngừng làm việc trên bất kỳ phiên bản tiếp theo, bởi vì nó sử dụng không api công khai. Như bạn thấy bạn alredy đã phải thêm 'if api <= 16' - có thể bạn sẽ sớm cần thêm' if api <= 19' – imbryk

+0

Đồng ý với nhận xét ở trên. Câu trả lời của Sam đơn giản hơn nhiều và không xử lý các số phiên bản. – Saik

30

Các giải pháp sau đây cần được đơn giản hơn và không có API ngoài công lập sử dụng:

Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); 
context.sendBroadcast(it); 
+1

Bạn có thể cho biết nơi để sử dụng điều này, trong onRecieve hoặc mục đích hành động? – Ajeet

+0

Làm việc như sự quyến rũ! Cảm ơn. Chỉ cần đặt 2 dòng mã trong onRecieve. –

+0

Vâng, điều này đã hiệu quả. Cảm ơn. – Saik

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