服务运行时如何更改状态栏图标

僧侣

我想smallIcon在前台服务运行时更改状态栏中的通知,具体取决于该服务收集的状态,即“静音”或“取消静音”。

smallIconsres.drawable资源显示替代内容需要什么

在服务类的initialize方法中,我目前将静音图标设置如下,但是在启动服务后我不知道如何更改它:

NotificationCompat.Builder builder = new NotificationCompat.Builder(
        this, NOTE_CHANNEL_ID)
        .setSmallIcon(R.drawable.mute_icon)
        .setContentTitle("Calm: Running")
        .setContentText(this.getString(R.string.calm_close_txt))
        .setOngoing(true)
        .setContentIntent(stopIntent);

startForeground(NOTIFICATION_ID, builder.build());
MDP

您只需要保存对的引用NotificationCompat.Builder然后使用的notify(int id, Notification notification)方法NotificationManagerCompat

例:

NotificationCompat.Builder notificationBuilder;

private void startNotif(){
    notificationBuilder = new NotificationCompat.Builder(
        this, NOTE_CHANNEL_ID)
        .setSmallIcon(R.drawable.mute_icon)
        .setContentTitle("Calm: Running")
        .setContentText(this.getString(R.string.calm_close_txt))
        .setOngoing(true)
        .setContentIntent(stopIntent);
    
    startForeground(NOTIFICATION_ID, notificationBuilder.build());
}

private void changeIcon(int resID, Context context){
    notificationBuilder.setSmallIcon(resID);
    NotificationManagerCompat notificationManagerCompat =
                NotificationManagerCompat.from(context);
    notificationManagerCompat.notify(NOTIFICATION_ID, notificationBuilder.build());
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何更改React Native推送通知状态栏图标?

如何在运行时更改工具栏中的动作图标?

更改状态栏图标的色彩

如何更改状态栏

如何将android状态栏颜色更改为白色并将状态栏图标颜色更改为灰色

运行时隐藏/显示状态栏 iOS 9+

如何在#flutter中更改用于推送通知的Android通知图标/状态栏图标?

如何在运行时更改Safari应用程序扩展中的工具栏图标?

如何更改状态栏颜色?

如何更改状态栏的背景颜色?

如何更改状态栏背景的颜色?

如何更改VS Code状态栏中的> <远程图标的颜色?

如何根据设置的主题更改flutter中的状态栏图标和文本颜色?

如何在优胜美地中检测黑暗模式以更改状态栏菜单图标

如何在Android中更改状态栏图标的颜色?

动态更改IntelliJ IDEA插件中的状态栏图标

wxPython在运行时Windows期间更改任务栏图标

如何在运行时更改Web服务地址?

在运行时从服务器更改应用程序图标

如何修复Android状态栏中的图标

如何删除状态栏并在视频上重叠图标?

如何为Android创建状态栏图标

如何使用Cordova在系统状态栏上放置图标

如何使状态栏白色背景与黑色图标?

如何在 Flutter 中设置状态栏图标颜色?

如何自定义状态栏图标和文字颜色?例如状态栏背景:白色,状态栏图标颜色,文字:红色

服务和状态栏

更改状态栏Alpha

更改状态栏的颜色