Android之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;
    }

}

 

posted @ 2014-03-16 17:45  欢醉  阅读(926)  评论(0编辑  收藏  举报