Android sutdio 开发学习笔记——第五篇:Notification 通知

Notfication

Android Studio 中的通知(Notification)是一种用户界面元素,用于向用户显示关于应用程序当前状态或事件的信息。通知可以显示在设备的状态栏中,并且可以展开以显示更多详细信息。通知通常用于向用户显示新消息、更新或其他重要事件。

以下是使用通知的基本步骤:

  1. 创建 NotificationCompat.Builder 对象,并设置通知的标题、内容等属性。
  2. 通过设置 Intent 或 PendingIntent 来定义通知的点击行为。
  3. 将通知发送到通知管理器(NotificationManager)以显示通知。

下面是一个简单的示例,演示如何在 Android Studio 中使用通知:

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import androidx.core.app.NotificationCompat;

public class NotificationHelper {
    private static final String CHANNEL_ID = "example_channel";
    private static final CharSequence CHANNEL_NAME = "Example Channel";
    private static final String CHANNEL_DESCRIPTION = "This is an example notification channel";

    public static void showNotification(Context context, String title, String content) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_notification)
                .setContentTitle(title)
                .setContentText(content)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);

        // Define the intent to open when notification is clicked
        // PendingIntent pendingIntent = ...

        // Set the intent to the notification
        // builder.setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        // Create a notification channel for Android Oreo and higher
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            channel.setDescription(CHANNEL_DESCRIPTION);
            notificationManager.createNotificationChannel(channel);
        }

        // Display the notification
        notificationManager.notify(/*notification_id*/ 1, builder.build());
    }
}

在这个示例中,我们创建了一个名为 showNotification 的静态方法,该方法接受上下文、通知的标题和内容作为参数。然后,我们创建了一个 NotificationCompat.Builder 对象,并设置了通知的小图标、标题和内容。你可以根据需要设置其他属性,如点击通知时的操作。

最后,我们通过 NotificationManager 将通知显示出来,并在需要时创建了一个通知渠道(Notification Channel),这是为了兼容 Android Oreo 及更高版本。

要使用这个示例,你需要在调用 showNotification 方法之前创建一个 PendingIntent,并将其设置为通知的点击行为。

我自己写的通知,明明是照着教程写的,但是却没办法使用,报错

    private NotificationManager manager;
    private Notification notification;
        
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//        当前手机安卓系统版本大于8的时候
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel channel = new NotificationChannel("leo", "报错",
                    NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);
        }else {
            notification = new NotificationCompat.Builder(this,"leo")
                    .setContentTitle("官方通知")
                    .setContentText("您的输入不合规")
                    .setSmallIcon(R.drawable.ic_launcher_background)
                    .build();
        }

使用这个通知

 manager.notify(1,notification);

Toolbar

**Android Studio 中的 Toolbar 是用来创建应用程序的顶部操作栏或工具栏的。**它通常位于屏幕的顶部,并包含应用程序的主要操作按钮、导航按钮和其他重要控件。Toolbar 提供了一种简洁而强大的方式来组织应用程序的用户界面,使用户可以轻松地访问应用程序的各种功能。

一些 Toolbar 的主要功能包括:

  1. 导航按钮:通常位于 Toolbar 的左侧,用于返回上一个屏幕或打开导航菜单。
  2. 应用程序标题:通常位于 Toolbar 的中心,显示当前屏幕的标题或应用程序的名称。
  3. 操作按钮:位于 Toolbar 的右侧或左侧,用于执行与当前屏幕相关的操作,如搜索、分享、设置等。
  4. 溢出菜单:当屏幕空间有限时,Toolbar 可以自动将一些操作按钮隐藏在溢出菜单中,用户可以通过点击溢出菜单按钮来访问这些隐藏的操作。

举例:

假设我们正在开发一个新闻阅读应用程序。在这个应用程序中,Toolbar 可以具有以下功能:

  • 导航按钮:返回按钮,允许用户从新闻详情页面返回到新闻列表页面。
  • 应用程序标题:显示当前正在阅读的新闻标题,让用户清楚地知道自己在浏览哪篇新闻。
  • 操作按钮:分享按钮,允许用户将当前正在阅读的新闻内容分享给其他人。
  • 溢出菜单:包含一些额外的操作,如设置、反馈、帮助等,以便用户可以轻松地访问这些功能。

通过 Toolbar,用户可以方便地浏览新闻、返回上一个页面、分享新闻内容,并且可以访问一些额外的功能,提升了应用程序的易用性和功能性。

  • 21
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值