一个简单的应用场景:假如用户打开Activity以后,按Home键,此时Activity 进入-> onPause() -> onStop() 不可见。代码在此时机发送一个Notification到通知栏。当用户点击通知栏的Notification后,又重新onRestart() -> onStart() -> onResume() 切换回原Activity。

  1. package com.zzw.testnotification;
  2.  
  3. import android.app.Activity;
  4. import android.app.Notification;
  5. import android.app.NotificationManager;
  6. import android.app.PendingIntent;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.os.Bundle;
  10. import android.support.v4.app.NotificationCompat.Builder;
  11. import android.util.Log;
  12. import android.widget.RemoteViews;
  13.  
  14. public class MainActivity extends Activity {
  15.  
  16. private static final String TAG = "---->";
  17.  
  18. private final int NOTIFICATION_ID = 0xa01;
  19. private final int REQUEST_CODE = 0xb01;
  20.  
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. Log.d(TAG, "onCreate");
  26. }
  27.  
  28. @Override
  29. protected void onResume() {
  30. Log.d(TAG, "onResume");
  31. super.onResume();
  32. }
  33.  
  34. @Override
  35. protected void onDestroy() {
  36. Log.d(TAG, "onDestroy");
  37. super.onDestroy();
  38. }
  39.  
  40. @Override
  41. protected void onPause() {
  42. Log.d(TAG, "onPause");
  43. super.onPause();
  44. }
  45.  
  46. @Override
  47. protected void onRestart() {
  48. Log.d(TAG, "onRestart");
  49. super.onRestart();
  50. }
  51.  
  52. @Override
  53. protected void onStart() {
  54. Log.d(TAG, "onStart");
  55. super.onStart();
  56. }
  57.  
  58. @Override
  59. protected void onStop() {
  60. super.onStop();
  61. Log.d(TAG, "onStop");
  62. sendNotification(this, NOTIFICATION_ID, "这是标题", "这是内容");
  63. }
  64.  
  65. //可当作发送通知栏消息模版使用
  66. private void sendNotification(Context context, int notification_ID, String title, String content) {
  67. NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  68.  
  69. //使用默认的通知栏布局
  70. Builder builder = new Builder(context);
  71. // 此处设置的图标仅用于显示新提醒时候出现在设备的通知栏
  72. builder.setSmallIcon(R.drawable.ic_launcher);
  73. builder.setContentTitle(title);
  74. builder.setContentText(content);
  75.  
  76. Notification notification = builder.build();
  77.  
  78. /* 使用自定义的通知栏布局
  79. * 当用户下来通知栏时候看到的就是RemoteViews中自定义的Notification布局
  80. */
  81. // RemoteViews contentView = new RemoteViews(context.getPackageName(),
  82. // R.layout.notification);
  83. // contentView.setImageViewResource(R.id.imageView, R.drawable.ic_launcher);
  84. // contentView.setTextViewText(R.id.title, "土耳其和IS的秘密");
  85. // contentView.setTextViewText(R.id.text, "土耳其拒绝向俄罗斯道歉,怀疑有IS撑腰");
  86. // notification.contentView = contentView;
  87.  
  88. // 发送通知到通知栏时:提示声音 + 手机震动 + 点亮Android手机呼吸灯。
  89. // 注意!!(提示声音 + 手机震动)这两项基本上Android手机均支持。
  90. // 但Android呼吸灯能否点亮则取决于各个手机硬件制造商自家的设置。
  91. notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS;
  92.  
  93. // 点击notification自动消失
  94. notification.flags = Notification.FLAG_AUTO_CANCEL;
  95.  
  96. // 通知的时间
  97. notification.when = System.currentTimeMillis();
  98.  
  99. // 需要注意的是,作为选项,此处可以设置MainActivity的启动模式为singleTop,避免重复新建onCreate()。
  100. Intent intent = new Intent(context, MainActivity.class);
  101.  
  102. // 当用户点击通知栏的Notification时候,切换回MainActivity。
  103. PendingIntent pi = PendingIntent.getActivity(context, REQUEST_CODE, intent, PendingIntent.FLAG_CANCEL_CURRENT);
  104. notification.contentIntent = pi;
  105.  
  106. // 发送到手机的通知栏
  107. notificationManager.notify(notification_ID, notification);
  108. }
  109.  
  110. //可当作清除通知栏消息模版使用
  111. private void deleteNotification(int id) {
  112. NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  113. notificationManager.cancel(id);
  114. }
  115. }

