Android 中的notify 机制
来源:互联网 发布:网络能上网不能看视频 编辑:程序博客网 时间:2024/06/16 00:20
大家都熟悉这样的一个场景:就是来短信的时候,手机上方会跳出一个短信的图标来提示你来新的信息了,然后你在上方拖下来就会看到短信息,点进去之后就能进到阅读短信的页面。这个流程一整套的完成就是android中的notify机制,下面我们一起来看看android中的notify机制,主要包含三个类:
1. NotificationManager:
通知管理器,我们就理解其实一个通知消息的管理者就可以了,整个系统就一个,通过getSystemService()函数来获得,它负责管理发送通知、清除通知等一系列的对通知的管理工作。
2.Notification :
通知本身,可以设置通知的铃声,震动方式等,通知点进去之后需要启动的应用(通过PendingIntent传递)
3:PendingIntent:
字面意思上来理解的话时悬挂起来的intent,intent的代表意图,这个叫悬起来的意图,解释不通。我的理解是这样:PendingIntent主要是为了授予其它的应用启动某个activity的权利,比如说:在短信程序里面收到的消息会发出一个通知,在通知栏里面你点击的时候会启动阅读短信的那个view(一般情况下是不行的,需要通过startActiviy等方式来做,这里是不需要的),怎么做到的呢?就是PendingIntent做到得,因为他在短信程序中利用PendingIntent方式赋予了再通知栏中点击启动短信view的权限。
下面来看代码:
NotifytestActivity.java
package com.huawei.android.notify_1;import android.app.Activity;import android.os.Bundle;import android.widget.Button;import android.app.NotificationManager;public class NotifytestActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main2); } /** * 启动该activity之后就应该清除通知标记 */@Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); NotificationManager nNotificaitonMan = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);nNotificaitonMan.cancel(0); } }NotifytestActivity2.java
package com.huawei.android.notify_1;import android.app.Activity;import android.view.View;import android.widget.Button;import android.os.Bundle;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;public class NotifytestActivity2 extends Activity {private Button mButton1 = null;private Button mButton2 = null;//声明消息管理器NotificationManager mNotifyManager = null;Intent mIntent = null;PendingIntent mPendIntent = null;//声明notify对象Notification mNotify = null;@Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); //初始化notification 对象 mNotifyManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); //获取4个按钮对象 mButton1 = (Button)findViewById(R.id.btn_1); mButton2 = (Button)findViewById(R.id.btn_2); //当点击的时候转移内容 mIntent = new Intent(NotifytestActivity2.this,NotifytestActivity.class); //设置点击时候显示内容的类, mPendIntent = PendingIntent.getActivity(NotifytestActivity2.this,0,mIntent,0); //构造notification对象 mNotify = new Notification(); mButton1.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){ //设置通知在状态栏显示的图标 mNotify.icon = R.drawable.icon; //当我们点击通知时显示的内容 mNotify.tickerText = "来新的通知啦~~~"; //通知时发出的声音 mNotify.defaults = Notification.DEFAULT_SOUND; //设置通知显示的参数 mNotify.setLatestEventInfo(NotifytestActivity2.this, "Button1", "Button1通知进行中", mPendIntent); //执行这个通知事件的跳转 mNotifyManager.notify(0, mNotify); } }); /** * 清楚通知,mNotifyManager.cancel(0)的参数0是mNotifyManager.notify(0, mNotify);里面第一个参数,也就是notify的ID,这个在系统中是唯一的。 * 这里是做测试用的,在系统中应该是点击了通知之后该通知图标就消失了。可以看NotifytestActivity中的onStart()中的处理方式。 */ mButton2.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){ mNotifyManager.cancel(0); } }); }}
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /><Button android:id="@+id/btn_1" android:layout_width="150px" android:layout_height="wrap_content" android:text="@string/button_1"/><Button android:id="@+id/btn_2" android:layout_width="150px" android:layout_height="wrap_content" android:text="@string/button_2"/></LinearLayout>
main2.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /></LinearLayout>
运行结果如下:
1.点击发送通知之后:
按下上方的状态栏拖下:
3.点击该通知后(从该通知进入到启动的那个activity是PendingIntent的功劳哦)
- Android 中的notify 机制
- Android 中的notify 机制
- Android 中的notify 机制
- Android 中的notify 机制
- java 中的 wait/notify 同步锁机制
- notify机制
- Java wait notify在android中的应用
- notify -- Linux 2.6 内核中的文件系统变化通知机制
- notify -- Linux 2.6 内核中的文件系统变化通知机制
- Android多线程设计模式之-wait()和notify()机制
- Android多线程设计模式之-wait()和notify()机制
- Android多线程设计模式---wait()和notify()机制
- Android应用开发之wait()和notify()机制
- Android多线程设计模式之-wait()和notify()机制
- Android多线程设计模式---wait()和notify()机制
- android-多线程设计wait、notify、notifyall、synchronized的使用机制
- android-多线程设计wait、notify、notifyall、synchronized的使用机制
- android-多线程设计wait、notify、notifyall、synchronized的使用机制
- java向数据库写入汉字
- 2.6内核编译配置选项简介--介绍make menuconfig中的每个选项含义
- No input file specified
- 在虚拟上安装Android X86(二)
- About Data Alignment(关于数据对齐)
- Android 中的notify 机制
- linux nfs
- 虚拟机中安装Android-x86(一)
- jQuery获取Select选择的Text和 Value(转)
- 黑马程序员--类和结构之间的区别
- Struts 2的基石——拦截器(Interceptor)
- Android利用NDK实现C++开发
- Java中抽象类和接口的区别
- 布局篇-StackPanel布局