【问题标题】:How to close the status bar/notification panel after notification button click单击通知按钮后如何关闭状态栏/通知面板
【发布时间】:2013-03-12 05:10:53
【问题描述】:

我浏览过 Stackoverflow,看到有人问过这个问题,但我没有找到任何解决方案。

我有一个带有 2 个按钮的自定义通知。我希望在按下该通知上的按钮后关闭状态栏面板,但不知道如何操作。如果我按下通知本身(即 contentIntent),则面板会关闭,这也是我想要的按钮。

这是我的通知代码:

Notification notification = new Notification(R.drawable.icon, "Service running", System.currentTimeMillis());
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.contentIntent = openIntent;
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_layout);


contentView.setOnClickPendingIntent(R.id.button1, stopIntent);
contentView.setOnClickPendingIntent(R.id.button2, addIntent);

notification.contentView = contentView;

notificationManager.notify(NOTIFICATION_ID, notification);

【问题讨论】:

  • stackoverflow.com/a/6435373/115145 从 API 级别 11 开始支持按钮。您在对我现在已删除的答案的评论中指出这是您的 minSdkVersion,因此您应该没问题,尽管我没有想法如何关闭抽屉。我为我的错误道歉——我忘记了他们正式开始支持这一点。
  • Np,也许我应该提前提到我的 minSdkVersion = 11 ;-)

标签: android notifications statusbar


【解决方案1】:

使用以下两行代码折叠通知栏。

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

【讨论】:

  • 这是正确答案,因为此代码无需私有 API 即可工作。
  • 你节省了我的时间。谢谢。
  • 正确的最佳答案。
  • 没有比这更容易的,绝对是正确的做事方式
【解决方案2】:

好的,我找到了解决方案。它不是公共 API,但它可以工作(目前):

Object sbservice = c.getSystemService( "statusbar" );
                    Class<?> statusbarManager = Class.forName( "android.app.StatusBarManager" );
                    Method hidesb = statusbarManager.getMethod( "collapse" );
                    hidesb.invoke( sbservice );

为了这个工作

<uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/>

需要

【讨论】:

  • @iDroidExplorer:c 是上下文。而且,正如我的问题所问的那样,此代码能够关闭扩展的通知栏面板。我按下自定义通知的按钮后使用那段代码
【解决方案3】:

适用于 Android 11 及更低版本:

提供权限
&lt;uses-permission android:name="android.permission.EXPAND_STATUS_BAR"/&gt;

然后:

Object statusBarService = context.getSystemService("statusbar");
Class<?> statusBarManager = Class.forName("android.app.StatusBarManager");
Method collapse = statusBarManager.getMethod("collapse");
collapse.invoke( statusBarService );

对于 Android 12 及更高版本:

您的应用应该是系统应用 然后使用上面使用的相同权限,该方法将起作用

对于第三方应用.. 此工作不可用

/**
 * Collapse the notifications and settings panels.
 *
 * Starting in Android {@link Build.VERSION_CODES.S}, apps targeting SDK level {@link
 * Build.VERSION_CODES.S} or higher will need {@link android.Manifest.permission.STATUS_BAR}
 * permission to call this API.
 *
 * @hide
 */
@RequiresPermission(android.Manifest.permission.STATUS_BAR)
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, publicAlternatives = "This operation"
        + " is not allowed anymore, please see {@link android.content"
        + ".Intent#ACTION_CLOSE_SYSTEM_DIALOGS} for more details.")
@TestApi
public void collapsePanels() {
  ...
}

并使用 Intent.ACTION_CLOSE_SYSTEM_DIALOGS

参考this

【讨论】:

    猜你喜欢
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多