Android倚天劍之Notification之動感地帶

來源:互聯網
上載者:User

  上文我們介紹怎樣管理和刪除通知,以及怎樣實現儲存使用者預期導航體驗的通知。本文作為Android平台Notification的最終章,我們將會給通知融入更多DIY的元素,大膽地在這把“倚天劍”上烙下自己的印記^-^。在此之前,先來看下如何在通知中顯示一個進度條。

一、顯示進度的通知

        通知可以包括一個動畫進度列指示器以顯示使用者正在啟動並執行操作的進度狀態。如果你能估計這種操作需要花費多長時間,可以使用“determinate”形式的指標(一個progress bar)。如果你不能估計花費的時間,那就使用“indeterminate”形式的指標。


1.顯示一個固定的時間進度列指示器

(1).技術要點

調用setProgress()方法添加進度列指示器到你的通知中。

(2).代碼陳列

[java] 
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(); 

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();(3).效果展示


2.顯示一個持續的Activity指標

(1).技術要點

調用setProgress(0, 0, true)添加Activity指標到你的通知中,前面兩個參數可以忽略。

(2).代碼陳列

[java] 
builder.setProgress(100, incr, false); 

builder.setProgress(100, incr, false);[java] view plaincopyprint?
mNotiMgr.notify(0, mBuilder.build()); 

mNotiMgr.notify(0, mBuilder.build());(3).效果展示

 

二、自訂樣式的通知

        通知架構允許你自訂通知布局,它在一個RemoteViews對象中定義了通知的布局。自訂布局通知和正常的通知類似,它們都是基於一個RemoteViews定義在一個XML布局檔案中。自訂通知的可用高度取決於通知view的布局。正常view布局限制為64dp,展開view布局限制為256dp。自訂通知布局,通過執行個體化一個RemoteViews對象然後inflates一個xml布局檔案啟動。不再調用setContentTitle()方法,而使用setContent()方法來設定自訂通知的內容細節。使用這個方法在RemoteViews中來設定view子類的值。

1.技術要點

(1).為通知建立一個單獨的xml布局檔案。
(2).在你的應用程式中,使用RemoteViews方法來定義你通知的icon和文本。調用setContent()方法put這個RemoteViews對象到你的NotificationCompat.Builder中。避免正在RemoteViews對象中設定Drawable背景,因為你的文本顏色可能會變的看不清。

2.代碼陳列

工程包目錄

 

自訂樣式通知建立和發布方法:showCustomNoti()

[java]
/**
 * 自訂樣式通知
 */ 
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()); 

    /**
     * 自訂樣式通知
     */
    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());
    }自訂通知布局檔案:custom.xml

[html] 
<?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> 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.