最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android使用Notification在狀態(tài)欄上顯示通知

 更新時(shí)間:2017年12月26日 11:15:08   作者:光仔December  
這篇文章主要為大家詳細(xì)介紹了Android使用Notification在狀態(tài)欄上顯示通知,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在使用手機(jī)時(shí),當(dāng)有未接來(lái)電或者是新短消息時(shí),手機(jī)會(huì)給出相應(yīng)的提示信息,這些提示信息通常會(huì)顯示到手機(jī)屏幕的狀態(tài)欄上。Android也提供了用于處理此類信息的類,他們是Notification和NotificationManager。其中,Notification代表的是具有全局效果的通知;而NotificationManager則是用于發(fā)送Notification通知的系統(tǒng)服務(wù)。

使用Notification和NotificationManager類發(fā)送和顯示通知也比較簡(jiǎn)單,大致可分為以下4個(gè)步驟。

(1)調(diào)用getSystemService()方法獲取系統(tǒng)的NotificationManager服務(wù)。
(2)創(chuàng)建一個(gè)Notification對(duì)象,并為其設(shè)置各種屬性
(3)為Notification對(duì)象設(shè)置事件信息
(4)通過(guò)NotificationManager類的notify()方法發(fā)送Notification通知

下面通過(guò)一個(gè)具體的實(shí)例說(shuō)明如何使用Notification在狀態(tài)欄上顯示通知:
res/layout/main.xml:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  android:orientation="vertical"  
  android:layout_width="fill_parent"  
  android:layout_height="fill_parent"  
  android:id="@+id/layout1" 
  android:gravity="center_horizontal" 
  >  
  <Button android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="顯示通知"/> 
  <Button android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="刪除通知"/> 
</LinearLayout>  

這個(gè)是點(diǎn)擊通知跳轉(zhuǎn)的頁(yè)面main2.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="這里是詳細(xì)內(nèi)容"/> 
</LinearLayout> 

在中AndroidManifest.xml添加一下兩個(gè)權(quán)限,并在<application>標(biāo)簽中注冊(cè)ContentActivity:

<!-- 添加操作閃光燈的權(quán)限 --> 
  <uses-permission android:name="android.permission.FLASHLIGHT"/> 
<!-- 添加操作震動(dòng)器的權(quán)限 --> 
  <uses-permission android:name="android.permission.VIBRATE"/> 
<application> 
<activity android:name=".ContentActivity"/> 
</application> 

MainActivity:

package com.example.test;  
  
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.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
  
public class MainActivity extends Activity {  
   public static int NOTIFYID_1=1,NOTIFYID_2=2; 
  @Override  
  public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main);  
      
    //獲取通知管理器,用于發(fā)送通知 
    final NotificationManager notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
     
    Button button1=(Button) findViewById(R.id.button1);//獲取"顯示通知"按鈕 
    //為"顯示通知"按鈕添加單擊事件監(jiān)聽器 
    button1.setOnClickListener(new OnClickListener() { 
       
      @Override 
      public void onClick(View arg0) { 
        Notification notify=new Notification();//創(chuàng)建一個(gè)Notification對(duì)象 
        notify.icon=R.drawable.in; 
        notify.tickerText="顯示第一個(gè)通知"; 
        notify.when=System.currentTimeMillis();//設(shè)置發(fā)送時(shí)間(設(shè)置為當(dāng)前時(shí)間) 
        notify.defaults=Notification.DEFAULT_ALL;//設(shè)置默認(rèn)聲音、默認(rèn)震動(dòng)和默認(rèn)閃光燈 
        notify.setLatestEventInfo(MainActivity.this, "無(wú)題", "每天進(jìn)步一點(diǎn)點(diǎn)", null);//設(shè)置事件信息 
        notificationManager.notify(NOTIFYID_1,notify);//通過(guò)通知管理器發(fā)送通知 
         
        //添加第二個(gè)通知 
        Notification notify1=new Notification(R.drawable.music,"顯示第二個(gè)通知",System.currentTimeMillis()); 
        notify1.flags=Notification.FLAG_AUTO_CANCEL;//打開應(yīng)用程序后圖標(biāo)消失 
        Intent intent=new Intent(MainActivity.this,ContentActivity.class);//設(shè)置為跳轉(zhuǎn)頁(yè)面準(zhǔn)備的Intent 
        //針對(duì)意圖的包裝對(duì)象,在下面就是通知被點(diǎn)擊時(shí)激活的組件對(duì)象(上下文,請(qǐng)求碼,意圖對(duì)象,標(biāo)識(shí)符) 
        PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this, 0, intent, 0); 
        //設(shè)置通知的內(nèi)容  (上下文對(duì)象,標(biāo)題, 內(nèi)容, 指定通知被點(diǎn)擊的時(shí)候跳轉(zhuǎn)到哪里,激活哪個(gè)組件) 
        notify1.setLatestEventInfo(MainActivity.this, "通知", "查看詳細(xì)內(nèi)容", pendingIntent); 
        notificationManager.notify(NOTIFYID_2,notify);//通過(guò)通知管理器發(fā)送通知 
      } 
    }); 
     
    Button button2=(Button) findViewById(R.id.button2);//獲取"刪除通知"按鈕 
    //為"顯示通知"按鈕添加單擊事件監(jiān)聽器 
    button2.setOnClickListener(new OnClickListener() { 
 
 
      @Override 
      public void onClick(View arg0) { 
        notificationManager.cancel(NOTIFYID_1);//清除ID號(hào)為常量NOTIFYID_1的通知 
        notificationManager.cancelAll();//清除全部通知 
      }   
    }); 
  }  
} 

 運(yùn)行本實(shí)例,單擊"顯示通知"按鈕,在屏幕的左上角將顯示第一個(gè)通知,如圖-4.2.2.a.jpg所示,過(guò)一段時(shí)間后,該通知消失,并顯示第二個(gè)通知,再過(guò)一段時(shí)間后,第二個(gè)通知消失,這時(shí)在狀態(tài)欄上將顯示這兩個(gè)通知的圖標(biāo),如圖-4.2.2.b.jpg所示,單擊通知圖標(biāo),將顯示如圖-4.2.2.c.jpg所示的通知列表,單擊第一個(gè)列表項(xiàng),可以查看通知的詳細(xì)內(nèi)容,如圖-4.2.2.d.jpg所示,查看后,該通知的圖標(biāo)將不在狀態(tài)欄中顯示。單擊"刪除通知"按鈕,可以刪除全部通知。

