欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android使用Notification實(shí)現(xiàn)普通通知欄(一)

 更新時(shí)間:2016年12月07日 09:38:23   作者:潘侯爺  
這篇文章主要為大家詳細(xì)介紹了Android使用Notification實(shí)現(xiàn)普通通知欄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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,在未展開的情況下也是以普通大小顯示出來(lái)。下面是一個(gè)普通的notification。

藍(lán)色指示框所代表的的意思如下:

1.標(biāo)題

2.大圖標(biāo)

3.通知內(nèi)容

4.通知數(shù)據(jù)

5.小圖標(biāo)

6.Notification的發(fā)布時(shí)間。

可以通過(guò)調(diào)用setWhen()設(shè)置一個(gè)明確的時(shí)間,

默認(rèn)是系統(tǒng)收到該notification的時(shí)間。

下面我們是我們本次的演示效果:


本次在普通視圖的基礎(chǔ)上添加了點(diǎn)擊頁(yè)面跳轉(zhuǎn)的效果,可以理解為添加Notification的動(dòng)作與行為:

雖然這也是可選的,但是你還是應(yīng)該為你的notification至少添加一種行為:允許用戶通過(guò)點(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)系起來(lái),你需要調(diào)用合適的NotificationCompat.Builder方法。

比如你想在點(diǎn)擊notification文字的時(shí)候啟動(dòng)activity,你需要調(diào)用NotificationCompat.Builder的setContentIntent()來(lái)添加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)頁(yè)面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()來(lái)添加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();
  //通過(guò)NotificationCompat.Builder.build()來(lái)獲得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);
  //通過(guò)獲取MainActivity中設(shè)置的putExtra獲取土司內(nèi)容
  Toast.makeText(this, getIntent().getStringExtra("info"), Toast.LENGTH_SHORT).show();
 }
}

演示效果的代碼就這些,我們梳理下本次實(shí)現(xiàn)的思路:

(1)通過(guò)按鈕觸發(fā)點(diǎn)擊事件

(2)將notification的一些UI信息以及相關(guān)動(dòng)作賦予NotificationCompat.Builder對(duì)象,然后通過(guò)NotificationCompat.Builder.build()來(lái)獲得notification對(duì)象自己;然后調(diào)用NotificationManager.notify()向系統(tǒng)轉(zhuǎn)交這個(gè)通知。

(3)在第二步中通過(guò)Builder的setContentIntent()來(lái)添加PendingIntent,為Notification添加行為,也就是Activity的跳轉(zhuǎn)

(4)對(duì)打開的Activity設(shè)置表現(xiàn)的效果。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Kotlin中內(nèi)置函數(shù)的用法和區(qū)別總結(jié)

    Kotlin中內(nèi)置函數(shù)的用法和區(qū)別總結(jié)

    眾所周知相比Java, Kotlin提供了不少高級(jí)語(yǔ)法特性。對(duì)于一個(gè)Kotlin的初學(xué)者來(lái)說(shuō)經(jīng)常會(huì)寫出一些不夠優(yōu)雅的代碼。下面這篇文章主要給大家介紹了關(guān)于Kotlin中內(nèi)置函數(shù)的用法和區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2018-06-06
  • Android RecycleView和線型布局制作聊天布局

    Android RecycleView和線型布局制作聊天布局

    大家好,本篇文章主要講的是Android RecycleView和線型布局制作聊天布局,感興趣的同學(xué)趕緊來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01
  • Android的UI調(diào)優(yōu)教程

    Android的UI調(diào)優(yōu)教程

    這篇文章主要為介紹了Android的UI調(diào)優(yōu)各種案例教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2022-03-03
  • Android seekbar(自定義)控制音量同步更新

    Android seekbar(自定義)控制音量同步更新

    這篇文章主要介紹了Android seekbar(自定義)控制音量同步更新的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • 如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效

    如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效

    這篇文章主要給大家介紹了關(guān)于如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • 基于Android中手勢(shì)交互的實(shí)現(xiàn)方法

    基于Android中手勢(shì)交互的實(shí)現(xiàn)方法

    本篇文章是對(duì)Android中手勢(shì)交互的實(shí)現(xiàn)進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下
    2013-05-05
  • android獲得當(dāng)前view在屏幕中坐標(biāo)的方法

    android獲得當(dāng)前view在屏幕中坐標(biāo)的方法

    這篇文章主要介紹了android獲得當(dāng)前view在屏幕中坐標(biāo)的方法,涉及Android針對(duì)view坐標(biāo)相關(guān)屬性的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • 詳解Android的MVVM框架 - 數(shù)據(jù)綁定

    詳解Android的MVVM框架 - 數(shù)據(jù)綁定

    這篇文章主要介紹了詳解Android的MVVM框架 - 數(shù)據(jù)綁定,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-05-05
  • Android ListView仿微信聊天界面

    Android ListView仿微信聊天界面

    這篇文章主要為大家詳細(xì)介紹了ListView仿微信聊天界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • Android開發(fā)使用Handler實(shí)現(xiàn)圖片輪播功能示例

    Android開發(fā)使用Handler實(shí)現(xiàn)圖片輪播功能示例

    這篇文章主要介紹了Android開發(fā)使用Handler實(shí)現(xiàn)圖片輪播功能,涉及Android基于Handler操作圖片的相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下
    2017-09-09

最新評(píng)論