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 ...
随机推荐
- ubuntu13 安装并配置ssh,交换密钥
1.下载安装sudo apt-get install ssh,若出现问题,可选择安装openssh-server,或ssh-server等 2.启动ssh,命令:sudo /etc/init.d/ss ...
- 一次性编译所有T-Code
SGEN, 然后选择Generate all,或regenerate ...... 转的,My question: 这个不太懂能干什么
- 319. Bulb Switche
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...
- .net中创建xml文件
//创建空的XML文档 XmlDocument xmldoc = new XmlDocument(); xmldoc.LoadXml("<?xml version='1.0' enco ...
- 精妙无比 8款HTML5动画实例及源码
1.jQuery垂直带小图标菜单导航插件 今天我们要来分享一款jQuery菜单插件,这款jQuery菜单是垂直的样式,鼠标滑过菜单项时会出现一个背景,菜单项的右侧也会出现一个小箭头.另外值得注意的是, ...
- [USACO11JAN]利润Profits
题目描述 The cows have opened a new business, and Farmer John wants to see how well they are doing. The ...
- Codeforces Round #114 (Div. 2)
Codeforces Round #114 (Div. 2) 代码 Codeforces Round #114 (Div. 2) C. Wizards and Trolleybuses 思路 每条车的 ...
- IOS中如果使用RGB实现背景色
在开发的过程中.我们往往会碰到图片很多的情况.这时候我们的程序打包就会变得很大.一些纯色的图片可以用RGB来实现.这样可以减少内存的占用MAC本中有数码测色计这个功能.通过这个我们可以获得图片的RGB ...
- 【BZOJ1009】【HNOI2008】GT考试
依旧看人代码写,不过我觉得自己慢慢写一个也可以写成? 原题: 阿申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字.他的不 ...
- Android Studio 常用快捷键以及设置
常用快捷键: Ctrl+Q 出现文档提示 跟ecplise的 鼠标悬浮差不多 Ctrl+Alt+t 包围代码 Home End 移动光标到文本首和文本尾 Alt+回车 导入当前包 Ctrl+Alt+O ...