安卓基础学习 Day 13|Notification显示通知

目录

1.了解Notification 

2.提出要求

3.编写主布局资源文件

4.编写主界面代码

5.创建跳转界面

6.最终效果


1.了解Notification 

2.提出要求

点击发出通知触发发送通知事件

点击取消通知触发取消通知事件

点击通知进入通知界面,退出后通知关闭

3.编写主布局资源文件

实现两个按钮

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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="sendNotification"
        android:text="发出通知">
    </Button>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="cacelNotification"
        android:text="取消通知">
    </Button>


</LinearLayout>

效果显示

4.编写主界面代码

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.Build;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    private NotificationManager manager;
    private Notification notification;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //这里的id需要与下面的channelId:保持一致   name可以随便这就相当于弹出时的提示
            NotificationChannel channel = new NotificationChannel("lu", "测试通知", NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);
        }

        //创建意图
        Intent intent = new Intent(this, NotificationActivity.class);
        //创建PendingIntent
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);


        notification = new NotificationCompat.Builder(this, "lu")
                .setContentTitle("官方通知")  // 必须要
                .setContentText("梧高凤必至花香蝶自来")//内容
                .setSmallIcon(R.drawable.ic_launcher_foreground) // 必须要不带颜色的图片
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.cherry))//设置大图标
                .setColor(Color.parseColor("#ff000000")) //设置小图标颜色
                .setContentIntent(pendingIntent) // 设置点击通知后自动清除通知
                .setAutoCancel(true)  //点击后自动取消
                .build();
    }

    public void sendNotification(View view) {
        manager.notify(1, notification);

    }

    public void cacelNotification(View view) {
        manager.cancel(1);
    }
}

 局部分析

主要方法

5.创建跳转界面

import android.app.Activity;
import android.os.Bundle;

import androidx.annotation.Nullable;

public class NotificationActivity extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

6.最终效果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小鲁不吃猪蹄

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

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

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

打赏作者

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

抵扣说明:

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

余额充值