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进度条一直弹出提示及提示音
506 0
|
11月前
|
移动开发 前端开发 Android开发
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
1888 12
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
|
11月前
|
移动开发 JavaScript 应用服务中间件
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
1211 5
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
|
11月前
|
移动开发 Rust JavaScript
【01】首页建立-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【01】首页建立-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
1270 4
【01】首页建立-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
|
12月前
|
开发工具 Android开发
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
1030 11
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
|
11月前
|
移动开发 Android开发
【03】建立隐私关于等相关页面和内容-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【03】建立隐私关于等相关页面和内容-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
510 0
|
Java 开发工具 Maven
【01】完整的安卓二次商业实战-详细的初级步骤同步项目和gradle配置以及开发思路-优雅草伊凡
【01】完整的安卓二次商业实战-详细的初级步骤同步项目和gradle配置以及开发思路-优雅草伊凡
1472 6
|
JavaScript Linux 网络安全
Termux安卓终端美化与开发实战:从下载到插件优化,小白也能玩转Linux
Termux是一款安卓平台上的开源终端模拟器,支持apt包管理、SSH连接及Python/Node.js/C++开发环境搭建,被誉为“手机上的Linux系统”。其特点包括零ROOT权限、跨平台开发和强大扩展性。本文详细介绍其安装准备、基础与高级环境配置、必备插件推荐、常见问题解决方法以及延伸学习资源,帮助用户充分利用Termux进行开发与学习。适用于Android 7+设备,原创内容转载请注明来源。
6398 77
|
安全 数据库 Android开发
在Android开发中实现两个Intent跳转及数据交换的方法
总结上述内容,在Android开发中,Intent不仅是活动跳转的桥梁,也是两个活动之间进行数据交换的媒介。运用Intent传递数据时需注意数据类型、传输大小限制以及安全性问题的处理,以确保应用的健壯性和安全性。
767 11

热门文章

最新文章