Android使用Notification在狀態(tài)欄上顯示通知
在使用手機(jī)時(shí),當(dāng)有未接來電或者是新短消息時(shí),手機(jī)會給出相應(yīng)的提示信息,這些提示信息通常會顯示到手機(jī)屏幕的狀態(tài)欄上。Android也提供了用于處理此類信息的類,他們是Notification和NotificationManager。其中,Notification代表的是具有全局效果的通知;而NotificationManager則是用于發(fā)送Notification通知的系統(tǒng)服務(wù)。
使用Notification和NotificationManager類發(fā)送和顯示通知也比較簡單,大致可分為以下4個(gè)步驟。
(1)調(diào)用getSystemService()方法獲取系統(tǒng)的NotificationManager服務(wù)。
(2)創(chuàng)建一個(gè)Notification對象,并為其設(shè)置各種屬性
(3)為Notification對象設(shè)置事件信息
(4)通過NotificationManager類的notify()方法發(fā)送Notification通知
下面通過一個(gè)具體的實(shí)例說明如何使用Notification在狀態(tài)欄上顯示通知:
res/layout/main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/layout1"
android:gravity="center_horizontal"
>
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="顯示通知"/>
<Button android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="刪除通知"/>
</LinearLayout>
這個(gè)是點(diǎn)擊通知跳轉(zhuǎn)的頁面main2.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="這里是詳細(xì)內(nèi)容"/> </LinearLayout>
在中AndroidManifest.xml添加一下兩個(gè)權(quán)限,并在<application>標(biāo)簽中注冊ContentActivity:
<!-- 添加操作閃光燈的權(quán)限 --> <uses-permission android:name="android.permission.FLASHLIGHT"/> <!-- 添加操作震動器的權(quán)限 --> <uses-permission android:name="android.permission.VIBRATE"/> <application> <activity android:name=".ContentActivity"/> </application>
MainActivity:
package com.example.test;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
public static int NOTIFYID_1=1,NOTIFYID_2=2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//獲取通知管理器,用于發(fā)送通知
final NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Button button1=(Button) findViewById(R.id.button1);//獲取"顯示通知"按鈕
//為"顯示通知"按鈕添加單擊事件監(jiān)聽器
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Notification notify=new Notification();//創(chuàng)建一個(gè)Notification對象
notify.icon=R.drawable.in;
notify.tickerText="顯示第一個(gè)通知";
notify.when=System.currentTimeMillis();//設(shè)置發(fā)送時(shí)間(設(shè)置為當(dāng)前時(shí)間)
notify.defaults=Notification.DEFAULT_ALL;//設(shè)置默認(rèn)聲音、默認(rèn)震動和默認(rèn)閃光燈
notify.setLatestEventInfo(MainActivity.this, "無題", "每天進(jìn)步一點(diǎn)點(diǎn)", null);//設(shè)置事件信息
notificationManager.notify(NOTIFYID_1,notify);//通過通知管理器發(fā)送通知
//添加第二個(gè)通知
Notification notify1=new Notification(R.drawable.music,"顯示第二個(gè)通知",System.currentTimeMillis());
notify1.flags=Notification.FLAG_AUTO_CANCEL;//打開應(yīng)用程序后圖標(biāo)消失
Intent intent=new Intent(MainActivity.this,ContentActivity.class);//設(shè)置為跳轉(zhuǎn)頁面準(zhǔn)備的Intent
//針對意圖的包裝對象,在下面就是通知被點(diǎn)擊時(shí)激活的組件對象(上下文,請求碼,意圖對象,標(biāo)識符)
PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
//設(shè)置通知的內(nèi)容 (上下文對象,標(biāo)題, 內(nèi)容, 指定通知被點(diǎn)擊的時(shí)候跳轉(zhuǎn)到哪里,激活哪個(gè)組件)
notify1.setLatestEventInfo(MainActivity.this, "通知", "查看詳細(xì)內(nèi)容", pendingIntent);
notificationManager.notify(NOTIFYID_2,notify);//通過通知管理器發(fā)送通知
}
});
Button button2=(Button) findViewById(R.id.button2);//獲取"刪除通知"按鈕
//為"顯示通知"按鈕添加單擊事件監(jiān)聽器
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
notificationManager.cancel(NOTIFYID_1);//清除ID號為常量NOTIFYID_1的通知
notificationManager.cancelAll();//清除全部通知
}
});
}
}
運(yùn)行本實(shí)例,單擊"顯示通知"按鈕,在屏幕的左上角將顯示第一個(gè)通知,如圖-4.2.2.a.jpg所示,過一段時(shí)間后,該通知消失,并顯示第二個(gè)通知,再過一段時(shí)間后,第二個(gè)通知消失,這時(shí)在狀態(tài)欄上將顯示這兩個(gè)通知的圖標(biāo),如圖-4.2.2.b.jpg所示,單擊通知圖標(biāo),將顯示如圖-4.2.2.c.jpg所示的通知列表,單擊第一個(gè)列表項(xiàng),可以查看通知的詳細(xì)內(nèi)容,如圖-4.2.2.d.jpg所示,查看后,該通知的圖標(biāo)將不在狀態(tài)欄中顯示。單擊"刪除通知"按鈕,可以刪除全部通知。
圖-4.2.2.a.jpg:

