单击按钮通知时关闭状态栏

时间:2013-10-17 18:04:54

标签: android cordova notifications statusbar

如何在点击通知按钮后关闭状态栏?

我试过了this,但我有一个例外:

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

2 个答案:

答案 0 :(得分:33)

以下解决方案应该更简单,并且不使用非公共API:

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

答案 1 :(得分:1)

好的,我解决了。

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