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

Android編程開發(fā)之NotiFication用法詳解

 更新時間:2015年12月26日 12:38:09   作者:sgx425021234  
這篇文章主要介紹了Android編程開發(fā)之NotiFication用法,結合實例形式較為詳細的分析了NotiFication的功能、使用技巧與注意事項,需要的朋友可以參考下

本文實例講述了Android編程開發(fā)之NotiFication用法。分享給大家供大家參考,具體如下:

notification就是通知的意思,安卓中指通知欄,一般用在電話,短信,郵件,鬧鐘鈴聲,在手機的狀態(tài)欄上就會出現一個小圖標,提示用戶處理這個快訊,這時手從上方滑動狀態(tài)欄就可以展開并處理這個快訊。

在幫助文檔中,是這么說的, notification類表示一個持久的通知,將提交給用戶使用NotificationManager。已添加的Notification.Builder,使其更容易構建通知。

notification是一種讓你的應用程序在沒有開啟情況下或在后臺運行警示用戶。它是看不見的程序組件(Broadcast Receiver,Service和不活躍的Activity)警示用戶有需要注意的事件發(fā)生的最好途徑。

先來區(qū)分以下狀態(tài)欄和狀態(tài)條的區(qū)別:

1、狀態(tài)條就是手機屏幕最上方的一個條形狀的區(qū)域;

在狀態(tài)條有好多信息量:比如usb連接圖標,手機信號圖標,電池電量圖標,時間圖標等等;

2、狀態(tài)欄就是手從狀態(tài)條滑下來的可以伸縮的view;

在狀態(tài)欄中一般有兩類(使用FLAG_標記):

(1)正在進行的程序;           (2)是通知事件;

一個Notification傳送的信息有:

1、一個狀態(tài)條圖標;

2、在拉伸的狀態(tài)欄窗口中顯示帶有大標題,小標題,圖標的信息,并且有處理該點擊事件:比如調用該程序的入口類;

3、閃光,LED,或者震動;

下面對Notification類中的一些常量,字段,方法簡單介紹一下:
常量:

DEFAULT_ALL                  使用所有默認值,比如聲音,震動,閃屏等等
DEFAULT_LIGHTS            使用默認閃光提示
DEFAULT_SOUNDS         使用默認提示聲音
DEFAULT_VIBRATE         使用默認手機震動

注意:在加入手機震動效果時一定要在項目清單文件中加入手機震動權限:

復制代碼 代碼如下:
<uses-permission android:name="android.permission.VIBRATE" />

下面通過簡單額小實例來具體實現notification功能:

MainActivity.java:

package com.example.lession16_notifi;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
  private NotificationManager notificationManager;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // 第一步:通過getSystemService()方法得到NotificationManager對象;
    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  }
  // 測試
  public void test1(View v) {
    showNotification("來短信了", "5557", "hello!", R.drawable.ic_launcher,
        R.drawable.ic_launcher);
  }
  // 第二步:對Notification的一些屬性進行設置比如:內容,圖標,標題,相應notification的動作進行處理等等;
  public void showNotification(String tickerText, String contentTitle,
      String contentText, int iconId, int notiId) {
    // 創(chuàng)建一個Notification
    Notification notification = new Notification();
    // 設置通知 消息 圖標
    notification.icon = iconId;
    // 設置發(fā)出消息的內容
    notification.tickerText = tickerText;
    // 設置發(fā)出通知的時間
    notification.when = System.currentTimeMillis();
    // 設置顯示通知時的默認的發(fā)聲、振動、Light效果
    notification.defaults = Notification.DEFAULT_VIBRATE;// 振動
    // Notification notification = new Notification(R.drawable.ic_launcher,"有新的消息", System.currentTimeMillis());
    // 3步:PendingIntent android系統(tǒng)負責維護
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,getIntent(), 0);
    // 4步:設置更加詳細的信息
    notification.setLatestEventInfo(this, contentTitle, contentText,pendingIntent);
    // 5步:使用notificationManager對象的notify方法 顯示Notification消息 需要制定
    // Notification的標識
    notificationManager.notify(notiId, notification);
  }
  // 6步:使用notificationManager對象的cancelAll()方法取消
  public void clearNoti(View v) {
    notificationManager.cancelAll();// 清除所有
  }
}

布局文件activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context=".MainActivity" >
  <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="22dp"
    android:onClick="test1"
    android:text="@string/text_notifi" />
  <Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_below="@+id/button1"
    android:layout_marginTop="60dp"
    android:onClick="clearNoti"
    android:text="@string/text_clear" />
</RelativeLayout>

布局效果:

切記實現震動效果在清單文件中加入權限:

復制代碼 代碼如下:
<uses-permission android:name="android.permission.VIBRATE" />

實現效果如下:

希望本文所述對大家Android程序設計有所幫助。

相關文章

  • Android中GPS坐標轉換為高德地圖坐標詳解

    Android中GPS坐標轉換為高德地圖坐標詳解

    最近因為公司需求,在做GPS定位,并且將獲得的坐標顯示在高德地圖上,但是實際效果跟我們期望的是有偏差的。通過查閱資料,才知道有地球坐標、火星坐標之說。下面這篇文章就詳細介紹了Android中GPS坐標轉換為高德地圖坐標的方法,需要的朋友可以參考下。
    2017-01-01
  • Android實現九宮格手勢解鎖

    Android實現九宮格手勢解鎖

    這篇文章主要為大家詳細介紹了Android實現九宮格手勢解鎖的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Android無需申請權限撥打電話的兩種方式

    Android無需申請權限撥打電話的兩種方式

    android 打電話有兩種實現方式,第一種方法撥打電話跳轉到撥號界面,第二種方法,撥打電話直接進行撥打,下面逐一給大家介紹這兩種方式,需要的朋友參考下吧
    2016-12-12
  • Android控件之GridView用法實例分析

    Android控件之GridView用法實例分析

    這篇文章主要介紹了Android控件之GridView用法,通過繪制九宮格的實例形式分析了GridView可滾動網格的實現技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09
  • android讀取Assets圖片資源保存到SD卡實例

    android讀取Assets圖片資源保存到SD卡實例

    本文為大家詳細介紹下android讀取Assets圖片資源保存到SD卡的具體實現,感興趣的各位可以參考下哈,希望對大家有所幫助
    2013-07-07
  • Android實現雅虎新聞摘要加載視差動畫效果

    Android實現雅虎新聞摘要加載視差動畫效果

    這篇文章主要介紹了Android實現雅虎新聞摘要加載視差動畫效果,通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08
  • Android中Fragment的加載方式與數據通信詳解

    Android中Fragment的加載方式與數據通信詳解

    本文主要介紹了Android中Fragment的加載方式與數據通信的相關知識。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-03-03
  • 詳解Android內存優(yōu)化策略

    詳解Android內存優(yōu)化策略

    這篇文章主要介紹了詳解Android內存優(yōu)化策略,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • Android實現View滑動的6種方式

    Android實現View滑動的6種方式

    這篇文章主要為大家詳細介紹了Android實現View滑動的6種方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • 淺談Android單元測試的作用以及簡單示例

    淺談Android單元測試的作用以及簡單示例

    本篇文章主要介紹了淺談Android單元測試的作用以及簡單示例,具有一定的參考價值,有興趣的可以了解一下
    2017-08-08

最新評論