一.概述

今天来讲讲Notification的使用,先看效果图。

二.代码

首先是布局文件,我们定义三个按钮

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:fitsSystemWindows="true"android:orientation="vertical"android:gravity="center"tools:context="com.example.notificationdemo.MainActivity"><Buttonandroid:id="@+id/normal"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="click"android:text="发送普通通知"/><Buttonandroid:id="@+id/custom"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="发送自定义通知"android:onClick="click"/><Buttonandroid:id="@+id/dismiss"android:layout_width="wrap_content"android:layout_height="wrap_content"android:onClick="click"android:text="取消通知"/>
</LinearLayout>

接下来看主要代码,我会给出比较详细的注释:

public class MainActivity extends AppCompatActivity {private NotificationManager manager;private static final int NOTIFYID = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获取通知服务manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);}public void click(View view){switch (view.getId()){case R.id.normal:sendNormalNotification();break;case R.id.custom:sendCustomNotification();break;case R.id.dismiss:
//                manager.cancel(NOTIFYID);//根据通知id取消对应的通知manager.cancelAll();//取消所有的通知break;}}/*** 发送普通通知*/public void sendNormalNotification(){Notification.Builder builder = new Notification.Builder(this);builder.setTicker("消息来了");//设置提示消息builder.setContentText("今天出去吃饭吧");//通知内容builder.setContentTitle("提示");//通知标题builder.setSmallIcon(R.mipmap.head);//提示消息旁边的小图标builder.setNumber(2);//设置右边显示的数字builder.setOngoing(true);//是否可以滑动移除通知,true代表可以builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));//设置大图标Notification notify = builder.build();//点击通知启动的页面Intent intent = new Intent(this,MessageActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);builder.setContentIntent(pendingIntent);//启动通知manager.notify(NOTIFYID,notify);}/*** 自定义通知*/public void sendCustomNotification(){Notification.Builder builder2 = new Notification.Builder(this);builder2.setTicker("开始下载");builder2.setSmallIcon(R.mipmap.ic_launcher);builder2.setWhen(System.currentTimeMillis());//第一个参数为包名。第二个参数为通知布局RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.cunstom_layout);builder2.setContent(remoteViews);Notification notify = builder2.build();manager.notify(1,notify);}
}

源码下载

Android 使用Notification进行消息提示相关推荐

  1. android接收消息后提示音,Android仿微信新消息提示音

    怕有些人不知道怎么进入微信的新消息提示音功能,我这里说下操作步骤: 打开微信----我---设置---新消息提醒---新消息提示音. 经过以上的步骤就进入了这样的界面 具体实现的步骤. 难点之一:获取 ...

  2. Android第三方开源对话消息提示框:SweetAlertDialog(sweet-alert-dialog)

     Android第三方开源对话消息提示框:SweetAlertDialog(sweet-alert-dialog) Android第三方开源对话消息提示框:SweetAlertDialog(swe ...

  3. android新消息提醒功能,Android仿微信新消息提示音

    怕有些人不知道怎么进入微信的新消息提示音功能,我这里说下操作步骤: 打开微信----我---设置---新消息提醒---新消息提示音. 经过以上的步骤就进入了这样的界面 具体实现的步骤. 难点之一:获取 ...

  4. Android自定义未读消息提示View

    微信的小红点不知道逼死了多少人的强迫症,今天我们就来实现一个带有小红点的View,先上效果图: 读者须知: 虽然我会在文末给出源码,但是我的源码只是按照我的需求编写的,希望读者能够理解整个思路然后结合 ...

  5. android微信加人有提示音吗,Android仿微信新消息提示音

    環信聊天消息提示音的實現 仿微信新消息提示音設置. 思路:用RingtoneManager查詢出title,Ringtone,uri信息,title用來展示,Ringtone用來播放,uri設置提示音 ...

  6. Android的notification通知

    概括: Android使用notification实现消息提醒,提供通知栏和通知抽屉查看通知和通知详情,Android的notification机制应用非常广泛,是指在应用程序之外显示的消息,布局由系 ...

  7. Android实现系统下拉栏的消息提示——Notification

    Android实现系统下拉栏的消息提示--Notification 系统默认样式 默认通知(通用) 效果图 按钮 <Button android:layout_width="match ...

  8. 【Android】消息提示notification

    notification 1.notification消息提示 由Android系统来管理和维护的,因此用户可以随时进入查看.某些信息不需要用户马上处理,可以利用通知,即延迟消息,比如软件的更新.短信 ...

  9. Android Notification消息提示

    一般而言,消息提示,可以通过Toast方式,提醒给用户看,而通过Notification方式的话,可以在状态栏显示出来.并且有声音,还有文字描述,并且可以出现在消息公告栏,这在QQ,飞信等常用即时通信 ...

最新文章

  1. 企业局域网离不开交换机/路由器/防火墙—Vecloud
  2. asp 取菜单的名字_这样的奶茶店菜单设计,店面营业额能提高30%!
  3. linux命令——init 的使用用法
  4. 模糊聚类算法(FCM)和硬聚类算法(HCM)的VB6.0实现及
  5. ABP实现EF执行SQL(增删改查)解决方案
  6. 取值方法_函数的定义域和参数的取值范围详解
  7. 双目视觉立体匹配算法
  8. 基于FPGA的多人表决器(VHDL)
  9. 线条边框简笔画图片大全_植物简笔画素材大全赶紧收藏起来,一定用的上!
  10. 伊斯坦布尔美丽风景mac高清桌面动态壁纸
  11. unity Layer CullingMask
  12. 数据分析 - 跨境电商爬虫成长记 之 第三篇:完整的爬虫工作环节有哪些
  13. Xshell6和Xftp6 破解免安装版,无窗口多开限制
  14. 使用AHK减少鼠标和方向键的使用频率,高效编辑
  15. 【Python】PEP8规范
  16. ArrayList中元素的删除操作
  17. web返回的数据集格式_200G倾斜数据无插件web端预览!兼容三端,有容乃大—MapGIS M3D数据格式...
  18. AtCoder - ABC 167 - E(数学推理+组合数)
  19. 2022-2028全球与中国ABF基板市场现状及未来发展趋势
  20. Qt官方示例-速度仪表盘

热门文章

  1. 【基于JAVA的旅游路线推荐系统-哔哩哔哩】 https://b23.tv/4STx5NI
  2. cadence软件选择网络功能
  3. Docer实现Django Uwsgi部署
  4. 算法设计与分析——动态规划
  5. 结合盒子模型,发布一张你的卡片
  6. 安装应用宝统一链接服务器,数据互通|安卓应用宝部分区服服务器数据互通维护公告...
  7. vue 中 用showdown预览markdown文件,并用highlight.js 实现代码高亮
  8. RK3568J edp屏幕点亮 时序调试总结
  9. writing idiomatic python 读书笔记(2)
  10. 4位共阴极数码管的动态扫描电路VHDL设计