android学习笔记22——Notification
Notification
==>
Notification是显示在手机状态栏的消息,位于手机屏幕的最上方;
一般显示手机当前网络、电池状态、时间等;
Notification所代表的是一种全局效果的通知,程序一般通过NotificationManager服务来发送Notification。
应用程序可通过NotificationManager向系统发送全局通知;
使用Notification发送Notification,操作步骤:
1.调用getSystemService(NoTIFICATION_SERVICE)方法获取系统的NotificationManager服务;
2.通过构造器创建一个Notification对象;
3.为Notification设置各种属性;
4.通过NotificationManager发送Notification
实例:
布局文件==》
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/btnone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="one" /> <Button
android:id="@+id/btntwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="two" /> </LinearLayout> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/btnone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="otehr" /> <Button
android:id="@+id/btntwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="other" /> </LinearLayout> 代码实现==》
package com.example.mynotification; import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity
{
final int NotificationId = 1; @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button btnone = (Button) this.findViewById(R.id.btnone);
Button btntwo = (Button) this.findViewById(R.id.btntwo); btnone.setOnClickListener(new OnClickListener()
{
@SuppressWarnings("deprecation")
@Override
public void onClick(View v)
{
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
PendingIntent pintent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
// 创建Notification
Notification notify = new Notification();
// 为Notification设置图标,该图标显示在状态栏
notify.icon = R.drawable.ele;
// 为Notification设置文本内容, 该文本显示在状态栏
notify.tickerText = "启动其他程序通知";
// 设置Notification发送时间
notify.when = System.currentTimeMillis();
// 为Notification设置默认声音、默认震动、默认闪关灯
notify.defaults = Notification.DEFAULT_SOUND;
// 设置事件信息
notify.setLatestEventInfo(MainActivity.this, "Notification通知", "Notification查看", pintent);
// 获取系统NotificationManager服务
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(NotificationId,notify);
}
}); btntwo.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.cancel(NotificationId);
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} } package com.example.mynotification; import android.app.Activity;
import android.os.Bundle; public class OtherActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
}
}
权限设置、Activity添加==》
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mynotification"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<!-- 添加操作权限 -->
<!-- 添加闪光灯操作权限 -->
<uses-permission android:name="android.permission.FLASHLIGHT" />
<!-- 添加操作振动器的操作权限 -->
<uses-permission android:name="android.permission.VIBRATE" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.mynotification.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.mynotification.OtherActivity"
android:label="other acitivity" />
</application> </manifest>
注意:
以上程序中粗体内容为Notification设置各种属性,包括图标、标题、发送时间。
也可以通过以下方式实现:Notification notify= new Notification(R.drawable.image,"启动Activity的通知",System.currentTimeMillis());
以上程序,还通过defaults属性Notification设置了声音提示、震动提示、闪光灯等,该属性支持的属性如下所示:
1.DEFAULT_SOUND:设置使用默认声音;
2.DEFAULT_VIBRATE:设置使用默认震动;
3.DEFAULT_LIGHTS:设置使用默认闪光灯;
4.ALL:设置使用默认声音、震动、闪光灯;
如果不想使用默认设置,也可使用代码进行设置:
//设置自定义声音
notify.sound=Uril.parse("file://sdcard//music.mp3");
//设置自定义震动
notify.vibrate = new Long[]{0,50,100,150};
//设置闪光灯颜色为红色
notify.ledARGB=Color.Red;
//设置闪光灯多少毫秒后熄灭
notify.ledoffms=800ms;
//设置闪光灯多少毫秒后开启
notify.ledOnms=800ms;
注意:添加新的Activity需要在AndroidMainfest.xml添加对应配置,需要操作系统设置,也需要在AndroidMainfest.xml添加对应的权限配置才可正常使用。
运行效果:

android学习笔记22——Notification的更多相关文章
- Android学习笔记之JSON数据解析
转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...
- 【转】Pro Android学习笔记(九八):BroadcastReceiver(2):接收器触发通知
文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.sina.com.cn/flowingflying或作者@恺风Wei-傻瓜与非傻瓜 广播接 ...
- 【转】 Pro Android学习笔记(七七):服务(2):Local Service
目录(?)[-] Local service代码 调用Local ServiceLocal Service client代码 AndroidManifestxml定义Serviceacitivty的l ...
- 【转】Pro Android学习笔记(十二):了解Intent(下)
解析Intent,寻找匹配Activity 如果给出component名字(包名.类名)是explicit intent,否则是implicit intent.对于explicit intent,关键 ...
- Android 学习笔记之Volley(七)实现Json数据加载和解析...
学习内容: 1.使用Volley实现异步加载Json数据... Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...
- Android学习笔记进阶之在图片上涂鸦(能清屏)
Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...
- android学习笔记36——使用原始XML文件
XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...
- Ext.Net学习笔记22:Ext.Net Tree 用法详解
Ext.Net学习笔记22:Ext.Net Tree 用法详解 上面的图片是一个简单的树,使用Ext.Net来创建这样的树结构非常简单,代码如下: <ext:TreePanel runat=&q ...
- udacity android 学习笔记: lesson 4 part b
udacity android 学习笔记: lesson 4 part b 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...
随机推荐
- 解决f.lux总是弹框定位
解决f.lux总是弹框定位,直接导入成功定位的注册表文件即可. 以下保存为f.lux.reg 双击导入即可. Windows Registry Editor Version 5.00 [HKEY_CU ...
- Java-->多线程复制(文件指针)
--> 这里用到两种方法...其实也不算两种,就一点点不一样而已... ---> Test 测试类 package com.dragon.java.multithreadcopy; imp ...
- 2016 ACM/ICPC Asia Regional Shenyang Online 1003/HDU 5894 数学/组合数/逆元
hannnnah_j’s Biological Test Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...
- left join测试验证之一
$ sqlite3 a.dbSQLite version 3.8.1 2013-10-17 12:57:35Enter ".help" for instructionsEnter ...
- boot/setup.S
!! setup.S Copyright (C) 1991, 1992 Linus Torvalds!! setup.s is responsible for getting th ...
- PHP设置图片文件上传大小的具体实现方法
PHP默认的上传限定是最大2M,想上传超过此设定的文件,需要调整PHP.apache等的一些参数 我们简要介绍一下PHP文件上传涉及到的一些参数: •file_uploads :是否允许通过HTTP上 ...
- Ubuntu安装Mysql+Django+MySQLdb
安装Mysql sudo apt-get install mysql-server mysql-client root@IdeaPad:/# mysql -u root -p Enter passwo ...
- netsh winsock reset
最近要在虚拟机上安装 git ,然后托管一个项目,搞得我是焦头烂额.今天下午我卸载了vmware workstation ,但是奇怪的事就发生了,我的google chrome 浏览器没法浏览网页了. ...
- GOOGLE的专业使用方法(转)
搜索引擎命令大全! 1.双引号 把搜索词放在双引号中,代表完全匹配搜索,也就是说搜索结果返回的页面包含双引号中出现的所有的词,连顺序也必须完全匹配.bd和Google 都支持这个指令.例如搜索: “s ...
- MySQL-python模块
1. Python 操作 Mysql 模块的安装 linux: pip install MySQL-python 或 yum -y install MySQL-python windows: exe ...