需要注意的是,默认Android的Activity为标准模式,即每次都new一个新的Activity出来,不是原先的Activity,在本例中,可以观察到MainActivity中的onCreate()如果不修改启动模式,则每次本调用每次TextView显示的时间不同(递增),所有为了使用原来的Activity、避免重复new一个新的出来,需要:

在AndroidManifest.xml中修改MainActivity启动模式为:singleTop

  1. <activity
  2. android:name=".MainActivity"
  3. android:label="@string/app_name"
  4. android:launchMode="singleTop" >
  5. <intent-filter>
  6. <action android:name="android.intent.action.MAIN" />
  7.  
  8. <category android:name="android.intent.category.LAUNCHER" />
  9. </intent-filter>
  10. </activity>

notification.xml文件源代码:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" >
  5.  
  6. <ImageView
  7. android:id="@+id/imageView"
  8. android:layout_width="50dp"
  9. android:layout_height="50dp"
  10. android:layout_alignParentLeft="true"
  11. android:layout_centerVertical="true"
  12. android:src="@drawable/ic_launcher" />
  13.  
  14. <TextView
  15. android:id="@+id/title"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:layout_above="@+id/text"
  19. android:layout_alignParentRight="true"
  20. android:layout_alignTop="@+id/imageView"
  21. android:layout_marginLeft="18dp"
  22. android:layout_toRightOf="@+id/imageView"
  23. android:gravity="center_vertical"
  24. android:singleLine="true"
  25. android:text="TextView" />
  26.  
  27. <TextView
  28. android:id="@+id/text"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:layout_alignBottom="@+id/imageView"
  32. android:layout_alignLeft="@+id/title"
  33. android:gravity="center_vertical"
  34. android:singleLine="true"
  35. android:text="TextView" />
  36.  
  37. </RelativeLayout>

notification.xml

由于sdk版本的不同,有的需要添加震动的权限:

  1. <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. Unity封装dll教程整理

    ///作者Unity3d师兄---LeroyYang 通过网上大神们的资料以及自己的整理,学习一下用vs2013简单的封装dll文件,方便接口模式下开发,使得逻辑层更为清晰. 操作步骤 1.打开vs2 ...

  2. jQuery阻止事件冒泡的例子

    下面给给各位朋友稍加整理了一jquery中阻止事件冒泡的一些例子,我们知道JQuery 提供了两种方式来阻止事件冒泡,但我们简单的利用它来做一些应用可能不深入或不理解,下面整理了更详细的方法,有兴趣的 ...

  3. Linux中的特殊权限粘滞位(sticky bit)详解

    Linux下的文件权限 在linux下每一个文件和目录都有自己的访问权限,访问权限确定了用户能否访问文件或者目录和怎样进行访问.最为我们熟知的一个文件或目录可能拥有三种权限,分别是读.写.和执行操作, ...

  4. 怒刷DP之 HDU 1029

    Ignatius and the Princess IV Time Limit:1000MS     Memory Limit:32767KB     64bit IO Format:%I64d &a ...

  5. java基础-在dos控制台编写简易的java程序

    第一步:在文件夹中修改隐藏的文件扩展名,让其文件的扩展名全部显示: 第二步:在文件夹中新建一个text文件,将其扩展名属性改为Hello.java的文件扩展名: 第三步:点击右键打开方式用txt文本打 ...

  6. POJ1064

    #include <iostream> #include <iomanip> #include <cmath> using namespace std; int N ...

  7. asp.net中导出excel数据的方法汇总

    1.由dataset生成 代码如下 复制代码 public void CreateExcel(DataSet ds,string typeid,string FileName)    {    Htt ...

  8. C#判断奇偶数的函數

    // 现代流行的"程序员" public static bool IsOdd(int n) { while (true) { switch (n) { : return true; ...

  9. DEEPIN下搭建FTP服务器步骤(备忘录)

    1.打开终端,执行命令[apt-get install vsftpd],安装VSFTPD 2.安装完成后,修改以下配置信息(否则文件无法传输) [echo 'listen=YES'>>/e ...

  10. 应该始终以PreparedStatement代替Statement

    在JDBC应用中,如果你已经是稍有水平开发者,你就应该始终以PreparedStatement代替Statement.也就是说,在任何时候都不要使用Statement 一.代码的可读性和可维护性.虽然 ...