Android控件【Notification】

Notification与NotifactionManager

创建一个NotificationManager

NotificationManagerl类是一个通知管理器类,这个对象是由系统维护的服务,是以单例模式的方式获得,所以一般并不直接实例化这个对象,在Activity中,可以使用Activity.getSystemService(String)方法获取NotificationManager对象,Activity.getSystemService(String)方法可以通过Android系统级服务的句柄,返回对应的对象。在这里需要返回NotificationManager,所以直接传递Context.NOTIFICATION_SERVICE即可

使用Builder构造器来创建Notification对象

使用NotificationCompat类的Builder构造器来创建Notification对象,可以保证程序在所有的版本上都能正常工作。Android8.0新增了通知渠道这个概念,如果有设置,则通知无法在Android8.0的机器上显示

NotificationChannel

通知渠道:Android8.0引入了通知渠道,其允许您为要显示的每种通知类型创建用户可自定义的渠道

通知重要程度设置,NotificationManager类中

IMPORTANCE_NONE关闭通知
IMPORTANCE_MIN开启通知,不会弹出,但没有提示音,状态栏中无显示
IMPORTANCE_LOW开启通知,不会弹出,不发出提示音,状态栏中显示
IMPORTANCE_DEFAULT默认状态,开启通知,不会弹出,发出提示音,状态栏中显示
IMPORTANCE_HIGH开启通知,会弹出,发出提示音,状态栏中显示

Notification(通知)常见方法说明

  1. setContentTitle(String string):设置标题
  2. setContentText:设置文本内容
  3. setSmallIcon(int icon):设置小图标
  4. setLargeIcon(Bitmap icon):设置通知的大图标
  5. setColor(int argb):设置小图标的颜色
  6. setContentIntent(PendingIntent intent):设置点击通知后的跳转意图
  7. setAutoCancel(boolean flag):设置点击通知后自动清除通知
  8. setWhen(long when):设置通知被创建的时间

前三个是必须写的

注意

Android5.0系统开始,对于通知栏图标的设计进行了修改。
现在Google要求,所有应用程序的通知栏图标,应该只使用alpha图层来进行绘制,而不应该包括RGB图层。
简单来说,就是通知栏图标不能带颜色

项目结构

在这里插入图片描述

主要代码

MainActivity

package com.study.mynotification;

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);

        //获取NotificationManager对象
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        //判断版本是否在8.0及以上
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //创建一个NotificationChannel
            //参数说明
            //第一个参数(id):也就是notification里面的channelId,在这里需要保持一致,才有作用
            //第二个参数(name):用户点击通知的时候可以看到,一般根据公司的需求
            //第三个参数(importance):通知的重要程度
            NotificationChannel notificationChannel = new NotificationChannel("leo", "测试通知",
                    NotificationManager.IMPORTANCE_HIGH);
            //这样相当于notificationChannel的Id和下面notification的channelId形成了一个绑定关系
            manager.createNotificationChannel(notificationChannel);
        }

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

        //创建一个notification对象(通知)
        //channelId:渠道id
        notification = new NotificationCompat.Builder(this, "leo")
                .setContentTitle("官方通知")    //必须设置
                .setContentText("立大事者不惟有超世之才,亦必有坚韧不拔之志") //必须设置
                .setSmallIcon(R.drawable.baseline_person_24)     //设置小图标,必须设置
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.two))//设置大图标
                .setColor(Color.parseColor("#ff0000"))  //设置小图标颜色
                .setContentIntent(pendingIntent)    //点击通知后的跳转意图
                .setAutoCancel(true)    //点击后自动清楚通知
                .build();
    }

    public void sendNotification(View view) {
        //发送通知
        manager.notify(1, notification);
    }

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

NotificationActivity

package com.study.mynotification;

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

import androidx.annotation.Nullable;

public class NotificationActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.e("leo", "onCreate: 进入NotificationActivity");
    }
}

activity_main.xml

<?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"
    android:orientation="vertical">

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

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


</LinearLayout>

AndroidManifest.xml

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

    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.StudyApp">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.study.mynotification.NotificationActivity" />
    </application>

</manifest>

tow.jpg
在这里插入图片描述

baseline_person_24.xml

<vector android:height="24dp" android:tint="#0A0A0A"
    android:viewportHeight="24" android:viewportWidth="24"
    android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="@android:color/white" android:pathData="M12,12c2.21,0 4,-1.79 4,-4s-1.79,-4 -4,-4 -4,1.79 -4,4 1.79,4 4,4zM12,14c-2.67,0 -8,1.34 -8,4v2h16v-2c0,-2.66 -5.33,-4 -8,-4z"/>
</vector>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值