今天学习并測试了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. JOIN和UNION的区别

    join 是两张表根据条件相同的部分合并生成一个记录集. SELECT Websites.id, Websites.name, access_log.count, access_log.dateFRO ...

  2. Java数据结构和算法(三)--三大排序--冒泡、选择、插入排序

    三大排序在我们刚开始学习编程的时候就接触过,也是刚开始工作笔试会遇到的,后续也会学习希尔.快速排序,这里顺便复习一下 冒泡排序: 步骤: 1.从首位开始,比较首位和右边的索引 2.如果当前位置比右边的 ...

  3. golang 解析json 动态数组

    #cat file { "Bangalore_City": "35_Temperature", "NewYork_City": " ...

  4. node的影响及前后端之争

    作者:知乎用户链接:https://www.zhihu.com/question/59578433/answer/326694511来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...

  5. SQL Server查看表的约束

    sysobjects是系统自建的表,里面存储了在数据库内创建的每个对象,包括约束.默认值.日志.规则.存储过程等. SELECT * FROM sysobjects WHERE OBJECT_NAME ...

  6. CF666E Forensic Examination SAM+倍增,线段树和并

    题面: 给你一个串S以及一个字符串数组T[1..m],q次询问,每次问S的子串S[p_l..p_r]在T[l..r]中的哪个串里的出现次数最多,并输出出现次数.如有多解输出最靠前的那一个. 分析: 第 ...

  7. 16. PLUGINS

    16. PLUGINS PLUGINS表提供有关服务器插件的信息. PLUGINS表有以下列: PLUGIN_NAME :用于在诸如INSTALL PLUGIN和UNINSTALL PLUGIN之类的 ...

  8. (二十二)python 3 sort()与sorted()

    Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列 一,最简单的排序 1.使用sort排序 my_list = [3 ...

  9. Apollo配置中心的使用

    1. 自己搭建Apollo配置中心 碰到如下错误: nested exception is org.hibernate.HibernateException: Access to DialectRes ...

  10. 安装SecureCRT注册

    注册方法 1.首先运行压缩包中的安装程序进行安装原版程序!2.安装完成后记得先不要运行程序!3.复制压缩包中的keygen.exe程序到安装目录!4.运行keygen.exe,点击Patch按钮对源程 ...