zoukankan      html  css  js  c++  java
  • Android应用开发基础篇(2)-----Notification(状态栏通知)

    一、概述

         Notification这个部件的功能是在状态栏里显示消息提醒,比如有未读的短信或者是未接的电话,那么状态栏里都会有显示,更或者是从某个应用(比如QQ,酷我音乐等等)里按Home键回到桌面,这时状态栏里也会显示这个应用的图标,这就是Notification。

     

    二、要求

         程序主界面上有一个Button按钮,当用户点击这个按钮时状态栏会显示一则通知,当按住状态栏下拉时可以看到这个通知在下拉列表里,此时点击这个通知就跳转到另一个界面(相当于查看这个通知)并且能将这个通知在状态栏里取消。

     

    三、实现

          新建工程MyNotice,在/res/layout/main.xml文件里添加一个Button:

     

     <Button
    android:id="@+id/mbutton"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:text
    ="Notice"
    />

    完整的main.xml文件:

     

    复制代码
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="fill_parent"
    android:orientation
    ="vertical" >

    <TextView
    android:layout_width="fill_parent"
    android:layout_height
    ="wrap_content"
    android:text
    ="@string/hello" />

    <Button
    android:id="@+id/mbutton"
    android:layout_width
    ="fill_parent"
    android:layout_height
    ="wrap_content"
    android:text
    ="Notice"
    />

    </LinearLayout>
    复制代码

    修改后的MyNoticeActivity.java文件

    复制代码
     1 package com.nan.notice;
    2
    3 import android.app.Activity;
    4 import android.app.Notification;
    5 import android.app.NotificationManager;
    6 import android.app.PendingIntent;
    7 import android.content.Intent;
    8 import android.os.Bundle;
    9 import android.view.View;
    10 import android.widget.Button;
    11
    12
    13 public class MyNoticeActivity extends Activity
    14 {
    15 //通知的编号
    16 static final int MYNOTICE = 0;
    17
    18 //定义各个对象
    19 private Button mButton = null;
    20 private NotificationManager mNotificationManager = null;
    21 private Intent mIntent = null;
    22 private Notification mNotification = null;
    23 private PendingIntent mPendingIntent = null;
    24
    25
    26 /** Called when the activity is first created. */
    27 @Override
    28 public void onCreate(Bundle savedInstanceState)
    29 {
    30 super.onCreate(savedInstanceState);
    31 setContentView(R.layout.main);
    32
    33 //mButton实例化
    34 mButton = (Button)findViewById(R.id.mbutton);
    35 //mNotificationManager实例化
    36 mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    37
    38 mIntent = new Intent();
    39 //设置要跳转到的Activity
    40 mIntent.setClass(MyNoticeActivity.this, Activity2.class);
    41 //设置点击下拉状态栏列表里的这个通知时所要显示的Activity
    42 mPendingIntent = PendingIntent.getActivity(MyNoticeActivity.this, 0, mIntent, 0);
    43 mNotification = new Notification();
    44 //设置在通知栏里显示的图标
    45 mNotification.icon = R.drawable.ic_launcher;
    46 //设置在通知栏里显示的文本
    47 mNotification.tickerText = "Button 通知...";
    48 //设置通知铃声
    49 mNotification.defaults = Notification.DEFAULT_SOUND;
    50 //设置在下拉状态栏时所显示的关于这个通知的内容
    51 mNotification.setLatestEventInfo(MyNoticeActivity.this, "Button", "Button通知", mPendingIntent);
    52 //设置按钮监听
    53 mButton.setOnClickListener(new View.OnClickListener()
    54 {
    55 @Override
    56 public void onClick(View v)
    57 {
    58 // TODO Auto-generated method stub
    59 //执行这个通知
    60 mNotificationManager.notify(MYNOTICE, mNotification);
    61
    62 }
    63 });
    64
    65 }
    66
    67 }
    复制代码

    在/src里添加一个名为Activity2.java文件:

    复制代码
     1 package com.nan.notice;
    2
    3 import android.app.Activity;
    4 import android.app.NotificationManager;
    5 import android.os.Bundle;
    6
    7
    8 public class Activity2 extends Activity
    9 {
    10 //与MyNoticeActivity.java中定义的值相同
    11 static final int MYNOTICE = 0;
    12
    13 private NotificationManager mNotificationManager = null;
    14
    15 /** Called when the activity is first created. */
    16 @Override
    17 public void onCreate(Bundle savedInstanceState)
    18 {
    19 super.onCreate(savedInstanceState);
    20 setContentView(R.layout.activity2);
    21
    22 mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    23
    24 }
    25
    26 @Override
    27 public void onResume()
    28 {
    29 super.onResume();
    30 //在Activity显示完成后取消在状态栏里的这个通知
    31 mNotificationManager.cancel(MYNOTICE);
    32 }
    33
    34 }
    复制代码

    在/res/layout里添加一个activity2.xml文件

    复制代码
     1 <?xml version="1.0" encoding="utf-8"?>
    2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3 android:layout_width="fill_parent"
    4 android:layout_height="fill_parent"
    5 android:orientation="vertical" >
    6
    7 <TextView
    8 android:layout_width="fill_parent"
    9 android:layout_height="wrap_content"
    10 android:text="Button Notificition"
    11 android:textSize="30px"
    12 />
    13
    14 </LinearLayout>
    复制代码

    在AndroidManifest.xml文件里声明多一个activity

    <activity
    android:name=".Activity2"
    >
    </activity>

    好了,运行程序后,如下

     点击按钮后,可以看到状态栏里显示一个消息:

     按住状态栏然后下拉,可以看到有一条提示:

     

     

     点击这条提示,进入到这条提示的内容,同时状态栏里的这个通知也消失了:

     

     要求完成!

     

  • 相关阅读:
    全屏透明遮罩层
    理解Javascript__理解undefined和null
    JS 对象属性相关--检查属性、枚举属性等
    js 空正则匹配任意一个位置
    a 标签 download 和 target 不配合
    Array.prototype.filter(Boolean)
    页面操作表单不会调用表单 value 属性的 set 函数
    Babel6.x的安装
    html 事件处理程序中的代码在执行时,有权访问全局作用域中的任何代码。
    js 常用 DOM 元素宽高
  • 原文地址:https://www.cnblogs.com/fx2008/p/3140154.html
Copyright © 2011-2022 走看看