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 ...
随机推荐
- 解决vscode格式化vue文件出现的问题
遇到的问题 使用vscode开发vue项目的时候,格式化vue文件,与自己配置的eslint标准会有冲突. 引号问题:单引号变双引号 分号问题:行末是否加分号.自动加/减分号 当然还会有其他个性化冲突 ...
- winxp改AHCI不再蓝屏,不用改注册表,所有PC机通用
要用win8的pe 在通常的情况下,硬盘在BIOS中默认为原生IDE模式以获得最好的兼容性.对配件要求较高的W7,W8系统中,而通过开启硬盘AHCI模式,可以在一定程度上提升硬盘的性能表现.如果在ID ...
- keras安装-【老鱼学keras】
为何要用keras? 两个字:简单. Keras让深度学习像搭建积木一样方便地来进行,使前面的tensorflow能够更加方便地使用. 虽然还有其它更多的理由,比如:Keras 支持多个后端引擎,不会 ...
- Delphi中DataSet和JSON的互转
//1)数据集转换为JSON字符串://需USES System.JSON; function DataSetToJson(ADataset: TDataSet): string; // [{&quo ...
- 使用vue-awesome-swiper的相关问题
最近自己在仿做一个旅游网站的vue项目,在首页中使用了vue-awesome-swiper插件来实现轮播图的效果,发现了以下几种问题: 一.需要额外引入swiper.css 原来使用vue-aweso ...
- <玩转Django2.0>读书笔记:URL规则和视图
1. 带变量的URL #urls.py from django.urls import path from .view import * urlpatterns = [ path('',index_v ...
- C# 串口操作系列(5)--通讯库雏形
C# 串口操作系列(5)--通讯库雏形 标签: 通讯c#数据分析byteclassstring 2010-08-09 00:07 21378人阅读 评论(73) 收藏 举报 分类: 通讯类库设计(4 ...
- SCOPE_IDENTITY() 和 @@identity
@@IDENTITY 和SCOPE_IDENTITY 返回在当前会话中的任何表内所生成的最后一个标识值.但是,SCOPE_IDENTITY 只返回插入到当前作用域中的值:@@IDENTITY 不受限于 ...
- vue 使用瞬间
vue 使用瞬间 一, 图片类 <img :src="data.deptLogo | imgUrl" onerror="this.src='../img/headD ...
- cadence钻孔文件及光绘文件的生成
完成PCB布线之后,需要生成钻孔文件和光绘文件交给PCB厂家制作PCB板,下面总结详细方法!