一个简单的应用场景:假如用户打开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(可以使用自定义的布局)的更多相关文章

  1. Python调用飞书发送消息

    一.创建飞书机器人 自定义飞书机器人操作步骤,具体详见飞书官方文档:<机器人 | 如何在群聊中使用机器人?>

  2. 如何给对话框中的控件发送消息呢?Windows消息分类

    以博文CTabCtrl中介绍的那样,给Tab添加子对话框来显示Tab内容.那么如果这个子对话框中含有个CTreeCtrl控件,有个Button控件,我想要模拟给这两个控件发送消息,该怎么办呢?直接把给 ...

  3. zabbix监控web应用日志报警并发送消息到钉钉

    首先在钉钉上开启钉钉机器人功能 说明:自定义关键词是zabbix发送过来的消息内容必须含有你定义的ERROR或者error字段,否则消息无法发送过来 ip地址段:一般都是zabbix-server的I ...

  4. PHP实现RTX发送消息提醒

    RTX是腾讯公司推出的企业级即时通信平台,大多数公司都在使用它,但是我们很多时候需要将自己系统或者产品的一些通知实时推送给RTX,这就需要用到RTX的服务端SDK,建议先去看看RTX的SDK开发文档( ...

  5. [转] C#中发送消息给指定的窗口,以及接收消息

    原文C#中发送消息给指定的窗口,以及接收消息 public class Note { //声明 API 函数 [DllImport("User32.dll", EntryPoint ...

  6. 2014-07-24 .NET实现微信公众号的消息回复与自定义菜单

    今天是在吾索实习的第12天.我们在这一天中,基本实现了微信公众号的消息回复与自定义菜单的创建. 首先,是实现消息回复,其关键点如下: 读取POST来的数据流:Stream 数据流变量 = HttpCo ...

  7. 使用WeCloud消息推送接口发送消息NodeJs版

    WeCloud是一家初创公司的产品,眼下主要在做Android和IOS消息推送这块.他们提供了用于向设备发送消息的协议,详细协议内容见消息推送协议. 这篇文章将使用NodeJs基于这个推送协议完毕向A ...

  8. 发送通知:Notification

    Intent的主要功能是完成一个Activity跳转到其他Activity或者是Service的操作,表示的是一种 操作的意图. PendingIntent表示的是暂时执行的一种意图,是一种在产生某一 ...

  9. Kafka生产者发送消息的三种方式

    Kafka是一种分布式的基于发布/订阅的消息系统,它的高吞吐量.灵活的offset是其它消息系统所没有的. Kafka发送消息主要有三种方式: 1.发送并忘记 2.同步发送 3.异步发送+回调函数 下 ...

随机推荐

  1. mysql 查看表记录新增、修改的时间

    ALTER TABLE `tableName` ADD `updateAt` TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP; ALTER TABLE kd_up ...

  2. 【Android 界面效果38】android:inputType常用取值

    <EditText android:layout_width="fill_parent" android:layout_height="wrap_content&q ...

  3. IOS设计模式六大法则

    设计模式的六大原则 单一职责原则 定义:不要存在多于一个导致类变更的原因.通俗的说,即一个类只负责一项职责 问题由来:类T负责两个不同的职责:职责P1,职责P2.当由于职责P1需求发生改变而需要修改类 ...

  4. ubuntu创建、删除文件及文件夹,强制清空回收站方法

    mkdir 目录名         => 创建一个目录 rmdir 空目录名      => 删除一个空目录 rm 文件名 文件名   => 删除一个文件或多个文件 rm –rf 非 ...

  5. Shodan!

    Shodan! 简介 首先先介绍一下Shodan CNNMoney的一篇文章写道,虽然目前人们都认为谷歌是最强劲的搜索引擎,但Shodan才是互联网上最可怕的搜索引擎. 与谷歌不同的是,Shodan不 ...

  6. DWZ按钮居中显示

  7. hdu 4700 那个啥树

    思路:我也不知道叫什么树,但是构造过程能理解. 我们可以将先将边按降序排序,那么就用kruskaer构造生成树.构造好的生成树也就是满足条件的图,因为点i,j的最大流量就是生成树上点i到点j的路径上的 ...

  8. arcsde service(esri_sde)服务启动后又停止

    由于最近几天我们公司换了新办公楼,各种服务器得重新配置.当我试图直接将arcsde Service的服务器IP改为现在的地址,就报上面如题的错误. SQL服务器是好的,不用管它,只要确保它是开启的.只 ...

  9. 计算日期时间 自动加1天 PHP计算闰年 java与PHP时间戳对比区别

    昨天写一个同步数据库的模块  从一个数据库同步到另外一个数据库,因为数据较多,不可能一次性全部搬迁过去,所以就按照每天搬迁! 写了一个 模块,点击加1,只要点击一次,自动从A数据库取出1天的数据, 并 ...

  10. 工作单元(Unit of Work)

    维护受业务事务影响的对象列表,并协调变化的写入和并发问题的解决. 从DB中存取Data时,必须记录增删改动作,以将对DB有影响的数据写会到DB中去. 如果在每次修改对象模型时就对DB进行相应的修改,会 ...