当前位置:操作系统 > 安卓/Android >>

Android学习笔记(一)之Notification

首先新建一个Android工程

然后编辑main.xml

代码如下:

[xhtml] 
<?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" 
    > 
<Button   
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:text="@string/send" 
    android:id="@+id/button" 
    /> 
</LinearLayout> 

再编辑strings.xml

[xhtml] 
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string name="hello">通知</string> 
    <string name="app_name">状态栏通知</string> 
<string name="send">发送通知</string> 
</resources> 

然后就是对其Notification进行处理

 

下面来看一下创建并显示一个Notification的步骤。创建和显 示一个Notification需要如下5步:

1. 通过getSystemService方法获得一个NotificationManager对象。

2. 创建一个Notification对象。每一个Notification对应一个Notification对象。在这一步需要设置显示在屏幕上方状态栏的通知消息、通知消息前方的图像资源ID和发出通知的时间。一般为当前时间。

3. 由于Notification可以与应用程序脱离。也就是说,即使应用程序被关闭,Notification仍然会显示在状态栏 中。当应用程序再次启动后,又可以重新控制这些Notification。如清除或替换它们。因此,需要创建一个PendingIntent对象。该对象由Android系统负责维护,因此,在应用程序关闭后,该对象仍然不会被释放。

4. 使用Notification类的setLatestEventInfo方法设置Notification的详细信息。

5. 使用NotificationManager类的notify方法显示Notification消息。在这一步需要指定标识Notification的唯一ID。这个ID必须相对于同一个NotificationManager对象是唯一的,否则就会覆盖相同ID的Notificaiton。

心动不如行动,下面我们来演练一下如何在状 态栏显示一个Notification,代码如下:

 

[java] 
package com.cayden.notification; 
 
import android.app.Activity; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
/**
 * @author 崔冉
 *
 */ 
public class NotificationActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        Button button=(Button)this.findViewById(R.id.button); 
        button.setOnClickListener(new View.OnClickListener() { 
             
            @Override 
            public void onClick(View v) { 
                // TODO Auto-generated method stub 
                /**
                 * 获取通知管理器
                 */ 
                NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
                int icon=android.R.drawable.sym_action_email; 
                long when=System.currentTimeMillis(); 
                /**
                 * 新建一个通知指定图标和标题
                 */ 
                Notification notification=new Notification(icon,null,when); 
                notification.defaults=Notification.DEFAULT_SOUND;//发出默认声音 
                PendingIntent contenIntent=PendingIntent.getActivity(NotificationActivity.this, 0, null, 0); 
                notification.setLatestEventInfo(NotificationActivity.this, "开会通知", "今天下午四点半在512开会!", contenIntent); 
                 
                 
                notificationManager.notify(0, notification);//发送通知 
                 
            } 
        }); 
    } 

 

 运行效果如图所示:

 

 

 

点击发送通知按钮

后的效果

 

 

 

 

 在左上角多出一个邮件符号.

然后用鼠标选中往下拖

 

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,