Android之Notification介绍

简介: Notification就是在桌面的状态通知栏。这主要涉及三个主要类: Notification:设置通知的各个属性。 NotificationManager:负责发送通知和取消通知 Notification.Builder:Notification内之类,创建Notification对象。

Notification就是在桌面的状态通知栏。这主要涉及三个主要类:

Notification:设置通知的各个属性。

NotificationManager:负责发送通知和取消通知

Notification.BuilderNotification内之类,创建Notification对象。非常方便的控制所有的flags,同时构建Notification的风格。

主要作用:

1.创建一个状态条图标。

2.在扩展的状态条窗口中显示额外的信息(和启动一个Intent)。

3.闪灯或LED

4.电话震动。

5.发出听得见的警告声(铃声,保存的声音文件)。

Notification是看不见的程序组件(Broadcast ReceiverService和不活跃的Activity)警示用户有需要注意的事件发生的最好途径

下面主要介绍这三个类:

一、NotificationManager

这个类是这三个类中最简单的。主要负责将Notification在状态显示出来和取消。主要包括5个函数:void cancel(int id),void cancel(String tag, int id), void cancelAll(),void notify(int id, Notificationnotification),notify(String tag, int id, Notification notification)

看看这五个函数就知道这个类的作用了。但是在初始化对象的时候要注意:

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

二、Notification

设置这个类主要是设置Notification的相关属性。初始化

Notification n = new Notification();

Notification里面有很多属性下面选择几个常用的介绍一下

icon  这个是设置通知的图标。像QQ的小企鹅

sound  这个是设置来通知时的提示音。

tickerText  设置提示的文字。

vibrate     来通知时振动。

when       设置来通知时的时间

flag     这个很有意思是设置通知在状态栏显示的方式。它的值可以设置为虾米这些值:

FLAG_NO_CLEAR 将flag设置为这个属性那么通知栏的那个清楚按钮就不会出现

FLAG_ONGOING_EVENT 将flag设置为这个属性那么通知就会像QQ一样一直在状态栏显示

DEFAULT_ALL  将所有属性设置为默认

DEFAULT_SOUND  将提示声音设置为默认

DEFAULT_VIBRATE  将震动设置为默认

三、Notification.Builder

这个类一般用于管理Notification,动态的设置Notification的一些属性。即用set来设置。也没啥好说的。

转自 http://blog.csdn.net/zqiang_55/article/details/7032025

StatusBarService:

package com.example.androidstatusbarnotification;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.util.Log;

public class StatusBarService extends IntentService {

    private final static String TAG = "MainActivity";

    public StatusBarService() {
        super("StatusBarService");
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.i(TAG, "开始下载");
        showNotification(false);
        try {
            Thread.sleep(10000);
            showNotification(true);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Log.i(TAG, "下载完成");
    }

    private void showNotification(boolean finish) {
        Notification notification = new Notification();
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent conIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        if (!finish) {
            notification.icon = R.drawable.ic_launcher;
            notification.tickerText = "开始下载";
            notification.setLatestEventInfo(this, "下载", "正在下载中……", conIntent);
        } else {
            notification.icon = R.drawable.ic_launcher;
            notification.tickerText = "下载完成";
            notification.setLatestEventInfo(this, "下载", "程序下载完毕", conIntent);
        }

        notification.defaults = Notification.DEFAULT_SOUND;// 添加声音提示
        // 下边的两个方式可以添加音乐
        // notification.sound =
        // Uri.parse("file:///sdcard/notification/ringer.mp3");
        // notification.sound =
        // Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
        // audioStreamType的值必须AudioManager中的值,代表着响铃的模式
        notification.audioStreamType = android.media.AudioManager.ADJUST_LOWER;
        manager.notify(R.layout.activity_main, notification);
    }

}

 

布局:

<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"
    tools:context=".MainActivity" >

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

