6、Android -- Notification 消息通知

对象

1、NotificationManger

  • NotificationManager类是一个通知管理器类,这个对象是由系统维护的服务,是以单例模式的方式获得,所以一般并不直接实例化这个对象
    • 引用: 在Activity中, 可以使用Activity.getSystemService(String)方法获取

NotificationManager对象
例: NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

2、Notification对象

现在一般使用NotificationCompat 类的Bu]der构造器来创建Notification对象,可以保证程序在所有的版本上都能正常工作(在老的版本中是使用Notification)

Notification设置方法

  1. setContentTitle(String string) 设置标题
  2. setContentText(String string) 设置文本内容
  3. setSmallcon(int icon) 设置小图标
  4. setLargelcon(Bitmap icon) 设置通知的大图标
  5. setColor(int argb) 设置小图标的颜色
  6. setContentintent(Pendingintent intent)设置点击通知后的跳转意图
  7. setAutoCancel(boolean boolean) 设置点击通知后自动清除通知
  8. setWhen(long when)设置通知被创建的时间
  9. setPriority(ing pri) 设置通知级别(类似是否置顶)
  10. setDefaultsint defaults:设置通知方式(声音、震动等)
  11. setProgress(nt max, int progress, boolean indeterminate):通知带滚动条(参数,最大值,当前进程,是否滚动)

发送通知

manager.notify(int id, Notification notification)

取消发送通知

manager.cancel(int id);

示例

package com.pha.first;

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

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

public class NotificationActivity extends AppCompatActivity {
    private NotificationManager manager;
    private Notification notification;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel channel = new NotificationChannel("Mr Zhao", "会议通知",
                    NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);
        }
    }

    public void SendNotification(View view) {
        Intent intent = new Intent(this,NotificationActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
        notification = new NotificationCompat.Builder(this, "Mr Zhao")
                .setDefaults(Notification.DEFAULT_SOUND)
                .setContentTitle("会议通知Info")
                .setContentText("今天下午16:00大型会议室开会,主题如何高效的提高工作效率")
                .setSmallIcon(R.drawable.ic_baseline_person_24)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_logo))
                .setColor(Color.parseColor("#FF0000"))
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .setWhen(System.currentTimeMillis())
                .setPriority(Notification.PRIORITY_MAX)
                .setProgress(100,24,false)
                //.addAction(R.drawable.ic_baseline_5g_24,"左按钮",null)
                //.addAction(R.mipmap.ic_launcher,"中按钮",null)
                .build();
        manager.notify(1,notification);

    }

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

在这里插入图片描述
在这里插入图片描述

记忆知识点

1、图片转化Bitmap:BitmapFactory.decodeResource(getResources(),R.drawable.ic_logo)
2、颜色转换成int:Color.parseColor(“#FF0000”)
3、获取当前时间,毫秒级:System.currentTimeMills()。(1970-01-01 00:00:00.000到现在的毫秒长度)
4、手机SDK版本:Build.VERSION.SDK_INT

  • 相关信息:
  • "手机型号: " + android.os.Build.MODEL
  • “,SDK版本:” + android.os.Build.VERSION.SDK
  • “,系统版本:” + android.os.Build.VERSION.RELEASE
  • “,SDK:”+Build.VERSION.SDK_INT

5、Build.VERSION_CODES.O :应该是代表的是Android8.0版本,android api为26,代号Oreo(缩写O),发布版2017
6、兼容高本版使用NotificationCompat

 		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            NotificationChannel channel = new NotificationChannel("Mr Zhao", "会议通知",
                    NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值