• android的通知使用


    在 Android 中,通知(Notification)是一种在状态栏显示消息的方式,通常用于向用户展示应用程序的重要信息、事件或更新。以下是一个简单的示例,演示如何在 Android 应用程序中使用通知:

    import android.app.Notification;
    import android.app.NotificationChannel;
    import android.app.NotificationManager;
    import android.content.Context;
    import android.os.Build;
    import android.os.Bundle;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.core.app.NotificationCompat;
    
    public class MainActivity extends AppCompatActivity {
    
        private static final String CHANNEL_ID = "my_channel_01";
        private static final int NOTIFICATION_ID = 1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // 创建通知通道(适用于 Android 8.0 及以上版本)
            createNotificationChannel();
    
            // 创建并显示通知
            showNotification();
        }
    
        private void createNotificationChannel() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                CharSequence name = "My Channel";
                String description = "Channel description";
                int importance = NotificationManager.IMPORTANCE_DEFAULT;
    
                NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
                channel.setDescription(description);
    
                NotificationManager notificationManager = getSystemService(NotificationManager.class);
                notificationManager.createNotificationChannel(channel);
            }
        }
    
        private void showNotification() {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setSmallIcon(R.drawable.ic_notification)
                    .setContentTitle("My Notification")
                    .setContentText("This is a notification from my app.")
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT);
    
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.notify(NOTIFICATION_ID, builder.build());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    在上述示例中:

    • createNotificationChannel 方法用于创建通知通道,这是 Android 8.0 及以上版本引入的概念。通知通道允许用户对不同类型的通知进行分组和管理。
    • showNotification 方法创建并显示通知。这里使用了 NotificationCompat.Builder 类,它提供了向后兼容性,允许在不同 Android 版本上使用相同的代码。

    请注意:

    • 在 Android 8.0 及以上版本,必须创建通知通道,否则通知将无法显示。
    • 为了显示通知,你需要使用 NotificationManagernotify 方法。通知的唯一标识符是一个整数,用于标识应用程序中的不同通知。在这里,使用了 NOTIFICATION_ID 常量。

    此外,你可以根据需求定制通知的样式、行为、点击事件等。通知是 Android 应用中一种重要的用户交互方式,可以根据应用的需求进行更复杂的定制。

  • 相关阅读:
    【科普】电脑屏幕刷新率:了解和选择需要的刷新率
    C++ 矩阵乘法
    boost Geometry
    多款可观测产品全面升级丨阿里云云原生 5 月产品月报
    文字转语音:语音合成(Speech Synthesis) 数组文字循环播放
    思维模型 留白效应
    java计算机毕业设计食用菌菌棒溯源系统的开发与设计源程序+mysql+系统+lw文档+远程调试
    双目视觉(双目相机)
    华为配置蓝牙终端定位实验
    CEC2018:动态多目标测试函数DF1~DF5的PS及PF
  • 原文地址:https://blog.csdn.net/duftgiiok/article/details/134450218