Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 實戰教程(三十八):打造自定義樣式的通知 Notification

編輯:關於 Android 程式設計

上一篇小案例,我們完成了一個普通的通知,點擊後能啟動一個 Activity。但那則通知少了點「靚點」。這一篇,我們就來為它加入自定義布局,做出一則樣式精美的自定義通知。

談到應用場景,不妨想想 QQ 音樂:當你點擊播放音樂時,手機上方會浮現一則通知,它不只有專屬布局,還能響應點擊事件,完成上一曲、下一曲的切換。今天這個小案例,就以它為藍本展開。

首先,主活動布局無需改動,仍放置兩個按鈕,分別用於開啟與關閉服務。

主活動中的程式碼則做了較大調整,如下:

 package com.example.notification; 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.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat.Builder; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.RemoteViews; public class MainActivity extends Activity { private Button btShow; private Button btCancel; private NotificationManager manager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btShow = (Button) findViewById(R.id.show); btCancel = (Button) findViewById(R.id.cancel); btShow.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showNotificationNewAPI(); } }); btCancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { cancelNotification(); } }); } public void showNotificationNewAPI(){ manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notification = showCustomViewNotification(); manager.notify(1, notification); } private Notification showCustomViewNotification() { NotificationCompat.Builder builder = new Builder(getApplicationContext()); builder.setSmallIcon(R.drawable.notification_music_playing) .setTicker("ticker") .setWhen(System.currentTimeMillis()) .setOngoing(true) .setContent(getRemoteView()); return builder.build(); } private RemoteViews getRemoteView(){ RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_layout); remoteViews.setTextViewText(R.id.tv_title, "愛你一萬年"); remoteViews.setTextViewText(R.id.tv_arties, "劉德華"); remoteViews.setOnClickPendingIntent(R.id.ll_root, getPaddingIntent()); remoteViews.setOnClickPendingIntent(R.id.ivnext, getNextPaddingIntent()); remoteViews.setOnClickPendingIntent(R.id.ivpre, getPrePaddingIntent()); return remoteViews; } private PendingIntent getPaddingIntent() { Intent intent = new Intent(this, DemoActivity.class); intent.putExtra("msg", "我是從通知欄啟動的!"); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); return pendingIntent; } private PendingIntent getNextPaddingIntent() { Intent intent = new Intent(this, DemoActivity.class); intent.putExtra("msg", "我是從通知欄啟動的!點擊了下一曲按鈕"); PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT); return pendingIntent; } private PendingIntent getPrePaddingIntent() { Intent intent = new Intent(this, DemoActivity.class); intent.putExtra("msg", "我是從通知欄啟動的!點擊了上一曲按鈕"); PendingIntent pendingIntent = PendingIntent.getActivity(this, 2, intent, PendingIntent.FLAG_UPDATE_CURRENT); return pendingIntent; } public void cancelNotification(){ manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.cancel(1); } }

程式碼已經寫得相當詳盡,而其中最核心的莫過於下面這個方法:

 private RemoteViews getRemoteView(){ RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_layout); //設置標題 remoteViews.setTextViewText(R.id.tv_title, "愛你一萬年"); //設置歌手名 remoteViews.setTextViewText(R.id.tv_arties, "劉德華"); //設置點擊事件 remoteViews.setOnClickPendingIntent(R.id.ll_root, getPaddingIntent()); remoteViews.setOnClickPendingIntent(R.id.ivnext, getNextPaddingIntent()); remoteViews.setOnClickPendingIntent(R.id.ivpre, getPrePaddingIntent()); return remoteViews; }

它讓我們得以導入自定義布局,並對布局中的元件進行資料填充與事件綁定,其用意可說是一目瞭然。此外,在建立通知時,setSmallIcon()、setTicker() 與 setWhen() 這三項屬於必填,缺一不可,否則通知根本不會顯示;而 setOngoing(true) 則讓通知無法被滑動刪除。

接著,我們把自定義布局也整理出來:

                

還差最後一環。我們替通知上的按鈕綁定了點擊事件,而事件的目的在於啟動另一個 Activity,因此需要到該 Activity 中取得這些資料,並做一個 Toast 提示。

另一個活動中的程式碼:

 package com.example.notification; import android.app.Activity; import android.os.Bundle; import android.widget.Toast; public class DemoActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.demo); String content = getIntent().getStringExtra("msg"); Toast.makeText(getApplicationContext(), content, 0).show(); } }

事件的最終結果,就是顯示一行 Toast 提示。

一切就緒,跑起來看看這則自定義通知的成果吧!效果圖如下:

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved