傳送門 ☞ 輪子的專欄 ☞ 轉載請註明 ☞ http://blog.csdn.net/leverage_1229
前面我們談到怎樣管理和刪除通知,以及怎樣實現保存用戶預期導航體驗的通知。本文作爲Android平臺Notification系列的最終章,我們將會給通知融入更多DIY的元素,大膽地在這把“倚天劍”上烙下自己的印記^-^。親,那些RPG網遊的鐵匠NPC貌似都有這功能。。。
1顯示進度的通知
通知可以包括一個動畫進度指示器以顯示用戶正在運行的操作的進度狀態。如果你能估計這種操作需要花費多長時間,可以使用“determinate”形式的指示器(一個progress bar)。如果你不能估計花費的時間,那就使用“indeterminate”形式的指示器。
1.1顯示一個固定的時間進度指示器
1.1.1技術要點
調用setProgress()方法添加進度指示器到你的通知中。
1.1.2代碼陳列
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContentInfo(String.valueOf(++progressNum))
.setContentTitle("Picture Download")
.setContentText("Download in progress")
.setDefaults(Notification.DEFAULT_ALL)
.setLargeIcon(icon)
.setSmallIcon(R.drawable.stat_notify_gmail)
.setTicker("Progress Notification")
.setOngoing(true);
// Start a lengthy operation in a background thread
new Thread(new Runnable() {
@Override
public void run() {
int incr;
// Do the "lengthy" operation 20 times
for (incr = 0; incr <= 100; incr+=5) {
builder.setProgress(100, incr, false);
mNotiMgr.notify(PROGRESS_NOTI_ID, builder.build());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Log.d(TAG, "sleep failure");
}
}
builder.setContentText("Download complete")
.setProgress(0, 0, false)
.setOngoing(false);
mNotiMgr.notify(PROGRESS_NOTI_ID, builder.build());
}
}).start();
1.1.3效果展示
1.2顯示一個持續的Activity指示器
1.2.1技術要點
調用setProgress(0, 0, true)添加Activity指示器到你的通知中,前面兩個參數可以忽略。
1.2.2代碼陳列
builder.setProgress(100, incr, false);
mNotiMgr.notify(0, mBuilder.build());
1.2.3效果展示
2自定義樣式的通知
通知框架允許你自定義通知佈局,它在一個RemoteViews對象中定義了通知的佈局。自定義佈局通知和正常的通知類似,它們都是基於一個RemoteViews定義在一個XML佈局文件中。自定義通知的可用高度取決於通知view的佈局。正常view佈局限制爲64dp,展開view佈局限制爲256dp。自定義通知佈局,通過實例化一個RemoteViews對象然後inflates一個xml佈局文件啓動。不再調用setContentTitle()方法,而使用setContent()方法來設置自定義通知的內容細節。使用這個方法在RemoteViews中來設置view子類的值。
2.1技術要點
2.1.1爲通知創建一個單獨的xml佈局文件
2.1.2在應用程序中,使用RemoteViews方法來定義你通知的icon和文本
調用setContent()方法put這個RemoteViews對象到你的NotificationCompat.Builder中。避免正在RemoteViews對象中設置Drawable背景,因爲你的文本顏色可能會變的看不清。
2.2代碼陳列
2.2.1工程包目錄
2.2.2自定義樣式通知創建和發佈方法:showCustomNoti()
/**
* 自定義樣式通知
*/
private void showCustomNoti() {
RemoteViews views = new RemoteViews(getPackageName(), R.layout.custom);
Intent intent = new Intent(INTENT_ACTION);
intent.putExtra("isPlay", true);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
views.setOnClickPendingIntent(R.id.play_pause_music, pendingIntent);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setContent(views)
.setDefaults(Notification.DEFAULT_ALL)
.setLargeIcon(icon)
.setSmallIcon(R.drawable.music_icon)
.setTicker("Custom Notification")
.setOngoing(true);
mNotiMgr.notify(CUSTOM_NOTI_ID, builder.build());
}
2.2.3自定義通知佈局文件:custom.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:gravity="center_vertical" >
<ImageView
android:id="@+id/songer"
android:layout_width="64dp"
android:layout_height="64dp"
android:src="@drawable/songer" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/song_name"
android:textSize="18sp"
android:gravity="center_horizontal" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/songer_name"
android:textSize="14sp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:layout_weight="1" >
<ImageView
android:id="@+id/last_music"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:src="@drawable/music_previous" />
<ImageView
android:id="@+id/play_pause_music"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:src="@drawable/music_play" />
<ImageView
android:id="@+id/next_music"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:src="@drawable/music_next" />
</LinearLayout>
</LinearLayout>
2.3效果展示
2.4源碼下載
3將來的議題
通知的附加操作、通知的優先級。。。