今天学习并測试了Notification组件,这个组件在应用中也经经常使用到。在这里写了一个简单的Demo。

Notification是显示在状态栏的消息----位于手机屏幕的最上方。

程序一般通过NotificationManager服务来发送Notification。

Notification发送Notification的步骤

1、调用getSystemService(NOTIFICATION_SERVICE)方法获取系统

NotificationManager服务

2、通过构造器创建一个Notification对象

3、为Notification设置各种属性

4、通过NotificationManerger发送Notification

在AndroidManifest.xml文件里设置权限:

 <!-- 设置闪光灯权限-->
<uses-permission android:name="android.permission.FLASHLIGHT"></uses-permission>
<!--设置震动权限-->
<uses-permission android:name="ANDROID.PERMISSION.VIBRATE"></uses-permission>

xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:id="@+id/startNotify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开启通知"
/>
<Button
android:id="@+id/stopNotify"
android:layout_width="wrap_content"
android:layout_toRightOf="@+id/startNotify"
android:layout_height="wrap_content"
android:text="关闭通知"/>
</RelativeLayout>

MainActivity.java

package lzl.edu.com.notificationtest;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button; public class MainActivity extends Activity implements View.OnClickListener{ private static final int NOTIFICATION = 1;
Button startNotify,stopNotify;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); startNotify =(Button)findViewById(R.id.startNotify);
stopNotify=(Button)findViewById(R.id.stopNotify);
startNotify.setOnClickListener(this);
stopNotify.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.startNotify:
Intent intent = new Intent(MainActivity.this,NextActivity.class);
PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
//创建一个Notification
Notification notification = new Notification();
//为Notification设置图标
notification.icon = R.mipmap.ic_launcher;
//设置文本内容
notification.tickerText="启动还有一个应用的通知";
//设置发送年时间
notification.when = System.currentTimeMillis();
//设置声音
notification.defaults = notification.DEFAULT_SOUND;
//设置默认声音、震动、闪光灯
notification.defaults=notification.DEFAULT_ALL;
notification.setLatestEventInfo(MainActivity.this,"一条应用的通知","点击查看",pi);
//获取系统的NotificationManerger服务
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION,notification);
break;
case R.id.stopNotify:
NotificationManager notificationManager1 = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager1.cancel(NOTIFICATION);
break;
default:break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId(); //noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} return super.onOptionsItemSelected(item);
} }

Notification发送通知的更多相关文章

  1. 发送通知:Notification

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

  2. 向通知栏发送通知点击跳转并传递数据(PendingIntent)传递数据

    // 为发送通知的按钮的点击事件定义事件处理方法 public void send() {///// 第一步:获取NotificationManager NotificationManager nm ...

  3. Android中的通知—Notification 自定义通知

    Android中Notification通知的实现步骤: 1.获取NotificationManager对象NotificationManager的三个公共方法:①cancel(int id) 取消以 ...

  4. Swift - 使用NSNotificationCenter发送通知,接收通知

    转载自:http://www.mamicode.com/info-detail-1069228.html 标签: 1,通知(NSNotification)介绍 这里所说的通知不是指发给用户看的通知消息 ...

  5. NotificationManager 发送通知

    该应用的界面如下,界面代码在此不再给出,源码github账户下载 MainActivity.java public class MainActivity extends Activity { priv ...

  6. Android学习总结(十五) ———— Notification(状态栏通知)基本用法

    一.Notification基本概念  Notification是一种具有全局效果的通知,它展示在屏幕的顶端,首先会表现为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容.我们在用手机的时候 ...

  7. iOS Notification – 远程通知

    本文讲解iOS的远程通知的基本使用,主要包括远程通知的类型,处理远程通知的场景,以及远程通知相关证书的配置等等. 一.APNs简介 APNs是苹果公司提供的远程通知的服务器,当App处于后台或者没有运 ...

  8. iOS之创建通知、发送通知和移除通知的坑

    1.创建通知,最好在viewDidLoad的方法中创建 - (void)viewDidLoad { [super viewDidLoad]; //创建通知 [[NSNotificationCenter ...

  9. iOS 在Host App 与 App Extension 之间发送通知

    如何从你的一个App发送通知给另一个App? (例:搜狗输入法下载皮肤完成后使用皮肤) 注:搜狗输入法是App.而键盘是Extension 当你为你的App 添加 App Extension时,如果想 ...

随机推荐

  1. docker环境安装

    centos7安装docker环境 # step 1: 安装必要的一些系统工具 yum install -y yum-utils device-mapper-persistent-data lvm2 ...

  2. QT_8_Qt中的事件处理_定时器事件_定时器类_事件分发器_事件过滤器_绘图事件_高级绘图事件_绘图设备_QFile 文件读写_QFileInfo文件信息

    Qt中的事件处理 1.1. 捕获QLabel中是鼠标事件 1.2. enterevent 鼠标进入 1.3. leaveevent 鼠标离开 1.4. 鼠标按下MyLabel::mousePressE ...

  3. 树状数组 || POJ 3321 Apple Tree

    一道dfs序+树状数组的题 因为并没有get到dfs序以及对树状数组也不熟练卡了很久orz dfs序: in和out是时间戳 dfs序可以将树转化成为一个序列,满足区间 -> 子树 然后就可以用 ...

  4. JavaSE-31 Java正则表达式

    概述 正则表达式是一个强大的字符串处理工具,可以实现对字符串的查找.提取.分割.替换等操作. String类的几个方法需要依赖正则表达式的支持. 方法 方法说明 boolean matches(Str ...

  5. 关于Java多线程(JAVA多线程实现的四种方式)

    Java多线程实现方式主要有四种:继承Thread类.实现Runnable接口.实现Callable接口通过FutureTask包装器来创建Thread线程.使用ExecutorService.Cal ...

  6. sed替换字符串中的某些字符

    test.txt原文内容 http://jsldfjaslfjsldfjasl/test?jlsdfjsalfjslfd 使用sed替换 sed -ri 's/(http:\/\/)[^\/]*(\/ ...

  7. npm的替代品

    npm安装依赖包太慢,cnpm也快不到哪里去,偶然发现了yarn,特快特好用! 安装yarn:npm install -g yarn 查看版本号:yarn -v 安装依赖项:yarn install

  8. Java POI 导出EXCEL经典实现 Java导出Excel弹出下载框(转载)

    https://blog.csdn.net/evangel_z/article/details/7332535

  9. Redis主从配置与数据备份还原

    一.主从配置: 1.下载: wget http://download.redis.io/releases/redis-4.0.9.tar.gz tar xzf redis-4.0.9.tar.gz c ...

  10. Python3--中括号"[]"与冒号":"在列表中的作用

    先来定义两个列表: liststr = ["helloworld","hahahh","123456"] listnum = [1,2,3, ...