    <Button
        android:id="@+id/btnStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始下载" />

</LinearLayout>

 

MainActivity:

package com.example.androidstatusbarnotification;

import android.os.Bundle;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    private final static String TAG = "MainActivity";
    private Button btnStart = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnStart = (Button) this.findViewById(R.id.btnStart);
        btnStart.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(MainActivity.this, StatusBarService.class);
                startService(intent);//开始下载服务
            }
        });
    }

    /* (non-Javadoc)
     * @see android.app.Activity#onStart()
     */
    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        manager.cancel(R.layout.activity_main);//此处ID取页面的,进入页面后取消提示
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

 

目录
相关文章
|
Android开发
Android 中Notification进度条一直弹出提示及提示音
Android 中Notification进度条一直弹出提示及提示音
234 0
|
21天前
|
Java Android开发
Android 开发获取通知栏权限时会出现两个应用图标
Android 开发获取通知栏权限时会出现两个应用图标
12 0
|
1月前
|
XML 缓存 Android开发
Android开发,使用kotlin学习多媒体功能(详细)
Android开发,使用kotlin学习多媒体功能(详细)
96 0
|
1月前
|
设计模式 人工智能 开发工具
安卓应用开发:构建未来移动体验
【2月更文挑战第17天】 随着智能手机的普及和移动互联网技术的不断进步,安卓应用开发已成为一个热门领域。本文将深入探讨安卓平台的应用开发流程、关键技术以及未来发展趋势。通过分析安卓系统的架构、开发工具和框架,本文旨在为开发者提供全面的技术指导,帮助他们构建高效、创新的移动应用,以满足不断变化的市场需求。
18 1
|
1月前
|
机器学习/深度学习 调度 Android开发
安卓应用开发:打造高效通知管理系统
【2月更文挑战第14天】 在移动操作系统中,通知管理是影响用户体验的关键因素之一。本文将探讨如何在安卓平台上构建一个高效的通知管理系统,包括服务、频道和通知的优化策略。我们将讨论最新的安卓开发工具和技术,以及如何通过这些工具提高通知的可见性和用户互动性,同时确保不会对用户造成干扰。
33 1
|
12天前
|
XML 开发工具 Android开发
构建高效的安卓应用:使用Jetpack Compose优化UI开发
【4月更文挑战第7天】 随着Android开发不断进化,开发者面临着提高应用性能与简化UI构建流程的双重挑战。本文将探讨如何使用Jetpack Compose这一现代UI工具包来优化安卓应用的开发流程,并提升用户界面的流畅性与一致性。通过介绍Jetpack Compose的核心概念、与传统方法的区别以及实际集成步骤,我们旨在提供一种高效且可靠的解决方案,以帮助开发者构建响应迅速且用户体验优良的安卓应用。
|
14天前
|
Java Android开发
Android开发之使用OpenGL实现翻书动画
本文讲述了如何使用OpenGL实现更平滑、逼真的电子书翻页动画,以解决传统贝塞尔曲线方法存在的卡顿和阴影问题。作者分享了一个改造后的外国代码示例,提供了从前往后和从后往前的翻页效果动图。文章附带了`GlTurnActivity`的Java代码片段,展示如何加载和显示书籍图片。完整工程代码可在作者的GitHub找到:https://github.com/aqi00/note/tree/master/ExmOpenGL。
19 1
Android开发之使用OpenGL实现翻书动画
|
14天前
|
Android开发 开发者
Android开发之OpenGL的画笔工具GL10
这篇文章简述了OpenGL通过GL10进行三维图形绘制,强调颜色取值范围为0.0到1.0,背景和画笔颜色设置方法;介绍了三维坐标系及与之相关的旋转、平移和缩放操作;最后探讨了坐标矩阵变换,包括设置绘图区域、调整镜头参数和改变观测方位。示例代码展示了如何使用这些方法创建简单的三维立方体。
12 1
Android开发之OpenGL的画笔工具GL10