今天学习并測试了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. Angular JavaScript内存溢出问题 (FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory)

    方法一和方法二参考:https://www.cnblogs.com/liugang-vip/p/6857595.html 方法一:my-project/node_modules/.bin 下增大内存( ...

  2. IDEA ctrl+alt+L 格式化快捷键无效时解决

    这几天发现自己Intellij IDEA ctrl+alt+L格式化代码无效 设置里面按照快捷键搜索 按了 ctrl+alt+L 也没反应 但是我设置的确实是默认的 ctrl+alt+L 最后终于找到 ...

  3. phpStrom+xdebug调试php

    1>xdebug下载 1.1>xdebug官网可以根据phpinfo()源代码来提供对应版本的xdebug,地址:https://xdebug.org/wizard.php 如下截图 1. ...

  4. 《算法导论》— Chapter 6 堆排序

    序 本文主要介绍堆排序算法(HeapSort),堆排序像合并排序而不像插入排序,堆排序的运行时间为O(nlgn):像插入排序而不像合并排序,它是一种原地(in place)排序算法.在任何时候,数组中 ...

  5. 算法导论 第十三章 红黑树(python)-1插入

    红黑树是上一章二叉搜索树的改进,实现一种平衡 ,保证不会出现二叉树变链表的情况,基本动态集合操作的时间复杂度为O(lgn) 实际用途:c++stl中的set,map是用他实现的 红黑树的性质: 1.每 ...

  6. js如何获取点击<li>标签里的值

  7. NYOJ-116士兵杀敌(二),树状数组~~

    士兵杀敌(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:5 描述 南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的. 小工是南将军手下的军师,南将军经常想知 ...

  8. hihoCoder#1139 二分·二分答案

    原题地址 挺简单一道题,结果因为一时傻逼写错一个小地方,导致WA成狗了_(:з」∠)_ 代码: #include <iostream> #include <cstring> # ...

  9. HDU1213最简单的并查集问题

    题目地址 http://acm.hdu.edu.cn/showproblem.php?pid=1213 #include<iostream> using namespace std; #d ...

  10. bzoj 4736 /uoj274【清华集训2016】温暖会指引我们前行 lct

    [清华集训2016]温暖会指引我们前行 统计 描述 提交 自定义测试 寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 “冻死宝宝了!” 这时 远处的天边出现了 ...