通知(Notification),当某个应用程序希望向用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现。

发出一条通知后,手机最上方的状态栏中会显示一个通知的图标,下拉状态栏后可以看到通知的详细内容。

《第一行代码》中的Notification的构造方法、setLatestEventInfo等方法已经过时了,但是思想还是一致的。

最新的具体做法如下:

1.需要一个 NotificationManager 来对通知进行管理,可以调用Context 的getSystemService(NOTIFICATION_SERVICE)方法获取到;

2.创建一个 Notification 对象,可通过NotificationCompat的Builder方法来实现.

setTicker()用来显示通知在状态栏的提示信息,

setSmallIcon()指定通知的图标

setContentTitle指定通知的标题,setContentText指定通知内容

setContentIntent指定跳转界面的PendingIntent,PendingIntent 简单地理解为延迟执行的 Intent

3. notify()方法就可以让通知显示出来了。

notify()方法接收两个参数,第一个参数是 id,要保证为每个通知所指定的 id 都是
不同的。第二个参数则是 Notification 对象,这里直接将我们刚刚创建好的 Notification 对象
传入即可。

具体代码如下:

MainActivity:

  1. package com.example.notificationtest;
  2.  
  3. import android.app.Activity;
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.app.NotificationCompat;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
  4.  
  5. public class MainActivity extends Activity implements OnClickListener {
  6.  
  7.     private Button sendNotice;
  8.  
  9.     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            sendNotice = (Button) findViewById(R.id.send_notice);
            sendNotice.setOnClickListener(this);
        }
  10.  
  11.     @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.send_notice:
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        
                Intent intent = new Intent(this, NotificationActivity.class);
                PendingIntent pi = PendingIntent.getActivity(this, 0, intent,
                        PendingIntent.FLAG_CANCEL_CURRENT);
                
                Notification notification = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setTicker("This is ticker text")
                .setContentTitle("This is title")
                .setAutoCancel(true)
                .setContentText("This is content text")
                .setContentIntent(pi)    
                .build();
                
                manager.notify(1, notification); 
                break;
            default:
                break;
            }
        }
  12.  
  13. }
  14.  

NotificationActivity:

  1. package com.example.notificationtest;
  2.  
  3. import android.app.Activity;
  4. import android.app.NotificationManager;
  5. import android.os.Bundle;
  6.  
  7. public class NotificationActivity extends Activity {
  8.  
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.notification_layout);
  13. NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  14. manager.cancel(1);
  15. }
  16.  
  17. }

activity_main.xml:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6.  
  7. <Button
  8. android:id="@+id/send_notice"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:text="Send notice"
  12. />
  13.  
  14. </LinearLayout>

notification_layout.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" >
  5.  
  6. <TextView
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:layout_centerInParent="true"
  10. android:textSize="24sp"
  11. android:text="This is notification layout"
  12. />
  13.  
  14. </RelativeLayout>

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.notificationtest"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6.  
  7. <uses-sdk
  8. android:minSdkVersion="14"
  9. android:targetSdkVersion="17" />
  10.  
  11. <application
  12. android:allowBackup="true"
  13. android:icon="@drawable/ic_launcher"
  14. android:label="@string/app_name"
  15. android:theme="@style/AppTheme" >
  16. <activity
  17. android:name="com.example.notificationtest.MainActivity"
  18. android:label="@string/app_name" >
  19. <intent-filter>
  20. <action android:name="android.intent.action.MAIN" />
  21.  
  22. <category android:name="android.intent.category.LAUNCHER" />
  23. </intent-filter>
  24. </activity>
  25. <activity android:name=".NotificationActivity" >
  26. </activity>
  27.  
  28. </application>
  29.  
  30. </manifest>

android笔记:Notification通知的使用的更多相关文章

  1. Android开发——Notification通知的各种Style详解

    本来是想与之前讲解使用Notification通知使用一起写的,查看了资料,觉得有必要将这Style部分单独拿出来讲解 前篇:Android开发——Notification通知的使用及Notifica ...

  2. Android开发——Notification通知的使用及NotificationCopat.Builder常用设置API

    想要看全部设置的请看这一篇 [转]NotificationCopat.Builder全部设置 常用设置: 设置属性 说明 setAutoCancel(boolean autocancel) 设置点击信 ...

  3. android之Notification通知

    我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的. pac ...

  4. Android笔记:通知

    可以在活动里创建,也可以在广播接收器里创建,还可以在服务里创建. NotificationManager manager = (NotificationManager)getSystemService ...

  5. Android中的通知—Notification 自定义通知

    Android中Notification通知的实现步骤: 1.获取NotificationManager对象NotificationManager的三个公共方法:①cancel(int id) 取消以 ...

  6. Android Notification通知详解

    根据activity的生命周期,在activity不显示时,会执行onStop函数(比如按下home键),所以你在onStop函数(按退出键除外)里面把notification放在通知栏里,再此显示时 ...

  7. Android Notification通知详细解释

    Android Notification通知具体解释  Notification: (一).简单介绍:         显示在手机状态栏的通知. Notification所代表的是一种具有全局效果的通 ...

  8. Android的状态栏通知(Notification)

    通知用于在状态栏显示消息,消息到来时以图标方式表示,如下: 如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息. 1.Layout布局文件: <RelativeLayout xmlns:an ...

  9. 【Android】状态栏通知Notification、NotificationManager详解(转)

    在Android系统中,发一个状态栏通知还是很方便的.下面我们就来看一下,怎么发送状态栏通知,状态栏通知又有哪些参数可以设置? 首先,发送一个状态栏通知必须用到两个类:  NotificationMa ...

随机推荐

  1. Android Studio IDE 简单学习和介绍

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  2. unity, 由scriptableObject创建.asset

    由继承自scriptableObject的类X创建.asset文件. 假设类X的定义为: [System.Serializable] public class X : ScriptableObject ...

  3. SQL2005 遍历表插入

    /* sql2005遍历表(方法1) insert into 数据表(userid,adddate) values((select userid from 用户表),date); */ /*sql20 ...

  4. 使用WebView加载assets下的html文件

    有时候,我们需要将html文件以及所用到的图片都放在 assets/html/ 目录下.然后在页面上通过WebView来显示出来,比如给页面一个默认的显示,这样子看起来效果要好很多.代码如下: pri ...

  5. android学习笔记56——Service

    Service四大组件之一,需要在AndroidMainfest.xml中添加相关配置,运行于后台,不与用户进行交换,没有UI... 配置时可通过<intent-filter.../>元素 ...

  6. struts1&&Hibernate Demo1

    用struts1和Hibernate实现简单登录案例 主要思路:通过用户名name验证 如果一致则验证成功. 附上源码: 1.建立student表,这里使用的是mysql数据库,以下为该表的DDL: ...

  7. 115、定时器(TimerTask+Timer+Handler)

    public class TimerUtils { public static Activity act; public static List<MaiDianModels> listMa ...

  8. 6.3Sum && 4Sum [ && K sum ] && 3Sum Closest

    3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...

  9. 慕课网-安卓工程师初养成-6-3 如何使用 Java 中的数组

    来源:http://www.imooc.com/code/1525 Java 中操作数组只需要四个步骤: 1. 声明数组 语法:  或者  其中,数组名可以是任意合法的变量名,如: 2. 分配空间 简 ...

  10. sql 分组查询及格不及格人数

    select score as 类别,count(*) as 人数 from (select case when fen>=60 then '及格' else '不及格' end as scor ...