Android 普通通知栏新方法,现在需要创建通知渠道才可以
先看看效果看看是不是你想要的

点击后


话不多所,贴代码
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainSendMessage">
<Button
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送通知"
/> </LinearLayout>

activity代码:
package com.example.love5; import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class MainSendMessage extends AppCompatActivity {
private Button b1; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_send_message); b1 = findViewById(R.id.b1);
b1.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View v) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//创建通知渠道实例
//并为它设置属性
//通知渠道的ID,随便写
String id = "channel_01";
//用户可以看到的通知渠道的名字,R.string.app_name就是strings.xml文件的参数,自定义一个就好了
CharSequence name = getString(R.string.app_name);
//用户可看到的通知描述
String description = getString(R.string.app_name);
//构建NotificationChannel实例
NotificationChannel notificationChannel =
new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);
//配置通知渠道的属性
notificationChannel.setDescription(description);
//设置通知出现时的闪光灯
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
//设置通知出现时的震动
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 100});
//在notificationManager中创建通知渠道
manager.createNotificationChannel(notificationChannel); //蓝色字是个新方法,旧的被api放弃了
Notification notification = new NotificationCompat.Builder(MainSendMessage.this, id)
.setContentTitle("标题")
.setContentText("内容666666666666666666666666666")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.tubiao)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.coffee))
.build();
manager.notify(1, notification); }
}); }
}
Android 普通通知栏新方法,现在需要创建通知渠道才可以的更多相关文章
- 【Android】一种提高Android应用进程存活率新方法
[Android]一种提高Android应用进程存活率新方法 SkySeraph Jun. 19st 2016 Email:skyseraph00@163.com 更多精彩请直接访问SkySeraph ...
- 一种提高Android应用进程存活率新方法
一.基础知识 1.Android 进程优先级 1.1 进程优先级等级一般分法:- Activte process- Visible Process- Service process- Backgrou ...
- 腾讯云安全:开发者必看|Android 8.0 新特性及开发指南
欢迎大家关注腾讯云技术社区-博客园官方主页,我们将持续在博客园为大家推荐技术精品文章哦~ 背景介绍 谷歌2017 I/O开发者大会今年将于5月17-19日在美国加州举办.大会将跟往年一样发布最新的 A ...
- 开发者必看|Android 8.0 新特性及开发指南
背景介绍 谷歌2017 I/O开发者大会今年将于5月17-19日在美国加州举办.大会将跟往年一样发布最新的 Android 系统,今年为 Android 8.0.谷歌在今年3 月21日发布 Andro ...
- 第6篇-Java方法新栈帧的创建
在 第2篇-JVM虚拟机这样来调用Java主类的main()方法 介绍JavaCalls::call_helper()函数的实现时提到过如下一句代码: address entry_point = me ...
- 《Android群英传》读书笔记 (5) 第十一章 搭建云端服务器 + 第十二章 Android 5.X新特性详解 + 第十三章 Android实例提高
第十一章 搭建云端服务器 该章主要介绍了移动后端服务的概念以及Bmob的使用,比较简单,所以略过不总结. 第十三章 Android实例提高 该章主要介绍了拼图游戏和2048的小项目实例,主要是代码,所 ...
- [Android]使用Dagger 2依赖注入 - 图表创建的性能(翻译)
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5098943.html 使用Dagger 2依赖注入 - 图表创 ...
- Android 5.X新特性之为RecyclerView添加下拉刷新和上拉加载及SwipeRefreshLayout实现原理
RecyclerView已经写过两篇文章了,分别是Android 5.X新特性之RecyclerView基本解析及无限复用 和 Android 5.X新特性之为RecyclerView添加Header ...
- Android经典完美退出方法
Android经典完美退出方法,使用单例模式创建一个Activity管理对象,该对象中有一个Activity容器(具体实现自己处理,使用LinkedList等)专门负责存储新开启的每一个Activit ...
随机推荐
- tensorflow RNN循环神经网络 (分类例子)-【老鱼学tensorflow】
之前我们学习过用CNN(卷积神经网络)来识别手写字,在CNN中是把图片看成了二维矩阵,然后在二维矩阵中堆叠高度值来进行识别. 而在RNN中增添了时间的维度,因为我们会发现有些图片或者语言或语音等会在时 ...
- DevExpress控件库 开发使用经验总结1 DevExpress简介、安装、使用
2015-01-24 最近公司开发的WinForm客户端图书行业ERP管理系统,界面端采用了DevExpress控件库.界面效果非常绚丽,类似于Office2007.2010的界面风格. 其中的控件功 ...
- pyzmq学习笔记
前言 使用过简单的python的ZMQ:server开启3个线程,client发送心跳包,如果服务端超过n秒没应答,则重新连接. 网上找的案例,server使用的zmq.device,但是一直不明白什 ...
- java 模拟浏览器爬虫
- Macbook pro从购买服务器到搭建服务器环境(2)
这里是在Mac本地上安装软件遇到的坑,先总结一下 在装完mysql的时候,安装wget,这个时候遇到的问题是openssl软件包找不到,我已经不记得是什么时候安装的openssl了,所以用命令查一下 ...
- c语言博客作业--结构体&文件
1.本章学习总结 1.1思维导图 1.2.本章学习体会 本章学到了结构体如何构建,用typedef进行重定义结构体,结构指针,访问结构体的方式等. 文件:文件的读取,文件读写的几个函数,基本了解了文本 ...
- 771. Jewels and Stones
You're given strings J representing the types of stones that are jewels, and S representing the ston ...
- 4.DHCP与PRE
如何配置IP地址 使用net-tools $ sudo ifconfig eth1 10.0.0.1/24 $ sudo ifconfig eth1 up 使用Iproute2 ...
- web全套资料 干货满满 各种文章详解
sql注入l MySqlMySQL False注入及技巧总结MySQL 注入攻击与防御sql注入学习总结SQL注入防御与绕过的几种姿势MySQL偏门技巧mysql注入可报错时爆表名.字段名.库名高级S ...
- ChartControl设置坐标轴范围
需求:有一条坐标轴是用来表示百分比的,所以刻度范围只能是从0 - 100: 解决方法:运行chatcontrol设计器,选中需要设置的轴,如下图: 设置Whole range 的最大最小值: 默认情况 ...