Android使用Notification實(shí)現(xiàn)普通通知欄(一)
Notification是在你的應(yīng)用常規(guī)界面之外展示的消息。當(dāng)app讓系統(tǒng)發(fā)送一個(gè)消息的時(shí)候,消息首先以圖表的形式顯示在通知欄。要查看消息的詳情需要進(jìn)入通知抽屜(notificationdrawer)中查看。(notificationdrawer)都是系統(tǒng)層面控制的,你可以隨時(shí)查看,不限制于app。
Notification的設(shè)計(jì):
作為android UI中很重要的組成部分,notification擁有專屬于自己的設(shè)計(jì)準(zhǔn)則。
Notification的界面元素在通知抽屜中的notification有兩種顯示方式,取決于你的android版本以及notificationdrawer的狀態(tài)。
Notification的兩種顯示方式:
(1)普通視圖
這種風(fēng)格是notification drawer的標(biāo)準(zhǔn)顯示方式。
(2)寬視圖
指你的notification被展開的時(shí)候會(huì)顯示更大的視圖,這種風(fēng)格是android4.1之后才有的新特性。
下面我們?cè)敿?xì)介紹普通視圖的實(shí)現(xiàn):
在圖通視圖中,notification最高64dp,即使你創(chuàng)建了一個(gè)寬視圖風(fēng)格的notification,在未展開的情況下也是以普通大小顯示出來。下面是一個(gè)普通的notification。

藍(lán)色指示框所代表的的意思如下:
1.標(biāo)題
2.大圖標(biāo)
3.通知內(nèi)容
4.通知數(shù)據(jù)
5.小圖標(biāo)
6.Notification的發(fā)布時(shí)間。
可以通過調(diào)用setWhen()設(shè)置一個(gè)明確的時(shí)間,
默認(rèn)是系統(tǒng)收到該notification的時(shí)間。
下面我們是我們本次的演示效果:

本次在普通視圖的基礎(chǔ)上添加了點(diǎn)擊頁面跳轉(zhuǎn)的效果,可以理解為添加Notification的動(dòng)作與行為:
雖然這也是可選的,但是你還是應(yīng)該為你的notification至少添加一種行為:允許用戶通過點(diǎn)擊notification進(jìn)入一個(gè)activity中進(jìn)行更多的查看或者后續(xù)操作。一個(gè)notification可以提供多種動(dòng)作,而且你也應(yīng)該讓用戶點(diǎn)擊一個(gè)notification之后能總是有相應(yīng)的響應(yīng)動(dòng)作,通常是打開一個(gè)activity。你還可以在notification中添加能響應(yīng)點(diǎn)擊事件的button,比如延遲一下鬧鐘,或者立即回復(fù)一條短消息。
在notification內(nèi)部,一個(gè)動(dòng)作本身是被定義在一個(gè)PendingIntent中,PendingIntent包含了一個(gè)用于啟動(dòng)你app中activity的intent。要將PendingIntent和一個(gè)手勢(shì)聯(lián)系起來,你需要調(diào)用合適的NotificationCompat.Builder方法。
比如你想在點(diǎn)擊notification文字的時(shí)候啟動(dòng)activity,你需要調(diào)用NotificationCompat.Builder的setContentIntent()來添加PendingIntent。啟動(dòng)一個(gè)activity是notification動(dòng)作響應(yīng)中最普遍的一類。
第一步:Layout中的activity_main.xml(僅設(shè)置觸發(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中的跳轉(zhuǎn)頁面activity_content.xml(僅設(shè)置顯示文本):
<?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="十勝十?dāng)? /> </LinearLayout>
第三步:java(主界面按鈕的點(diǎn)擊事件)實(shí)現(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("我們打袁紹吧");
//設(shè)置Notification.Default_ALL(默認(rèn)啟用全部服務(wù)(呼吸燈,鈴聲等)
builder.setDefaults(Notification.DEFAULT_ALL);
//調(diào)用NotificationCompat.Builder的setContentIntent()來添加PendingIntent
Intent intent = new Intent(this, ContentActivity.class);
intent.putExtra("info", "郭嘉給你發(fā)了一個(gè)計(jì)策!");
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pi);
//獲取Notification
Notification n = builder.build();
//通過NotificationCompat.Builder.build()來獲得notification對(duì)象自己
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//然后調(diào)用NotificationManager.notify()向系統(tǒng)轉(zhuǎn)交
manager.notify(NO_1, n);
}
}
第四步:java(跳轉(zhuǎn)后Activity)功能代碼實(shí)現(xiàn)ContentActivity.java(只土司):
public class ContentActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
//通過獲取MainActivity中設(shè)置的putExtra獲取土司內(nèi)容
Toast.makeText(this, getIntent().getStringExtra("info"), Toast.LENGTH_SHORT).show();
}
}
演示效果的代碼就這些,我們梳理下本次實(shí)現(xiàn)的思路:
(1)通過按鈕觸發(fā)點(diǎn)擊事件
(2)將notification的一些UI信息以及相關(guān)動(dòng)作賦予NotificationCompat.Builder對(duì)象,然后通過NotificationCompat.Builder.build()來獲得notification對(duì)象自己;然后調(diào)用NotificationManager.notify()向系統(tǒng)轉(zhuǎn)交這個(gè)通知。
(3)在第二步中通過Builder的setContentIntent()來添加PendingIntent,為Notification添加行為,也就是Activity的跳轉(zhuǎn)
(4)對(duì)打開的Activity設(shè)置表現(xiàn)的效果。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android notification 的總結(jié)分析
- Android界面 NotificationManager使用Bitmap做圖標(biāo)
- Android中通知Notification使用實(shí)例(振動(dòng)、燈光、聲音)
- android中創(chuàng)建通知欄Notification代碼實(shí)例
- Android中通過Notification&NotificationManager實(shí)現(xiàn)消息通知
- Android編程實(shí)現(xiàn)攔截短信并屏蔽系統(tǒng)Notification的方法
- Android開發(fā) -- 狀態(tài)欄通知Notification、NotificationManager詳解
- Android中關(guān)于Notification及NotificationManger的詳解
- 詳解Android中Notification通知提醒
- 詳解Android中Notification的使用方法
相關(guān)文章
Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫效果(二)
這篇文章主要為大家詳細(xì)介紹了Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Android實(shí)現(xiàn)可復(fù)用的篩選頁面
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)可復(fù)用的篩選頁面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
Android實(shí)現(xiàn)一個(gè)比相冊(cè)更高大上的左右滑動(dòng)特效(附源碼)
這篇文章主要介紹了Android實(shí)現(xiàn)一個(gè)比相冊(cè)更高大上的左右滑動(dòng)特效(附源碼),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
android基礎(chǔ)總結(jié)篇之九:Intent應(yīng)用詳解
這篇文章主要介紹了android基礎(chǔ)總結(jié)篇之九:Intent應(yīng)用詳解,有需要的可以了解一下。2016-11-11
Android進(jìn)階KOOM線上APM監(jiān)控全面剖析
這篇文章主要為大家介紹了Android進(jìn)階KOOM線上APM監(jiān)控全面剖析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Android獲取手機(jī)通訊錄、sim卡聯(lián)系人及調(diào)用撥號(hào)界面方法
這篇文章主要介紹了Android獲取手機(jī)通訊錄、sim卡聯(lián)系人及調(diào)用撥號(hào)界面方法,本文分別給出實(shí)現(xiàn)代碼實(shí)現(xiàn)獲取通訊錄和sim卡的聯(lián)系人,以及權(quán)限配置和調(diào)用系統(tǒng)撥打電話的界面的實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-04-04
Android Imageloader的配置的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android Imageloader的配置的實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-07-07

