Android 使用通知(最基础的用法)

本文详细介绍了在安卓应用中如何通过编写代码实现点击按钮后在通知栏显示通知的过程。包括创建NotificationChannel,使用NotificationManager,设置通知内容和图标,以及处理点击跳转事件。

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

点击按钮,通知栏出现通知标志
在这里插入图片描述

代码如下:

package com.example.notificationtest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button sendNotice=(Button)findViewById(R.id.send_notice);
        sendNotice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,NotificationActivity.class);
                PendingIntent pi=PendingIntent.getActivity(getApplicationContext(),0,intent,0);

                NotificationChannel channel = new NotificationChannel("1",
                        "Channel1", NotificationManager.IMPORTANCE_DEFAULT);
                NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                manager.createNotificationChannel(channel);
                Notification notification=new NotificationCompat.Builder(getApplicationContext(),"1")
                        .setContentTitle("This is contenttitle")
                        .setContentText("This is content text")
                        .setWhen(System.currentTimeMillis())
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                        .setContentIntent(pi).build();
                manager.notify(1,notification);
            }
        });
    }
}

1、首先创建一个NotificationChannel实例

 
NotificationChannel channel = new NotificationChannel("1",
                        "Channel1", NotificationManager.IMPORTANCE_DEFAULT);
//                        * IMPORTANCE_NONE 关闭通知
//                        * IMPORTANCE_MIN 开启通知,不会弹出,但没有提示音,状态栏中无显示 
//                        * IMPORTANCE_LOW 开启通知,不会弹出,不发出提示音,状态栏中显示
//                        * IMPORTANCE_DEFAULT 开启通知,不会弹出,发出提示音,状态栏中显示
//                        * IMPORTANCE_HIGH 开启通知,会弹出,发出提示音,状态栏中显示

前两个参数分别代表:channel的id,channel的名字

2、然后创建一个NotificationManager实例,并调用createNotificationChannel把channel传给这个实例

NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);

3、创建一个Notification实例,然后设置通知的大小图标、文本、点击跳转事件等等,最后用manager调用notify()方法,传入两个参数:通知的id和通知。通知的id只要是唯一的就可以

Notification notification=new NotificationCompat.Builder(getApplicationContext(),"1")
                        .setContentTitle("This is contenttitle")
                        .setContentText("This is content text")
                        .setWhen(System.currentTimeMillis())
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                        .setContentIntent(pi).build();
                manager.notify(2,notification);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值