Android之Notification通知详解

实现点击按钮发送通知功能:

一、布局文件:

<?xml version="1.0" encoding="utf-8"?>
    <!--这里的LinearLayout我们可以当作是一个存放控件的容器,后面会学习它的使用-->
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
            android:layout_width="400dp"
            android:layout_height="200dp"
            android:text="发送通知"
            android:textSize="50sp"
            android:onClick="myclick1"/>
        <Button
            android:layout_width="400dp"
            android:layout_height="200dp"
            android:text="取消通知"
            android:textSize="50sp"
            android:onClick="myclick2"/>
    </LinearLayout>

二、Activity.java文件:

package com.example.t.androidproject.myapplication2;

import android.app.Activity;
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.Build;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

public class LoginActivity extends Activity {

    NotificationManager manager;
    Notification notification;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.avtivity_login);

        //连接原Activity和跳转Activity
        Intent intent = new Intent(this, NotiFicationActicity.class);//this是本Activity,NotiFicationActicity是跳转Activity
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        //第一步:创建通知管理器 NotificationManager
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        //第二步:使用NotificationCompat的Builder构造器的build()方法来创建Notification通知对象
            //Builder(Context context,String channelId)
                //context:上下文
                //channelId:通知渠道id
        notification = new NotificationCompat.Builder(this,"channel1")
                //链式结构,设置notification属性
                .setContentTitle("官方通知")//设置通知标题
                .setContentText("世界那么大,我想去看看")//设置通知文本内容
                .setSmallIcon(R.drawable.ic_baseline_airport_shuttle_24)//设置通知小图标,图标不能有颜色,不能是RGB图片
                .setColor(Color.parseColor("#FF0000"))//设置小图标颜色
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                        R.drawable.image1))//设置大图标,所以可以有颜色,但是要通过BitmapFactory转换成Bitmap类型
                .setContentIntent(pendingIntent)//设置通知后的跳转意图,需要一个PendingIntent对象
                .setAutoCancel(true)//点击通知后是否自动清除通知
                .build();

        //创建通知渠道,注意通知渠道是8.0后引入的,所以必须判断版本是否为8.0以上
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            //NotificationChannel(String id, CharSequence name, int importance)
                //id:通知渠道id
                //name:用户可见的通知名称
                //importance:通知的重要程度
                    //IMPORTANCE_NONE 关闭通知
                    //IMPORTANCE_MIN 开启通知,不会弹出,无提示音,状态栏中不显示
                    //IMPORTANCE_LOW 开启通知,不会弹出,无提示音,状态栏中显示
                    //IMPORTANCE_DEFAULT 开启通知,不会弹出,发出提示音,状态栏中显示
                    //IMPORTANCE_HIGH 开启通知,会弹出,发出提示音,状态栏中显示
            NotificationChannel notificationChannel = new NotificationChannel("channel1", "测试通知", NotificationManager.IMPORTANCE_HIGH);
            //通知渠道加载到通知管理器
            manager.createNotificationChannel(notificationChannel);
        }
    }

    public void myclick1(View view){
        //通过通知管理器对象manager发送通知notification,通知的id是1
        manager.notify(1,notification);
    }

    public void myclick2(View view){
        //关闭id为1的通知
        manager.cancel(1);
    }
}

三、运行结果:

页面布局:
请添加图片描述

点击发送通知按钮后:
请添加图片描述
请添加图片描述
点击通知后通知消失,跳转到对应页面。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

姓蔡小朋友

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值