通知栏发送消息Notification(可以使用自定义的布局)

一个简单的应用场景:假如用户打开Activity以后,按Home键,此时Activity 进入-> onPause() -> onStop() 不可见。代码在此时机发送一个Notification到通知栏。当用户点击通知栏的Notification后,又重新onRestart() -> onStart() -> onResume() 切换回原Activity。
package com.zzw.testnotification; import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat.Builder;
import android.util.Log;
import android.widget.RemoteViews; public class MainActivity extends Activity { private static final String TAG = "---->"; private final int NOTIFICATION_ID = 0xa01;
private final int REQUEST_CODE = 0xb01; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate");
} @Override
protected void onResume() {
Log.d(TAG, "onResume");
super.onResume();
} @Override
protected void onDestroy() {
Log.d(TAG, "onDestroy");
super.onDestroy();
} @Override
protected void onPause() {
Log.d(TAG, "onPause");
super.onPause();
} @Override
protected void onRestart() {
Log.d(TAG, "onRestart");
super.onRestart();
} @Override
protected void onStart() {
Log.d(TAG, "onStart");
super.onStart();
} @Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop");
sendNotification(this, NOTIFICATION_ID, "这是标题", "这是内容");
} //可当作发送通知栏消息模版使用
private void sendNotification(Context context, int notification_ID, String title, String content) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //使用默认的通知栏布局
Builder builder = new Builder(context);
// 此处设置的图标仅用于显示新提醒时候出现在设备的通知栏
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle(title);
builder.setContentText(content); Notification notification = builder.build(); /* 使用自定义的通知栏布局
* 当用户下来通知栏时候看到的就是RemoteViews中自定义的Notification布局
*/
// RemoteViews contentView = new RemoteViews(context.getPackageName(),
// R.layout.notification);
// contentView.setImageViewResource(R.id.imageView, R.drawable.ic_launcher);
// contentView.setTextViewText(R.id.title, "土耳其和IS的秘密");
// contentView.setTextViewText(R.id.text, "土耳其拒绝向俄罗斯道歉,怀疑有IS撑腰");
// notification.contentView = contentView; // 发送通知到通知栏时:提示声音 + 手机震动 + 点亮Android手机呼吸灯。
// 注意!!(提示声音 + 手机震动)这两项基本上Android手机均支持。
// 但Android呼吸灯能否点亮则取决于各个手机硬件制造商自家的设置。
notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS; // 点击notification自动消失
notification.flags = Notification.FLAG_AUTO_CANCEL; // 通知的时间
notification.when = System.currentTimeMillis(); // 需要注意的是,作为选项,此处可以设置MainActivity的启动模式为singleTop,避免重复新建onCreate()。
Intent intent = new Intent(context, MainActivity.class); // 当用户点击通知栏的Notification时候,切换回MainActivity。
PendingIntent pi = PendingIntent.getActivity(context, REQUEST_CODE, intent, PendingIntent.FLAG_CANCEL_CURRENT);
notification.contentIntent = pi; // 发送到手机的通知栏
notificationManager.notify(notification_ID, notification);
} //可当作清除通知栏消息模版使用
private void deleteNotification(int id) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(id);
}
}
需要注意的是,默认Android的Activity为标准模式,即每次都new一个新的Activity出来,不是原先的Activity,在本例中,可以观察到MainActivity中的onCreate()如果不修改启动模式,则每次本调用每次TextView显示的时间不同(递增),所有为了使用原来的Activity、避免重复new一个新的出来,需要:
在AndroidManifest.xml中修改MainActivity启动模式为:singleTop
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
notification.xml文件源代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ImageView
android:id="@+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" /> <TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/text"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/imageView"
android:layout_marginLeft="18dp"
android:layout_toRightOf="@+id/imageView"
android:gravity="center_vertical"
android:singleLine="true"
android:text="TextView" /> <TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView"
android:layout_alignLeft="@+id/title"
android:gravity="center_vertical"
android:singleLine="true"
android:text="TextView" /> </RelativeLayout>
notification.xml
由于sdk版本的不同,有的需要添加震动的权限:
<uses-permission android:name="android.permission.VIBRATE"/>
通知栏发送消息Notification(可以使用自定义的布局)的更多相关文章
- Python调用飞书发送消息
一.创建飞书机器人 自定义飞书机器人操作步骤,具体详见飞书官方文档:<机器人 | 如何在群聊中使用机器人?>
- 如何给对话框中的控件发送消息呢?Windows消息分类
以博文CTabCtrl中介绍的那样,给Tab添加子对话框来显示Tab内容.那么如果这个子对话框中含有个CTreeCtrl控件,有个Button控件,我想要模拟给这两个控件发送消息,该怎么办呢?直接把给 ...
- zabbix监控web应用日志报警并发送消息到钉钉
首先在钉钉上开启钉钉机器人功能 说明:自定义关键词是zabbix发送过来的消息内容必须含有你定义的ERROR或者error字段,否则消息无法发送过来 ip地址段:一般都是zabbix-server的I ...
- PHP实现RTX发送消息提醒
RTX是腾讯公司推出的企业级即时通信平台,大多数公司都在使用它,但是我们很多时候需要将自己系统或者产品的一些通知实时推送给RTX,这就需要用到RTX的服务端SDK,建议先去看看RTX的SDK开发文档( ...
- [转] C#中发送消息给指定的窗口,以及接收消息
原文C#中发送消息给指定的窗口,以及接收消息 public class Note { //声明 API 函数 [DllImport("User32.dll", EntryPoint ...
- 2014-07-24 .NET实现微信公众号的消息回复与自定义菜单
今天是在吾索实习的第12天.我们在这一天中,基本实现了微信公众号的消息回复与自定义菜单的创建. 首先,是实现消息回复,其关键点如下: 读取POST来的数据流:Stream 数据流变量 = HttpCo ...
- 使用WeCloud消息推送接口发送消息NodeJs版
WeCloud是一家初创公司的产品,眼下主要在做Android和IOS消息推送这块.他们提供了用于向设备发送消息的协议,详细协议内容见消息推送协议. 这篇文章将使用NodeJs基于这个推送协议完毕向A ...
- 发送通知:Notification
Intent的主要功能是完成一个Activity跳转到其他Activity或者是Service的操作,表示的是一种 操作的意图. PendingIntent表示的是暂时执行的一种意图,是一种在产生某一 ...
- Kafka生产者发送消息的三种方式
Kafka是一种分布式的基于发布/订阅的消息系统,它的高吞吐量.灵活的offset是其它消息系统所没有的. Kafka发送消息主要有三种方式: 1.发送并忘记 2.同步发送 3.异步发送+回调函数 下 ...
随机推荐
- 2.1.4 扫描器X-Scan查本机隐患
X-Scan是由安全焦点开发的一个功能强大的扫描工具.它采用多线程方式对指定IP地址段(或单机)进行安全漏洞检测,支持插件功能. 1.用X-Scan查看本机IP地址 利用X-Scan扫描器来查看本机的 ...
- Android(java)学习笔记267:Android线程池形态
1. 线程池简介 多线程技术主要解决处理器单元内多个线程执行的问题,它可以显著减少处理器单元的闲置时间,增加处理器单元的吞吐能力. 假设一个服务器完成一项任务所需时间为:T1 创建线程时间, ...
- CF Sea and Islands
Sea and Islands time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Solr中Schema.xml中文版
<?xml version="1.0" encoding="UTF-8" ?> <!-- Licensed to the Apache Sof ...
- CentOS服务器的16个监控命令
你可以通过图形化用户界面(GUI)程序来获取这些外壳命令提供的大量信息,具体取决于使用哪一种Linux发行版.比如说,SUSE Linux就有一款出色的.图形化配置和管理工具YaST,KDE的KDE ...
- 用户体验测试(UE测试)
用户体验测试(UE测试) 在测试周期早些时候就开始用户体验测试很明智.多数人往往会把UE测试放在最后,但UE测试可以揭示很多问题,如外观.字体.文本颜色.背景颜色.内容.布局等,还可以在测试周期尽可能 ...
- 提高HTML5 Canvas性能的技巧
详细内容请点击 一:使用缓存技术实现预绘制,减少重复绘制Canvs内容 很多时候我们在Canvas上绘制与更新,总是会保留一些不变的内容,对于这些内容 应该预先绘制缓存,而不是每次刷新. 直接绘制代码 ...
- django 学习-12 Django表单 初步
1.先创建项目和应用 django.admin.py startproject cs cd cs django.admin.py startapp blog 2.vim setti ...
- 一.CSS工作原理
CSS全称层叠样式表,它是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言.是能够真正做到网页表现与内容分离的一种样式设计语言,能够对网页 ...
- 通过Scrapy抓取QQ空间
毕业设计题目就是用Scrapy抓取QQ空间的数据,最近毕业设计弄完了,来总结以下: 首先是模拟登录的问题: 由于Tencent对模拟登录比较讨厌,各个防备,而本人能力有限,所以做的最简单的,手动登录后 ...