通知栏发送消息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.异步发送+回调函数 下 ...
随机推荐
- Unity封装dll教程整理
///作者Unity3d师兄---LeroyYang 通过网上大神们的资料以及自己的整理,学习一下用vs2013简单的封装dll文件,方便接口模式下开发,使得逻辑层更为清晰. 操作步骤 1.打开vs2 ...
- jQuery阻止事件冒泡的例子
下面给给各位朋友稍加整理了一jquery中阻止事件冒泡的一些例子,我们知道JQuery 提供了两种方式来阻止事件冒泡,但我们简单的利用它来做一些应用可能不深入或不理解,下面整理了更详细的方法,有兴趣的 ...
- Linux中的特殊权限粘滞位(sticky bit)详解
Linux下的文件权限 在linux下每一个文件和目录都有自己的访问权限,访问权限确定了用户能否访问文件或者目录和怎样进行访问.最为我们熟知的一个文件或目录可能拥有三种权限,分别是读.写.和执行操作, ...
- 怒刷DP之 HDU 1029
Ignatius and the Princess IV Time Limit:1000MS Memory Limit:32767KB 64bit IO Format:%I64d &a ...
- java基础-在dos控制台编写简易的java程序
第一步:在文件夹中修改隐藏的文件扩展名,让其文件的扩展名全部显示: 第二步:在文件夹中新建一个text文件,将其扩展名属性改为Hello.java的文件扩展名: 第三步:点击右键打开方式用txt文本打 ...
- POJ1064
#include <iostream> #include <iomanip> #include <cmath> using namespace std; int N ...
- asp.net中导出excel数据的方法汇总
1.由dataset生成 代码如下 复制代码 public void CreateExcel(DataSet ds,string typeid,string FileName) { Htt ...
- C#判断奇偶数的函數
// 现代流行的"程序员" public static bool IsOdd(int n) { while (true) { switch (n) { : return true; ...
- DEEPIN下搭建FTP服务器步骤(备忘录)
1.打开终端,执行命令[apt-get install vsftpd],安装VSFTPD 2.安装完成后,修改以下配置信息(否则文件无法传输) [echo 'listen=YES'>>/e ...
- 应该始终以PreparedStatement代替Statement
在JDBC应用中,如果你已经是稍有水平开发者,你就应该始终以PreparedStatement代替Statement.也就是说,在任何时候都不要使用Statement 一.代码的可读性和可维护性.虽然 ...