Notification发送通知
今天学习并測试了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发送通知的更多相关文章
- 发送通知:Notification
Intent的主要功能是完成一个Activity跳转到其他Activity或者是Service的操作,表示的是一种 操作的意图. PendingIntent表示的是暂时执行的一种意图,是一种在产生某一 ...
- 向通知栏发送通知点击跳转并传递数据(PendingIntent)传递数据
// 为发送通知的按钮的点击事件定义事件处理方法 public void send() {///// 第一步:获取NotificationManager NotificationManager nm ...
- Android中的通知—Notification 自定义通知
Android中Notification通知的实现步骤: 1.获取NotificationManager对象NotificationManager的三个公共方法:①cancel(int id) 取消以 ...
- Swift - 使用NSNotificationCenter发送通知,接收通知
转载自:http://www.mamicode.com/info-detail-1069228.html 标签: 1,通知(NSNotification)介绍 这里所说的通知不是指发给用户看的通知消息 ...
- NotificationManager 发送通知
该应用的界面如下,界面代码在此不再给出,源码github账户下载 MainActivity.java public class MainActivity extends Activity { priv ...
- Android学习总结(十五) ———— Notification(状态栏通知)基本用法
一.Notification基本概念 Notification是一种具有全局效果的通知,它展示在屏幕的顶端,首先会表现为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容.我们在用手机的时候 ...
- iOS Notification – 远程通知
本文讲解iOS的远程通知的基本使用,主要包括远程通知的类型,处理远程通知的场景,以及远程通知相关证书的配置等等. 一.APNs简介 APNs是苹果公司提供的远程通知的服务器,当App处于后台或者没有运 ...
- iOS之创建通知、发送通知和移除通知的坑
1.创建通知,最好在viewDidLoad的方法中创建 - (void)viewDidLoad { [super viewDidLoad]; //创建通知 [[NSNotificationCenter ...
- iOS 在Host App 与 App Extension 之间发送通知
如何从你的一个App发送通知给另一个App? (例:搜狗输入法下载皮肤完成后使用皮肤) 注:搜狗输入法是App.而键盘是Extension 当你为你的App 添加 App Extension时,如果想 ...
随机推荐
- 【整理】iview中刷新页面的时候更新导航菜单的active-name
iview中刷新页面的时候更新导航菜单的active-name https://blog.csdn.net/lhjuejiang/article/details/83212070
- Linux常用命令大全2
Linux命令是对Linux系统进行管理的命令.对于Linux系统来说,无论是中央处理器.内存.驱动.键盘.鼠标,还是用户等都是文件,Linux命令是它正常运行的核心.接下来,就来看看xp系统下载编辑 ...
- ios7与ios6UI风格区别
http://apple.xdnice.com/content/applenews/2013/0614/142195.html (ios7 ui风格) http://blog.csdn ...
- 第二讲:vcs debugging basics
要求: 1.describe three methods of debugging verilog code using vcs 2.invoke ucli debugger(不重要) 3.debug ...
- Python-接口自动化(十一)
配置文件的作用(十一) (十二)配置文件 1.python当中有一个模块可以读取配置文件里面的信息:configparser,对这个模块进行导入之后就可以使用了,import configparser ...
- 关于linux安装kettle的总结
一.部署准备 1.1 JDK安装配置 命令行键入“cd /etc”进入etc目录 命令行键入“vi profile”打开profile文件 敲击键盘ctrl+F到文件末尾 在末尾处,即第一个~的地方, ...
- python直接赋值、深浅拷贝实例剖析
根据数据类型分为两部分进行剖析: int.str类型 list.tuple.dict类型等 1. int.str类型 [int类型实例] >>> import copy ...
- DEV Express中ImageCollection的使用
1, ImageCollection作为窗体组件的一种,位于Components分类下,拖进窗体以后,显示在界面的底部. 2, 注意ImageCollection的Imag ...
- tarjan 学习记
1.强连通分量是什么 强连通分量指的是部分点的集合如果能够互相到达(例如 1→3,3→2,2→1(有向图)这种,132每个点都能互相抵达) 或者说,有一个环,环上点的集合就是一个强连通分量 2.那怎么 ...
- 跟初学者学习IbatisNet第一篇
写在前面的话:我自己也是一个初学者,写这个专题只是为了对学过知识的巩固,如果有什么不对的地方,欢迎大家指正…………………… 第一篇就简单介绍一下什么是IbatisNet,然后写一个简单的Demo,在后 ...