通知栏发送消息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.异步发送+回调函数 下 ...
随机推荐
- Android 常用 adb 命令
查看原文:http://blog.csdn.net/u010818425/article/details/52266593 (一)基础操作 安装app adb install -r xxx.apk / ...
- iis8 默认不支持svc解决方法
最近在IIS8中发布WCF服务应用时,发现IIS8不支持WCF服务svc请求,后来发现IIS8缺少对WCF服务的Managed Handler,按照以下步骤添加后,IIS8即支持WCF服务. 1. 首 ...
- unity与Android交互
unity打包成安卓工程中的JAVA代码 public class UnityPlayerActivity extends Activity { //Unity中调用此函数 public int Ma ...
- Umbraco(1) - Document Types(翻译文档)
Document Types Data first nothing in = nothing out! 任何网站的第一步是创建一个"Document Type"-几次安装后你会熟悉 ...
- Linux 网络设备驱动程序设计(2)
二.回环网卡的程序设计 /*************************** *******回环网卡的驱动程序*** ***********吕晓宁*********** *********2015 ...
- html+css--水平居中总结(不定宽块状元素方法)(一)
来源:http://www.imooc.com/code/6363 在实际工作中我们会遇到需要为“不定宽度的块状元素”设置居中,比如网页上的分页导航,因为分页的数量是不确定的,所以我们不能通过设置宽度 ...
- 四舍五入PK银行四舍五入
描述 在实际开发中decimal.Round(1.23525,4)!=1.2353实际是1.2352,而decimal.Round(1.23535,4)==1.2354 说明 四舍五入:当舍去位的数值 ...
- Session,Cookie,jsessionid,Url重写
在一些投票之类的场合,我们往往因为公平的原则要求每人只能投一票,在一些WEB开发中也有类似的情况,这时候我们通常会使用COOKIE来实现,例如如下的代码: < % cookie[]cookies ...
- 会话—session
这篇随笔是上一篇的姊妹篇@_@! session 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下) ...
- 关于div 浮动在select,或table控件之上
<div style="position:absolute; display:none; z-index:99999" id="d3" onmouseov ...