android开发(11) 消息栏通知(Notification)

android 的消息通知还是很方便的,它会出现在窗体的顶部,并给出提示。常见的短信就是这样的通知方式。本文我们尝试实现一个这样的演示。

演示截图:

实现步骤:

1.获得NotificationManager 对象,这是一个通知管理器。我们在窗体里调用方法获得

NotificationManager manager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE);

   getSystemService是获得系统服务的方法。android以服务的形式提供给用户操作接口。也就是说,我们要想操作 通知相关的操作接口,就先获得系统提供的 “通知管理器”

   NotificationManager 对象就是一个服务管理器了。

2.构建一个Notification 对象,这个Notification 对象描述了:通知的标题和内容,通知要调用的窗体。

//构建一个通知对象,指定了 图标,标题,和时间
Notification notification = new Notification(R.drawable.icon, "通知", System.currentTimeMillis());

//设定事件信息

notification.setLatestEventInfo(getApplicationContext(),
                         "通知标题"
                         
"通知显示的内容"
                         pendingIntent);


 

 notification.flags |= Notification.FLAG_AUTO_CANCEL;  // 自动终止
 notification.defaults  |=  Notification.DEFAULT_SOUND;  // 默认声音

 

其中pendingIntent对象 是一个跳转intent,当提示后,点击在消息提示栏的 “通知”时,能打开一个窗体activity

PendingIntent pendingIntent = PendingIntent.getActivity( 
                         ActNotifyDemo.
this
                         
0
                         
new Intent(ActNotifyDemo.this,ActNotifyDemo.class), //指定一个跳转的intent
                         
0
                 );

实际上是PendingIntent  包含(封装)了一个跳转的 intent对象。

 

3.调用NotificationManager.notify方法发起通知,发起后的通知就会在消息栏提示。

-----------------

代码如下:

public   class  ActNotifyDemo  extends  Activity {
    Button _btn1;
    
    
/**  Called when the activity is first created.  */
    @Override
    
public   void  onCreate(Bundle savedInstanceState) {
        
super .onCreate(savedInstanceState);
        setContentView(R.layout.main);

 
        _btn1  =  (Button)findViewById(R.id.button1);
        _btn1.setOnClickListener(
new  OnClickListener(){
            
// 触发通知
             public   void  onClick(View arg0) {
                
// 获得通知管理器
                 NotificationManager manager  =  (NotificationManager) getSystemService(
                         Context.NOTIFICATION_SERVICE); 
                 
// 构建一个通知对象
                 Notification notification  =   new  Notification(R.drawable.icon, 
                            
" 通知 " , System.currentTimeMillis()); 

                 PendingIntent pendingIntent 
=  PendingIntent.getActivity( 
                         ActNotifyDemo.
this
                         
0
                         
new  Intent(ActNotifyDemo. this ,ActNotifyDemo. class ),
                         
0
                 );
                 
                 notification.setLatestEventInfo(getApplicationContext(),
                         
" 通知标题 "
                         
" 通知显示的内容 "
                         pendingIntent);
                 notification.flags
|= Notification.FLAG_AUTO_CANCEL;  // 自动终止
                 notification.defaults  |=  Notification.DEFAULT_SOUND;  // 默认声音
                 manager.notify( 0 , notification); // 发起通知
            }
        });
    }
}

 

源代码下载

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值