今天学习并測试了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. import downloadjs from 'downloadjs' 如果是自己写的函数 没用默认导出 记得加花括号 例如 import { download } from './data.js'

    import downloadjs from 'downloadjs' 如果是自己写的函数 没用默认导出 记得加花括号 例如 import { download } from './data.js'

  2. hard fault 学习记录

    使用 segger 的 hard fault 的源文件后,当调试时,发生硬件错误的时候,可以查看 HardFaultRegs 中的内容,并对比 segger_HardFaultHandler.c 中的 ...

  3. OpenCV2:第九章 图像比较

    一.简介 图像相似度主要是对两幅图像内容的相似程度进行打分,根据分数的高低来判断图像内容的相似程度. 常见的图像比较有两种方法:峰值信噪比PSNR和结构相似性SSIM 二.峰值信噪比PSNR(Peak ...

  4. 核心动画中的几种layer

    第10章其他有用的层 免责申明(必读!):本博客提供的所有教程的翻译原稿均来自于互联网,仅供学习交流之用,切勿进行商业传播.同时,转载时不要移除本申明.如产生任何纠纷,均与本博客所有人.发表该翻译稿之 ...

  5. vue计算属性computed和methods的区别

    computed和methods的区别 在new Vue的配置参数中的computed和methods都可以处理大量的逻辑代码,但是什么时候用哪个属性,要好好区分一下才能做到正确的运用vue. com ...

  6. Puppeteer-常规操作一

    这里不讲 Puppeteer 怎么使用,主要讲一些常规操作在这里如何通过另类方法实现.等实现后,你就会感觉,嗯~~ 真香! 场景一 已经找出要的元素,现在有需求再继续寻找他的子元素 第一种.将父元素带 ...

  7. WebSocket 学习笔记

    WebSocket 学习笔记 来自我的博客 因为项目原因需要用到双工通信,所以比较详细的学习了一下浏览器端支持的 WebSocket. 并记录一些遇到的问题. 简介 WebSocket 一般是指浏览器 ...

  8. focus,focusin,blur,focusout区别

    focus与focusin 1.共同点:当 <div> 元素或其任意子元素获得焦点时执行事件 2.区别:focus不支持冒泡,而focusin支持冒泡: blur与focusout 1.共 ...

  9. redis:哨兵集群配置

    最少配置1主2从3哨兵 一.引言 上一篇文章我们详细的讲解了Redis的主从集群模式,其实这个集群模式配置很简单,只需要在Slave的节点上进行配置,Master主节点的配置不需要做任何更改,但是有一 ...

  10. HTML5新增的非主体元素header元素、footer元素、hgroup元素、adress元素

    ---恢复内容开始--- header header元素是一种具有引导和导航作用的结构元素,通常用来放置整个页面或页面内的一个内容区块的标题,但是也可以包含其他内容,例如数据表格.搜索表单或相关的lo ...