ปิดแถบสถานะเมื่อมีการคลิกปุ่มแจ้งเตือน

ฉันจะปิดแถบสถานะหลังจากคลิกปุ่มแจ้งเตือนได้อย่างไร

ฉันลองสิ่งนี้ แต่มีข้อยกเว้น:

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

รหัสของฉัน:

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());

ที่คลาส 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));
}

person Poliane Brito    schedule 17.10.2013    source แหล่งที่มา


คำตอบ (2)


วิธีแก้ปัญหาต่อไปนี้ควรง่ายกว่านี้และไม่ใช้ API ที่ไม่ใช่แบบสาธารณะ:

Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
context.sendBroadcast(it); 
person Sam Lu    schedule 06.12.2013
comment
คุณบอกได้ไหมว่าจะใช้สิ่งนี้ที่ไหนใน onRecieve หรือ action intent ? - person Ajeet; 17.06.2015
comment
ทำงานเหมือนมีเสน่ห์! ขอบคุณ. เพียงใส่โค้ด 2 บรรทัดใน onRecieve - person franco phong; 29.07.2015

ตกลง ฉันแก้ไขมันแล้ว

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();
}
person Poliane Brito    schedule 17.10.2013
comment
คุณควรยอมรับความคิดเห็นของ Sam Lu จริงๆ โซลูชันของคุณอาจหยุดทำงานในเวอร์ชันถัดไปเนื่องจากใช้ API ที่ไม่ใช่แบบสาธารณะ อย่างที่คุณเห็นคุณต้องเพิ่ม if api<=16 แล้ว - เป็นไปได้ว่าเร็วๆ นี้คุณจะต้องเพิ่ม if api<=19 - person imbryk; 17.04.2014
comment
เห็นด้วยกับความเห็นข้างบนครับ คำตอบของ Sam นั้นง่ายกว่ามากและไม่เกี่ยวข้องกับหมายเลขเวอร์ชัน - person Saik; 18.11.2017