【问题标题】:Android: Status Bar NotificationsAndroid:状态栏通知
【发布时间】:2026-01-05 13:15:01
【问题描述】:

有什么方法可以捕捉/检测状态栏上系统通知的开始或实例?我打算将状态栏上显示的通知重定向到简单的 toast 或类似的东西。

【问题讨论】:

    标签: android notifications statusbar


    【解决方案1】:

    AFAIK,没有办法做到这一点。

    编辑:添加示例来处理 sdcard 事件

    IntentFilter f = new IntentFilter();
    f.addAction(Intent.ACTION_MEDIA_EJECT);
    f.addAction(Intent.ACTION_MEDIA_MOUNTED);
    f.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
    f.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
    f.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
    f.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
    f.addDataScheme("file");
    registerReceiver(mScanListener, f);
    
    

    private BroadcastReceiver mScanListener = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { ... } }

    【讨论】:

    • 具体通知怎么样?比方说安装和卸载 SD 卡?当从设备上安装/卸载 SD 卡时,我想创建一个简单的 toast。
    • SD卡相关通知,可以监听ACTION_MEDIA_MOUNTED/UNMOUNTED通知。
    • 我实际上正在尝试解决这个问题。使用 BroadcastReceiver 对吗?
    • 我已在答案中添加了示例。
    • 我明白了。我想我的第一直觉是正确的。 :) 非常感谢您的帮助!