Android使用Notification實現(xiàn)普通通知欄(一)
Notification是在你的應用常規(guī)界面之外展示的消息。當app讓系統(tǒng)發(fā)送一個消息的時候,消息首先以圖表的形式顯示在通知欄。要查看消息的詳情需要進入通知抽屜(notificationdrawer)中查看。(notificationdrawer)都是系統(tǒng)層面控制的,你可以隨時查看,不限制于app。
Notification的設計:
作為android UI中很重要的組成部分,notification擁有專屬于自己的設計準則。
Notification的界面元素在通知抽屜中的notification有兩種顯示方式,取決于你的android版本以及notificationdrawer的狀態(tài)。
Notification的兩種顯示方式:
(1)普通視圖
這種風格是notification drawer的標準顯示方式。
(2)寬視圖
指你的notification被展開的時候會顯示更大的視圖,這種風格是android4.1之后才有的新特性。
下面我們詳細介紹普通視圖的實現(xiàn):
在圖通視圖中,notification最高64dp,即使你創(chuàng)建了一個寬視圖風格的notification,在未展開的情況下也是以普通大小顯示出來。下面是一個普通的notification。
藍色指示框所代表的的意思如下:
1.標題
2.大圖標
3.通知內容
4.通知數(shù)據(jù)
5.小圖標
6.Notification的發(fā)布時間。
可以通過調用setWhen()設置一個明確的時間,
默認是系統(tǒng)收到該notification的時間。
下面我們是我們本次的演示效果:
本次在普通視圖的基礎上添加了點擊頁面跳轉的效果,可以理解為添加Notification的動作與行為:
雖然這也是可選的,但是你還是應該為你的notification至少添加一種行為:允許用戶通過點擊notification進入一個activity中進行更多的查看或者后續(xù)操作。一個notification可以提供多種動作,而且你也應該讓用戶點擊一個notification之后能總是有相應的響應動作,通常是打開一個activity。你還可以在notification中添加能響應點擊事件的button,比如延遲一下鬧鐘,或者立即回復一條短消息。
在notification內部,一個動作本身是被定義在一個PendingIntent中,PendingIntent包含了一個用于啟動你app中activity的intent。要將PendingIntent和一個手勢聯(lián)系起來,你需要調用合適的NotificationCompat.Builder方法。
比如你想在點擊notification文字的時候啟動activity,你需要調用NotificationCompat.Builder的setContentIntent()來添加PendingIntent。啟動一個activity是notification動作響應中最普遍的一類。
第一步:Layout中的activity_main.xml(僅設置觸發(fā)按鈕):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.administrator.day12.MainActivity"> <Button android:text="顯示通知" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button" android:onClick="show1" /> </LinearLayout>
第二步:Layout中的跳轉頁面activity_content.xml(僅設置顯示文本):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_content" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.administrator.day12.ContentActivity"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:textSize="30sp" android:text="十勝十敗" /> </LinearLayout>
第三步:java(主界面按鈕的點擊事件)實現(xiàn)代碼MainActivity.java:
import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.graphics.BitmapFactory; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.app.NotificationCompat; import android.view.View; import android.widget.RemoteViews; public class MainActivity extends AppCompatActivity { private static final int NO_1 =0x1 ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void show1(View v){ NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.mipmap.guojia); builder.setContentTitle("郭嘉"); builder.setContentText("我們打袁紹吧"); //設置Notification.Default_ALL(默認啟用全部服務(呼吸燈,鈴聲等) builder.setDefaults(Notification.DEFAULT_ALL); //調用NotificationCompat.Builder的setContentIntent()來添加PendingIntent Intent intent = new Intent(this, ContentActivity.class); intent.putExtra("info", "郭嘉給你發(fā)了一個計策!"); PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pi); //獲取Notification Notification n = builder.build(); //通過NotificationCompat.Builder.build()來獲得notification對象自己 NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); //然后調用NotificationManager.notify()向系統(tǒng)轉交 manager.notify(NO_1, n); } }
第四步:java(跳轉后Activity)功能代碼實現(xiàn)ContentActivity.java(只土司):
public class ContentActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_content); //通過獲取MainActivity中設置的putExtra獲取土司內容 Toast.makeText(this, getIntent().getStringExtra("info"), Toast.LENGTH_SHORT).show(); } }
演示效果的代碼就這些,我們梳理下本次實現(xiàn)的思路:
(1)通過按鈕觸發(fā)點擊事件
(2)將notification的一些UI信息以及相關動作賦予NotificationCompat.Builder對象,然后通過NotificationCompat.Builder.build()來獲得notification對象自己;然后調用NotificationManager.notify()向系統(tǒng)轉交這個通知。
(3)在第二步中通過Builder的setContentIntent()來添加PendingIntent,為Notification添加行為,也就是Activity的跳轉
(4)對打開的Activity設置表現(xiàn)的效果。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- android notification 的總結分析
- Android界面 NotificationManager使用Bitmap做圖標
- Android中通知Notification使用實例(振動、燈光、聲音)
- android中創(chuàng)建通知欄Notification代碼實例
- Android中通過Notification&NotificationManager實現(xiàn)消息通知
- Android編程實現(xiàn)攔截短信并屏蔽系統(tǒng)Notification的方法
- Android開發(fā) -- 狀態(tài)欄通知Notification、NotificationManager詳解
- Android中關于Notification及NotificationManger的詳解
- 詳解Android中Notification通知提醒
- 詳解Android中Notification的使用方法
相關文章
Android RecycleView和線型布局制作聊天布局
大家好,本篇文章主要講的是Android RecycleView和線型布局制作聊天布局,感興趣的同學趕緊來看一看吧,對你有幫助的話記得收藏一下2022-01-01詳解Android的MVVM框架 - 數(shù)據(jù)綁定
這篇文章主要介紹了詳解Android的MVVM框架 - 數(shù)據(jù)綁定,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05Android開發(fā)使用Handler實現(xiàn)圖片輪播功能示例
這篇文章主要介紹了Android開發(fā)使用Handler實現(xiàn)圖片輪播功能,涉及Android基于Handler操作圖片的相關實現(xiàn)技巧與操作注意事項,需要的朋友可以參考下2017-09-09