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的更多相关文章

  1. Android学习笔记之JSON数据解析

    转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...

  2. 【转】Pro Android学习笔记(九八):BroadcastReceiver(2):接收器触发通知

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.sina.com.cn/flowingflying或作者@恺风Wei-傻瓜与非傻瓜 广播接 ...

  3. 【转】 Pro Android学习笔记(七七):服务(2):Local Service

    目录(?)[-] Local service代码 调用Local ServiceLocal Service client代码 AndroidManifestxml定义Serviceacitivty的l ...

  4. 【转】Pro Android学习笔记(十二):了解Intent(下)

    解析Intent,寻找匹配Activity 如果给出component名字(包名.类名)是explicit intent,否则是implicit intent.对于explicit intent,关键 ...

  5. Android 学习笔记之Volley(七)实现Json数据加载和解析...

    学习内容: 1.使用Volley实现异步加载Json数据...   Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...

  6. Android学习笔记进阶之在图片上涂鸦(能清屏)

    Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...

  7. android学习笔记36——使用原始XML文件

    XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...

  8. Ext.Net学习笔记22:Ext.Net Tree 用法详解

    Ext.Net学习笔记22:Ext.Net Tree 用法详解 上面的图片是一个简单的树,使用Ext.Net来创建这样的树结构非常简单,代码如下: <ext:TreePanel runat=&q ...

  9. udacity android 学习笔记: lesson 4 part b

    udacity android 学习笔记: lesson 4 part b 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...

随机推荐

  1. 使用Jmeter测试MySQL性能——(2)多客户端配置

    在测试性能过程中,单个测试客户端可能存在性能瓶颈无法达到测试要求的压力.在这种情况下,可以设置jmeter的多客户端模式,然后通过一台控制端,同时控制多台PC上的客户端向服务器发送测试请求.若有4台P ...

  2. easyUI分页显示

    struts2: http://www.cnblogs.com/huozhicheng/archive/2011/09/27/2193605.html springMVC http://blog.cs ...

  3. HDU 4686 矩阵快速幂 Arc of Dream

    由式子的性质发现都是线性的,考虑构造矩阵,先有式子,a[i] = ax * a[i-1] + ay; b[i] = bx*b[i-1] +by; a[i]*b[i] = ax*bx*a[i-1]*b[ ...

  4. 《jave程序设计》第一周学习总结

    <JAVA程序设计>第一周学习总结 教材学习内容总结 在第一周的学习中,我初次认识了JAVA程序的一些基础知识.我发现了很多问题 问题就是自己的代码编写出现了一点问题,符号和单词的大小写都 ...

  5. jsonp跨域js

    http://www.cnblogs.com/yuzhongwusan/archive/2012/12/11/2812849.html window.opener用法 http://www.cnblo ...

  6. PHP pdao用法总结

    $sql = 'SELECT name, colour, calories     FROM fruit     WHERE calories < :calories AND colour =  ...

  7. OpenCV CommandLineParser 的用法

    OpenCV CommandLineParser 的用法 去百度了一下,关键字:OpenCV CommandLineParser  发现,最多的讲解是:opencv源码解析之(5):CommandLi ...

  8. Compiler ,Interpreter, Linker

    https://en.wikipedia.org/wiki/Interpreter_(computing) https://en.wikipedia.org/wiki/Compiler https:/ ...

  9. Unity脚本系统

    什么是脚本?脚本是一个 MonoBehavior, 继承关系是 MonoBehavior -> Behavior -> Component -> Object GameObject ...

  10. Wireshark-BPF过滤规则

    设置过滤规则就是让网络设备只是捕获我们感兴趣的网络数据包,如果没有设置过滤规则,即上面的 filter_app 是空字符串,那么网络设备就捕获所有类型的数据包,否则只是捕获过滤规则设置的数据包,此时过 ...