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 ...
随机推荐
- C# 模拟键盘操作SendKey(),SendKeys()
模拟键盘输入就是使用以下2个语法实现的. SendKeys.Send(string keys); //模拟汉字(文本)输入SendKeys.SendWait(string keys); //模拟按键 ...
- Cocos动作执行时,同时执行完毕再进行下一步的方式
在js中,runAction是统一保存起来等单个文件运行完了再统一进行回调运行的,所以如果想在动作执行完毕之后调用某个函数,那这个函数就应该存在于回调函数中,不会就不能同步了
- IOS 修改图片的地理位置信息
直接上代码: // // ViewController.m // changeLocation // // Created by 陈飞 on 16/10/31. // Copyright © 2016 ...
- lxml爬取实验
1.豆瓣 爬取单个页面数据 import requests from lxml import etree #import os url = "https://movie.douban.com ...
- PostGIS导出SHP中文乱码
设置系统的环境变量 PGCLIENTENCODING=GBK,退出PostgreSQL重新登錄,执行成功!
- 做IT,必备的安全知识!
以前的认知 以前刚接触IT行业,而我身为运维,我以为我所需要做的安全就是修改服务器密码为复杂的,ssh端口改为非22,还有就是不让人登录服务器就可以保证我维护的东西安全. 现在的认知 工作也好几年了, ...
- caffe 根据txt生成多标签LMDB数据
1. 前提: 已经准备好train.txt, test.txt文件, 格式如下 此处有坑, 如果是windows下生成txt, 换行符为\r\n, 需要替换成 \n才能在linux运行. 可以使用se ...
- Java - Multithreading zz
Java is a multi-threaded programming language which means we can develop multi-threaded program usin ...
- MongoDb安装和快速入门
1.Mongodb安装 2.mongodb的增删改查 3.MongoDB数据类型 4.Mongodb $关键字 $修改器 5.MongoDB 之 "$" 的奇妙用法 6.Mongo ...
- U-Boot内存管理
如<Linux内核内存管理架构>一文中提到,linux内核中的内存管理支持内存地址映射.内存分配.内存回收.内存碎片管理.页面缓存等众多功能.但U-Boot做为启动引导程序,其核心功能就是 ...