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. VIM键盘快捷键映射

    http://www.jianshu.com/p/216811be226b

  2. javascript 字符串方法名调用

    项目中有时候需要通过字符串传递方法名称,供页面调用 var ParameterDefaultCallMethod = Request("ParameterDefaultCallMethod& ...

  3. centos6.4搭建knowlededgeroot-1.0.4知识库平台

    知识库平台选择 http://www.oschina.net/project/tag/320/pkm 最近接到一个任务,要求搭建一个用于部门内部业务知识规范管理和共享的平台,目的是把部门内的FAQ知识 ...

  4. js部分---数组及练习题;

    数据存储--数组: 强类型语言数组 1.同一类型的数据存储的集合,在内存中是连续的 2.定义的时候需要制定长度 弱类型语言数组 1.可以存储任意类型的数据 2.在内存中不连续,不需要制定长度 定义一个 ...

  5. leetcode 93 Restore IP Addresses ----- java

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  6. Codeforces Round #126 (Div. 2)

    A. Cinema 假设当前要的位置为\((x, y)\),如果枚举答案的横坐标,那么每次找离\(y\)最近的纵坐标. 如果占用了位置\((x,y)\),需要要更新第\(x\)行的信息,而占用位置\( ...

  7. scala言语基础学习十

    类型参数 泛型函数 多个参数 使用泛型参数时候,不给类型scala也能自己判断 上边界bounds 下边界bounds 专门用于打包泛型数组

  8. 【P1326】超级教主

    DP优化 原题: LHX教主很能跳,因为Orz他的人太多了.教主跳需要消耗能量,每跳1米就会消耗1点能量,如果教主有很多能量就能跳很高.教主为了收集能量,来到了一个神秘的地方,这个地方凡人是进不来的. ...

  9. C#编写的windows程序随系统启动

    url:http://www.cnblogs.com/emanlee/archive/2009/08/31/1557380.html 设置某程序随系统启动自动运行,取消自动运行. 使用到using M ...

  10. Foundation框架 - 快速创建跨平台的网站页面原型

    API参考:http://foundation.zurb.com/docs/ 作为网页设计和开发人员,我们面临着以下几个严峻的问题: 每天,人们用来上网的设备种类和数量都在不断上升. 为每种设备设计开 ...