Notification 与状态栏信息

21 篇文章 0 订阅

新建一个Android工程

然后编辑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"
    >
<Button  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/send"
    android:id="@+id/button"
    />
</LinearLayout>


再编辑strings.xml

<?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对象是唯一的,否则就会覆盖相同IDNotificaiton

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

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 York
 *
 */
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);//发送通知
				
			}
		});
    }
}

运行效果如图所示:

 

 

点击发送通知按钮

后的效果

 

 

 

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

然后用鼠标选中往下拖




参考内容:

http://blog.csdn.net/cuiran/article/details/5731513

http://www.cnblogs.com/nokiaguy/archive/2010/07/13/1776190.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值