- 浏览: 65862 次
- 性别:
- 来自: Mercury
最新评论
Android系统的状态栏(Status Bar)中有一个创新UI设计,这就是可以下拉的通知提示。当系统有一些消息要通知用户时,例如,收到短信、电子邮件、有未接来电时,都会把信息作为通知(Notification)发送给用户。
实例:
Main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/shownotification" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="显示通知" /> <Button android:id="@+id/deletenotification" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="清除通知" /> </LinearLayout>
NotificationActivity.java
package com.android.activity; 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.View; import android.view.View.OnClickListener; import android.widget.Button; public class NotificationActivity extends Activity { private Button showNotification = null; private Button deleteNotification = null; //Notification管理器 private NotificationManager nm = null; private static final int NOTIFICATION_ID = 123456; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //创建 NotificationManager,其中创建的nm 对象负责发出与取消 Notification nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); showNotification = (Button)findViewById(R.id.shownotification); deleteNotification = (Button)findViewById(R.id.deletenotification); showNotification.setOnClickListener(new ShowNotificationListener()); deleteNotification.setOnClickListener(new DeletedNotificationListener()); } class ShowNotificationListener implements OnClickListener{ public void onClick(View v) { showNotification(R.drawable.image,"图标边的文字","标题","内容"); } } class DeletedNotificationListener implements OnClickListener{ public void onClick(View v) { //表示当用户点击 Clear 之后,能够清除该通知。 nm.cancel(NOTIFICATION_ID); } } public void showNotification(int icon,String tickertext, String title,String content){ //创建 Notification ,参数依次为:icon的资源id,在状态栏上展示的滚动信息,时间。 Notification notification = new Notification(icon,tickertext,System.currentTimeMillis()); //这是设置通知是否同时播放声音或振动,声音为Notification.DEFAULT_SOUND //振动为Notification.DEFAULT_VIBRATE; //Light为Notification.DEFAULT_LIGHTS //全部为Notification.DEFAULT_ALL //如果是振动或者全部,必须在AndroidManifest.xml加入振动权限 notification.defaults = Notification.DEFAULT_ALL; //创建一个Intent,该Intent使得当用户点击该通知后发出这个Intent //请注意,如果要以该Intent启动一个Activity,一定要设置 Intent.FLAG_ACTIVITY_NEW_TASK 标记。 //Intent.FLAG_ACTIVITY_CLEAR_TOP :如果在当前Task中,有要启动的Activity,那么把该Acitivity之前的所有Activity都关掉,并把此Activity置前以避免创建Activity的实例 //Intent.FLAG_ACTIVITY_NEW_TASK :系统会检查当前所有已创建的Task中是否有该要启动的Activity的Task,若有,则在该Task上创建Activity,若没有则新建具有该Activity属性的Task,并在该新建的Task上创建Activity Intent intent = new Intent(this,NotificationActivity.class); //PendingIntent 为Intent的包装,这里是启动Intent的描述,PendingIntent.getActivity 返回的PendingIntent表示,此PendingIntent实例中的Intent是用于启动 Activity 的Intent。PendingIntent.getActivity的参数依次为:Context,发送者的请求码(可以填0),用于系统发送的Intent,标志位 PendingIntent pt = PendingIntent.getActivity(this, 0,intent, 0); notification.setLatestEventInfo(this,title,content,pt); //启动Notification,参数依次为:在你的程序中标识Notification的id值(用来区分同一程序中的不同Notifycation,如果程序中只有一个Notification那么这里随便你填什么都可以,不过类型必须要为int),要通知的Notification nm.notify(NOTIFICATION_ID, notification); } }
运行结果:
文字消失后:
清除通知后:
发表评论
文章已被作者锁定,不允许评论。
-
Android40_Dialog
2011-11-14 00:11 2939Dialog是Android常用的对话框控件。AlertDia ... -
Android39_Clock和TimePicker
2011-11-14 00:08 2314一、AnalogClock和DigitalClock ... -
Android38_ImageView和Gallery
2011-11-14 00:07 3553一、ImageView使用方法 ImageVi ... -
Android37_JSON数据解析
2011-11-08 00:14 2300一、JSON介绍 JSON(JavaSc ... -
Android36_Animations使用(四)
2011-11-08 00:14 3375一、LayoutAnimationsContrlller ... -
Android35_Animations使用(三)
2011-11-08 00:13 2607一、AnimationSet的具体使用方法 ... -
Android34_Animations使用(二)
2011-11-08 00:12 1915在代码中使用Animations可以很方便的调试、运行 ... -
Android33_Animations使用(一)
2011-11-08 00:12 2242一、Animations介绍 Anima ... -
Android31_AppWidget使用(二)
2011-11-05 00:09 2456一、PendingIntent介绍 PendingIn ... -
Android30_AppWidget使用(一)
2011-11-05 00:08 2222一、App Widget定义 App ... -
Android29_SeekBar和RatingBar
2011-11-02 23:21 2062一、使用SeekBar步骤: SeekB ... -
Android28_ExpandableListActivity
2011-11-02 23:21 1453ExpandableListActivity就是可扩展的 ... -
Android27_AutoCompleteTextView
2011-11-02 23:21 1024一、创建AutoCompleteTextView ... -
Android26_DatePicker
2011-11-02 23:20 1741一、DatePicker和DatePickerDialo ... -
Android25_Spinner使用方法
2011-11-02 23:20 2768一、创建Spinner的步骤 1.在布局 ... -
Android24_Service初步
2011-10-18 22:27 961一、Service概念 ... -
Android23_Socket编程
2011-10-18 22:19 1464一、什么是Socket Socket是基 ... -
Android22_WIFI网络操作
2011-10-18 22:12 1648一、什么是WIFI WIFI就是一种无线 ... -
Android21_广播机制(二)
2011-10-18 22:00 957一、注册BroadcastReceiver的方法 ... -
Android20_广播机制(一)
2011-10-18 21:48 1040一、Android广播机制介绍 Android:操作系统 ...
相关推荐
一个关于安卓toast和notification使用方法的Android源码,来自eoeandroid社区,对初学者或许是个帮助吧。
这个项目整合了Android平台所有Notification的使用方法,有些使用场景不生效,不知道是否是我使用方法有问题。
一个关于安卓toast和notification使用方法的Android源码
了解Android Notification的基本用法
简单介绍Android Notification用法 详细介绍NotificationManageService和StatusBarManageService与App层交互过程
Android Notification消息框 Toast弹出框用法演示范例,本例中关于 Toast弹出框的演示,演示了适时的 Toast和长时间的 Toast,关于Notification的定义,则演示了高级Notification的用法,自定义4种Notification的...
本文实例讲述了Android开发之Notification通知用法。分享给大家供大家参考,具体如下: 根据activity的生命周期,在activity不显示时,会执行onStop函数(比如按下home键),所以你在onStop函数(按退出键除外)里面把...
android:label="通知使用权测试程序" android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"> <action android:name="android.service.notification.NotificationListenerService"/> ...
Android Notification使用详解 Notification 核心代码(链式调用):适用于Android 4.0以上(不兼容低版本) Notification noti = new Notification.Builder(this) .setContentTitle(标题名称) .setContentText...
主要介绍了Android中Notification的用法汇总的相关资料,需要的朋友可以参考下
本文实例讲解了通知Notification使用方法,此知识点就是用作通知的显示,包括振动、灯光、声音等效果,分享给大家供大家参考,具体内容如下 效果图: MainActivity: import java.io.File; import android....
主要介绍了Android中Notification用法,以实例形式较为详细的分析并总结了Notification的功能与使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
主要介绍了Android Notification 使用方法详解的相关资料,这里提供实例来帮助大家理解掌握这部分内容,需要的朋友可以参考下
Notification最新用法、实现Notification的通知栏常驻、Notification的各种样式big View、解决Notification点击无效
本文实例讲述了Android编程自定义Notification的用法。分享给大家供大家参考,具体如下: Notification是一种让你的应用程序在不使用Activity的情况下警示用户,Notification是看不见的程序组件警示用户有需要注意的...
Android Notification通知 简单使用 demo下载。 解决问题:兼容高低版本,帮助用户打开通知开关,android5.x手机状态栏icon是白块问题。 详细文章请参考 ...
本文实例讲述了Android中Notification用法。分享给大家供大家参考,具体如下: Notification可以理解为通知的意思一般用来显示广播信息 用Notification就必须要用到NotificationManager 想用Notification一般有三个...
本篇文章主要介绍了Android之Notification的多种用法实例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