通知栏(Notification)的基本使用

本文介绍了Android系统的通知功能,包括基本使用方法及如何构建丰富多样的通知。通过代码示例展示了如何创建可点击的通知,并设置了通知的标题、内容、时间、图标等属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通知是Android系统中比较有特色的一个功能,当某个应用程序希望向用户发出一些提示信息,而应用程序又不在前台运行时,可以通过通知来实现。发送一个通知,手机的最上方会出现通知的图标,下拉状态栏的时候,可以看到通知的详细内容。


一.通知的基本使用:

(1).先获取通知的管理者对象,通过下面的方式获取

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

(2).开启通知前,需要一个Notification的对象,由于版本的兼容,采用V4包下的NotificationCompat类来获取

Notification notification = new NotificationCompat.Builder(this).build();

(3).发送一个通知(一个id对应一个通知)

manager.notify(1,notification);

二.构建一个丰富多样的通知显示代码:


布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_send"
        android:text="发送一条通知"
        android:textColor="#ff0000"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>


页面逻辑实现:

public class MainActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendNotification();
            }
        });
    }

    private void sendNotification() {
        //1.获取通知管理者对象
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        //如果需要通知可点击,需要使用PendingIntent(延迟意图)
        Intent intent = new Intent(MainActivity.this,NotificationActivity.class);
        //参数:(上下文,请求码(通常填0,不常用),意图对象,行为(通常填0))
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        //2.获取一个notification对象实例
        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("银行到账")//设置标题
                .setContentText("工商银行为你转入10000000000,你卡上余额为10000003460。")//设置内容
                .setWhen(System.currentTimeMillis())//设置通知的时间
                .setSmallIcon(R.drawable.zhifu_icon_payply)//设置小图标
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.zhifu_icon_payply))//设置大图标
                .setContentIntent(pendingIntent)//延迟处理的意图,当点击通知时
                .setAutoCancel(true)//设置当用户点击面板时通知自动取消
                //.setSound(Uri.fromFile(new File(".....")))//设置声音播放
                //.setLights(Color.GREEN,1000,1000)//设置LED闪烁灯
                .setDefaults(NotificationCompat.DEFAULT_ALL)//使用默认的通知选项
                .setPriority(NotificationCompat.PRIORITY_MAX)//设置通知的优先级
                .build();
        //3.开启通知
        manager.notify(1,notification);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值