圖-4.2.2.b.jpg:

圖-4.2.2.c.jpg:

圖-4.2.2.d.jpg:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android使用Notification實(shí)現(xiàn)通知功能
- Android開發(fā)之Notification手機(jī)狀態(tài)欄通知用法實(shí)例分析
- Android中Notification通知用法詳解
- Android 中Notification彈出通知實(shí)現(xiàn)代碼
- Android中使用Notification實(shí)現(xiàn)狀態(tài)欄的通知
- android 通知Notification詳解及實(shí)例代碼
- Android開發(fā)之Notification通知用法詳解
- Android中通知Notification的使用方法
- Android Notification通知使用詳解
相關(guān)文章
Android 坐標(biāo)系與視圖坐標(biāo)系圖解分析
下面小編就為大家?guī)硪黄狝ndroid 坐標(biāo)系與視圖坐標(biāo)系圖解分析。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03
Android APP之WebView校驗(yàn)SSL證書的方法
這篇文章主要介紹了Android APP之WebView校驗(yàn)SSL證書的方法,需要的朋友可以參考下2017-09-09
Android天氣預(yù)報(bào)之基于HttpGet對象解析天氣數(shù)據(jù)的方法
這篇文章主要介紹了Android天氣預(yù)報(bào)之基于HttpGet對象解析天氣數(shù)據(jù)的方法,非常實(shí)用的功能,需要的朋友可以參考下2014-08-08
Android使用gradle讀取并保存數(shù)據(jù)到BuildConfg流程詳解
這篇文章主要介紹了Android使用gradle從資源目錄讀取數(shù)據(jù)并存到BuildConfg內(nèi),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02
Android提高之SurfaceView的基本用法實(shí)例分析
這篇文章主要介紹了Android提高之SurfaceView的基本用法,非常實(shí)用的功能,需要的朋友可以參考下2014-08-08
Android實(shí)現(xiàn)App中導(dǎo)航Tab欄懸浮的功能
相信大家在玩手機(jī)的過程中應(yīng)該會注意到很多的app都有這種功能,比如說外賣達(dá)人常用的“餓了么”。所以這篇文章給大家分享了Android如何實(shí)現(xiàn)app中的導(dǎo)航Tab欄懸浮的功能,有需要的朋友們可以參考借鑒。2016-10-10
Android中activity處理返回結(jié)果的實(shí)現(xiàn)方式
這篇文章主要介紹了Android中activity處理返回結(jié)果的實(shí)現(xiàn)方式,為了實(shí)現(xiàn)這個(gè)功能,Android提供了一個(gè)機(jī)制,跳轉(zhuǎn)到其他activity時(shí),再返回,可以接受到其他activity返回的值,無需再start新的當(dāng)前activity。需要的朋友可以參考下2016-12-12
Android自定義控件實(shí)現(xiàn)簡單寫字板功能
這篇文章主要介紹了Android自定義控件實(shí)現(xiàn)簡單寫字板功能的相關(guān)資料,需要的朋友可以參考下2016-04-04
Android實(shí)現(xiàn)多個(gè)連續(xù)帶數(shù)字圓圈效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)多個(gè)連續(xù)帶數(shù)字圓圈效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07

