當程序並非出在運行狀態的時候,能夠調用Notification來顯示通知。java
一、建立android
Notification的建立主要涉及到三個類:NotificationManager,Notification和PendingIntentui
NotificationManager主要是對通知進行管理。this
Notification類主要用於對Notification的一些屬性進行定義。得到的方式主要是經過兼容各個版本的support-v4中的NotificationCompat類來得到。spa
PendingIntent類能夠理解成一個延遲執行的intent。code
下面是一個實例代碼:blog
public static final int NOTIFICATIONID = 0x101; private NotificationManager notificationManager; private NotificationCompat.Builder builder; private Notification notification; private PendingIntent pendingIntent; notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); builder = new NotificationCompat.Builder(NotificationManiActivity.this,null); Intent intent = new Intent(NotificationManiActivity.this,NotificationTestActivity.class); pendingIntent = PendingIntent.getActivity(NotificationManiActivity.this,0,intent,0); notification =builder .setContentTitle("通知標題") .setContentText("通知內容") .setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher)//Notification何時顯示 .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)) .setContentIntent(pendingIntent) .build(); notificationManager.notify(NOTIFICATIONID,notification);
二、取消ip
取消的方式有兩種,分別爲:get
a、調用setAutoCancel方法,點擊之後,Notification會自動消失it
b、調用notificationManager.cancel方法,根據notificationId來取消Notification
實例代碼以下:
/** * 取消方式一 * 調用setAutoCancel方法,點擊之後,Notification會自動消失 * @param view */ public void oneCancel(){ notification =builder .setContentTitle("通知標題") .setContentText("通知內容") .setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)) .setContentIntent(pendingIntent) .setAutoCancel(true)//點擊之後,Notification會自動消失 .build(); notificationManager.notify(NOTIFICATIONID,notification); } /** * 取消方式二 * 調用notificationManager.cancel方法,根據notificationId來取消Notification * @param view */ public void twoCancel(){ notificationManager.cancel(NOTIFICATIONID);//根據notificationId來取消Notification }
三、技巧
3.1 Notificationt帶聲音
/** * notificationt帶聲音 * @param view */ public void sound(){ notification =builder .setContentTitle("notificationt帶聲音") .setContentText("notificationt帶聲音") .setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)) .setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luma.ogg")))//設置Notification建立的時候的提示音 .setContentIntent(pendingIntent) .build(); notificationManager.notify(NOTIFICATIONID,notification); }
3.2 Notificationt帶震動
/** * notificationt帶震動 * @param view */ public void vibrate(View view){ notification =builder .setContentTitle("notificationt帶震動") .setContentText("notificationt帶震動") .setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)) .setContentIntent(pendingIntent) //設置震動的頻率,小標爲0的靜止的時長,下標爲1的表示振動的時長,小標爲2的靜止的時長,以此類推 .setVibrate(new long[]{0,1000,1000,1000}) .build(); notificationManager.notify(NOTIFICATIONID,notification); }
3.3 Notificationt帶LED燈光
/** * Notificationt帶LED燈光 * @param view */ public void led(View view){ notification =builder .setContentTitle("Notificationt帶LED燈光") .setContentText("Notificationt帶LED燈光") .setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)) .setContentIntent(pendingIntent) //LED燈光顏色,LED燈光亮起時長,LED燈光暗去時長 .setLights(Color.GREEN,1000,1000) .build(); notificationManager.notify(NOTIFICATIONID,notification); }
3.4 Notificationt帶bigText
/** * Notificationt帶bigText * @param view */ public void bigText(View view){ notification =builder .setContentTitle("Notificationt帶bigText") .setStyle(new NotificationCompat.BigTextStyle().bigText("Notificationt帶bigTextNotificationt帶bigTextNotificationt帶" + "bigTextNotificationt帶bigTextNotificationt帶bigTextNotificationt帶"+ "bigTextNotificationt帶bigTextNotificationt帶bigTextNotificationt帶bigT" + "extNotificationt帶bigTextNotificationt帶bigTextNotificationt帶bigText" + "Notificationt帶bigTextNotificationt帶bigText")) .setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)) .setContentIntent(pendingIntent) .build(); notificationManager.notify(NOTIFICATIONID,notification); }
效果圖爲:
注意:這個是模擬器的效果,對於部分真機,則直接不顯示文字內容
3.5 Notificationt帶bigPicture
notification =builder .setContentTitle("Notificationt帶bigPicture") .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher_round))) .setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)) .setContentIntent(pendingIntent) .build(); notificationManager.notify(NOTIFICATIONID,notification);
注意:這個對模擬器有效果,對於部分真機,則直接不顯示文字內容
3.6 Notificationt帶priority
/** * Notificationt帶priority * @param view */ public void priority(View view){ notification =builder .setContentTitle("Notificationt帶priority") .setContentText("Notificationt帶priority") .setPriority(NotificationCompat.PRIORITY_MAX) .setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)) .setContentIntent(pendingIntent) .build(); notificationManager.notify(NOTIFICATIONID,notification); }
優先級的參數從低到高分別爲:PRIORITY_DEFAULT表示默認的重要程度,和不設置效果是同樣的。PRIORITY_MIN表示最低的重要程度,系統只會在特定的場景中才會顯示這條通知,好比用戶下拉狀態欄的時候;PRIORITY_LOW表示較低的重要程度,系統可能會將這類通知縮小,或者是改變其顯示的順序(有一部分手機會修改顯示順序),並將其排在更重要的通知以後;PRIORITY_HIGH表示較重的重要程度,系統可能會將這類通知放大,或者是改變其顯示順序,排在比較靠前的位置;PRIORITY_MAX表示最重要的通知,有一些手機上面會直接顯示成一個彈窗的形式,有一些仍是改變顯示順序。
四、自定義部分
參考地址:https://developer.android.com/training/notify-user/custom-notification#java