android笔记:Notification通知的使用

通知(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:

package com.example.notificationtest;

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;

public class MainActivity extends Activity implements OnClickListener {

    private Button sendNotice;

    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        sendNotice = (Button) findViewById(R.id.send_notice);        sendNotice.setOnClickListener(this);    }

    @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;        }    }

}

NotificationActivity:

package com.example.notificationtest;

import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;

public class NotificationActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notification_layout);
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.cancel(1);
    }

}

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/send_notice"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Send notice"
        />

</LinearLayout>

notification_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="24sp"
        android:text="This is notification layout"
        />

</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.notificationtest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.notificationtest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".NotificationActivity" >
        </activity>

    </application>

</manifest>
时间: 2025-01-07 15:16:09

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

android之Notification通知

android之Notification通知 我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的. [java] view plaincopy package cn.com.chenzheng_java; import android.app.Activity; import android.app.Notification; import android.

Android Train&mdash;notification通知

Notification extends Objectimplements Parcelable java.lang.Object ?android.app.Notification Public Constructors:   比较常用的构造方法 Notification(int icon, CharSequence tickerText, long when) Constructs a Notification object with the information needed to ha

Android笔记:通知

可以在活动里创建,也可以在广播接收器里创建,还可以在服务里创建. NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 创建一个Notification 对象就可以写成:Notification notification = new Notification(R.drawable.icon, "——————This is ticker text—————

Android学习笔记二十之Toast吐司、Notification通知、PopupWindow弹出窗

Android学习笔记二十之Toast吐司.Notification通知.PopupWindow弹出窗 Toast吐司 Toast吐司是我们经常用到的一个控件,Toast是AndroidOS用来显示消息的一种机制,它与Dialog不同,Toast不会获取到焦点,通常显示一段时间之后就会自动消失,下面我们来介绍Toast的几种常用方式: 第一种,默认显示方式,也是最常用的方式: Toast.makeText(MainActivity.this, "这是默认的显示方式", Toast.LE

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

在Android系统中,发一个状态栏通知还是很方便的.下面我们就来看一下,怎么发送状态栏通知,状态栏通知又有哪些参数可以设置? 首先,发送一个状态栏通知必须用到两个类:  NotificationManager . Notification. NotificationManager :  是状态栏通知的管理类,负责发通知.清楚通知等. NotificationManager 是一个系统Service,必须通过 getSystemService()方法来获取. [java] view plainc

Android Notification通知详解

Android Notification通知详解 Notification: (一).简介: 显示在手机状态栏的通知.Notification所代表的是一种具有全局效果的通知,程序一般通过NotificationManager服务来发送Notification. Android3.0增加了Notification.Builder类,该类可以轻松地创建Notification对象. (二).Notification.Builder类中提供的方法: builder.setAutoCancel();

Android Notification通知详细解释

Android Notification通知具体解释 Notification: (一).简单介绍: 显示在手机状态栏的通知. Notification所代表的是一种具有全局效果的通知,程序一般通过NotificationManager服务来发送Notification. Android3.0添加了Notification.Builder类.该类能够轻松地创建Notification对象. (二).Notification.Builder类中提供的方法: builder.setAutoCance

android开发之notification通知完全解析

android开发之notification通知完全解析 本文主要介绍的是notification通知的使用,通过阅读此文,你可以了解,在android开发中,notification通知各种使用方法.本文的notification主要是针对android4.4以下的版本. 现在,我们来看一下,如何实现一个notification.估计大家现在最常用的做法是下面这种: Notification notification = new Notification(R.drawable.ic_launc

从零开始学android&lt;Notification通知.四十四.&gt;

在android中有时会在主界面上收到某些应用的推送,有的可以包含图片,声音或者震动效果,当点击这些提示时,有时还可以进入到发送提示的的应用. 这些提示的推送就是通知,当然通知早根本上也是你一种服务. 首先想要使用通知就必须使用到Notification.Builder 和NotificationManager这两个类 使用Notification.Builder来取得Notification对象,使用NotificationManager来取得操作通知的对象 当然,我们也可以使用RemoteV