編輯:關於Android編程
1、Notification 是顯示在手機狀態欄的通知——手機狀態欄位於手機屏幕的最上方那裡一般顯示了手機當前網絡狀態、電池狀態、時間等。
2、Notification 代表的是一種具有全局效果的通知,程序一般通過NotificationManager 服務來發送Notification。
3、NotificationManager 是一個重要的系統服務,位於應用程序框架層,應用程序可通過NotificationManager 向系統發送全局通知。
4、通過Notification.Builder 類允許開發者更輕松地創建Notification對象。
5、Notification.Builder 提供的常用方法如下:
①setDefaults() ===> 設置通知LED 燈、音樂、震動等
該方法支持的屬性值:
DEFAULT_SOUND:設置使用默認聲音
DEFAULT_VIBRATE:設置使用默認震動
DEFAULT_LIGHTS:設置使用默認閃光燈
ALL:設置使用默認聲音、震動、閃光燈
②setAutoCancel() ===> 設置點擊通知後,狀態欄自動刪除通知
③setContentTitle() ===> 設置通知標題
④setContentText() ===> 設置通知內容
⑤setSmalllcon() ===> 為通知設置圖標
⑥setLargelcon() ===> 為通知設置大圖標
⑦setTick() ===> 設置通知在狀態欄的提示文本
⑧setContentlntent() ===> 設置點擊通知後將要啟動的程序組件對應的Pendinglntent
6、發送Notification 的步驟:
①調用getSystemService(NOTIFICATION_SERVICE) 方法獲取系統的NotificationManager 服務
②通過構造器創建一個Notification 對象
③為Notification 設置各種屬性
④通過NotificationManager 發送Notification 的功能和用法
7、訪問系統功能須加權限
8、要啟動另一個頁面,別忘了在AndroidManifest.xml 文件中聲明該Activity
android:name=.OtherActivity
android:label=@string/other_activity>
9、具體看如下代碼
package org.crazyit.ui;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
public class NotificationTest extends Activity {
private static final int NOTIFICATION_ID = 0x123;
private NotificationManager nm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 獲取系統的NotificationManager服務
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
// 為發送通知的按鈕的點擊事件定義事件處理方法
public void send(View source) {
// 創建一個啟動其他Activity的Intent
Intent intent = new Intent(NotificationTest.this, OtherActivity.class);
// 單擊Notification 通知時將會啟動Intent 對應的程序,實現頁面的跳轉
PendingIntent pi = PendingIntent.getActivity(NotificationTest.this, 0, intent, 0);
Notification notify = new Notification.Builder(this)
// 設置打開該通知,該通知自動消失
.setAutoCancel(true)
// 設置顯示在狀態欄的通知提示信息
.setTicker(有新消息)
// 設置通知的圖標
.setSmallIcon(R.drawable.notify)
// 設置通知內容的標題
.setContentTitle(一條新通知)
// 設置通知內容
.setContentText(恭喜你,您加薪了,工資增加20%!)
// // 設置使用系統默認的聲音、默認LED燈
// .setDefaults(Notification.DEFAULT_SOUND
// |Notification.DEFAULT_LIGHTS)
// 設置通知的自定義聲音
.setSound(
Uri.parse(android.resource://org.crazyit.ui/
+ R.raw.msg))
.setWhen(System.currentTimeMillis())
// 設改通知將要啟動程序的Intent
.setContentIntent(pi)
.getNotification();
// 發送通知
nm.notify(NOTIFICATION_ID, notify);
}
// 為刪除通知的按鈕的點擊事件定義事件處理方法
public void del(View v) {
// 取消通知
nm.cancel(NOTIFICATION_ID);
}
}
Intent Filter匹配規則
Intent簡介Android中提供了Intent機制來協助應用間的交互與通訊,Intent負責對應用中一次操作的動作、動作涉及數據、附加數據進行描述,Android則根
Android Design之Android4.0不一樣的裡程碑
想成為Android的傑出開發工程師,不懂得Android的設計規則怎麼可以,Android4.0問世後谷歌公司為Android程序員規范了一系列的設計原則,不要再盲目的
Android CTS測試
一什麼是CTS CTS簡介:Compatibility Test suite系列兼容測試 google定義了一個兼容性規范(Compatible Definition),
Android異步任務類分析
一、為什麼要使用異步任務類? Handler模式需要為每一個任務創建一個新的線程,任務完成後通過Handler對象向UI線程發送消息,完成界面的更新, 這種方式對整