zoukankan      html  css  js  c++  java
  • 通知栏Notification的应用

    使用通知就要先获取一个 NotificationManage 对象,使用getSystemService(“获取系统的哪一个服务”);,然后在获取赢Notification对象, Notification notification = new NotificationCompat.Builder(MainActivity.this),参数传入上下文。然后在。Builder()方法后面连缀去多方法来创建一个丰富的Notification对象。

    然后调用manager.notify(1, notification);方法完成创建。

    两个参数,第一是ID保证所有通知的ID是不相同的,第二个Notification对象。

    通知震动需要添权限:

    <uses-permission android:name="android.permission.VIBRATE"/>

    Notification的基本功能都在下面代码

    public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, Two.class);
                    PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
                    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                    Notification notification = new NotificationCompat.Builder(MainActivity.this)
                            .setContentTitle("二")
                            //.setContentText("可以跳转")//短文本
                            .setStyle(new NotificationCompat.BigTextStyle().bigText("dsgrth 问题浩如烟海让他加塔吉克就让他" +
                                    " uuj tyy7如图统计如若tu656凧个人挺好看tjy7i而已人个人的体会"))//长文本
                            //.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)))//显示长图片
                            .setWhen(System.currentTimeMillis())//显示通知出现的时间
                            .setSmallIcon(R.mipmap.ic_launcher_round)
                            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round))
                            .setContentIntent(pendingIntent)//跳转
    //                        .setAutoCancel(true)//跳转后消除通知
                            // .setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luna.ogg")))//指定铃声声音
                            .setDefaults(NotificationCompat.DEFAULT_ALL)//手机默认铃声
                            .setVibrate(new long[]{0, 1000, 1000, 1000})//震动提示,数组里面0下表代表静音,1下表代表震动,以此类推
                            .setLights(Color.GREEN, 1000, 1000)//LED灯闪烁
                            .setPriority(NotificationCompat.PRIORITY_MAX)//显示内容的重要程度
                            .build();
                    manager.notify(1, notification);
                }
  • 相关阅读:
    STM32 HAL库学习笔记
    嵌入式Linux学习笔记
    AVR_Interrupt
    shutdown命令用法
    ULINK2 USB电脑无法识别(连接电脑后,设备管理器显示未知设备)
    MDK中编译程序后Program Size详解
    Keil(MDK-ARM)系列教程(三)_工程目标选项配置(Ⅰ)
    第48章 MDK的编译过程及文件类型全解
    Do not access Object.prototype method ‘hasOwnProperty’ from target object no-prototype-builtins
    让vscode按照eslint进行格式化
  • 原文地址:https://www.cnblogs.com/zhoushenglei/p/7194087.html
Copyright © 2011-2022 走看看