圖-4.2.2.a.jpg:


圖-4.2.2.b.jpg:


圖-4.2.2.c.jpg:


圖-4.2.2.d.jpg:


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

相關(guān)文章

  • Android?優(yōu)雅的讀寫Excel

    Android?優(yōu)雅的讀寫Excel

    這篇文章主要為大家介紹了Android優(yōu)雅的讀寫Excel實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Kotlin學(xué)習(xí)教程之函數(shù)的默認(rèn)參數(shù)

    Kotlin學(xué)習(xí)教程之函數(shù)的默認(rèn)參數(shù)

    這篇文章主要給大家介紹了關(guān)于Kotlin學(xué)習(xí)教程之函數(shù)的默認(rèn)參數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Android中懸浮窗口的實(shí)現(xiàn)原理實(shí)例分析

    Android中懸浮窗口的實(shí)現(xiàn)原理實(shí)例分析

    這篇文章主要介紹了Android中懸浮窗口的實(shí)現(xiàn)原理,以實(shí)例形式較為詳細(xì)的分析了Android懸浮窗口的原理與具體實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • android studio 打包自動(dòng)生成版本號(hào)與日期,apk輸入路徑詳解

    android studio 打包自動(dòng)生成版本號(hào)與日期,apk輸入路徑詳解

    這篇文章主要介紹了android studio 打包自動(dòng)生成版本號(hào)與日期,apk輸入路徑詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • Android屬性動(dòng)畫之ValueAnimator代碼詳解

    Android屬性動(dòng)畫之ValueAnimator代碼詳解

    這篇文章主要介紹了Android屬性動(dòng)畫之ValueAnimator代碼詳解,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • Android中EditText+Button組合導(dǎo)致輸入板無(wú)法收起的原因分析及解決辦法

    Android中EditText+Button組合導(dǎo)致輸入板無(wú)法收起的原因分析及解決辦法

    這篇文章主要介紹了Android中EditText+Button組合導(dǎo)致輸入板無(wú)法收起的原因分析及解決辦法的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • 深入理解Android之接口回調(diào)機(jī)制

    深入理解Android之接口回調(diào)機(jī)制

    本篇文章主要介紹了Android之接口回調(diào)機(jī)制,在開發(fā)中經(jīng)常會(huì)用到,具有一定的學(xué)習(xí)價(jià)值,有需要的可以來(lái)了解一下。
    2016-10-10
  • Android入門之在Activity之間穿梭的Intent

    Android入門之在Activity之間穿梭的Intent

    Intent可以用來(lái)啟動(dòng)Activity(startActivity(Intent))、Serveice(startService(Intent))等組件,可以用來(lái)綁定Activity和Service以建立它們之間的通信(bindServiceConnaction(Intent,ServiceConnection,int)),可以作為Broadcast Intent發(fā)送給廣播接收器
    2021-10-10
  • Android 高仿斗魚滑動(dòng)驗(yàn)證碼

    Android 高仿斗魚滑動(dòng)驗(yàn)證碼

    這篇文章主要介紹了Android 高仿斗魚滑動(dòng)驗(yàn)證碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-11-11
  • Android開發(fā)使用Handler的PostDelayed方法實(shí)現(xiàn)圖片輪播功能

    Android開發(fā)使用Handler的PostDelayed方法實(shí)現(xiàn)圖片輪播功能

    這篇文章主要介紹了Android開發(fā)使用Handler的PostDelayed方法實(shí)現(xiàn)圖片輪播功能,結(jié)合實(shí)例形式分析了Android基于Handler的PostDelayed方法實(shí)現(xiàn)圖片輪播功能的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2017-12-12

最新評(píng)論

隆安县| 海兴县| 临西县| 房产| 江源县| 漳州市| 河南省| 浮梁县| 南岸区| 安康市| 剑阁县| 威信县| 仪陇县| 松滋市| 崇仁县| 克什克腾旗| 平南县| 石门县| 开原市| 玛纳斯县| 邢台县| 来宾市| 青田县| 志丹县| 资阳市| 莱州市| 民丰县| 武汉市| 乌恰县| 泸州市| 通山县| 延津县| 收藏| 广汉市| 朔州市| 祁阳县| 长治县| 巨野县| 民勤县| 瑞昌市| 崇左市|