Android基礎教程之Button事件發送消息到通知欄Notification

在手機上,我們經常會看到手機頂部會有收到消息,覺得還蠻不錯的,於是乎今天研究了一下,做了點總結。

一、Notification通知欄通知
Notification是顯示在手機狀態欄的消息(手機狀態欄位於手機最頂端),代表一種全局效果的通知。

二、通知欄的內容
一般包含圖標、標題、內容、時間、點擊後響應

三、如何實現通知欄
1.通知管理類
獲取NotificationManager
顯示通知欄:notify(id, notification);
取消通知欄:cancle(id);
2.通知類
構造Notification並設置顯示內容
通知欄通知可以設置聲音提示、指示燈,以及震動效果
步驟:
一、實現 OnClickListener接口public class MainActivity extends Activity implements OnClickListener{
二、構造notification併發送到通知欄

這裏特別講一下,在開發中我們還需要用到pendingIntent類

關於這個類,我在網上做了一下大概的總結

首先,pendingIntent字面意義:等待的,未決定的Intent。
要得到一個pendingIntent對象,使用方法類的靜態方法 getActivity(Context, int, Intent, int),getBroadcast(Context, int, Intent, int),getService(Context, int, Intent, int) 分別對應着Intent的3個行爲,跳轉到一個activity組件、打開一個廣播組件和打開一個服務組件。
參數有4個,比較重要的事第三個和第一個,其次是第四個和第二個。可以看到,要得到這個對象,必須傳入一個Intent作爲參數,必須有context作爲參數。
pendingIntent是一種特殊的Intent。主要的區別在於Intent的執行立刻的,而pendingIntent的執行不是立刻的。pendingIntent執行的操作實質上是參數傳進來的Intent的操作,但是使用pendingIntent的目的在於它所包含的Intent的操作的執行是需要滿足某些條件的。
主要的使用的地方和例子:通知Notificatio的發送,短消息SmsManager的發送和警報器AlarmManager的執行等等

好了基礎都講的差不多了,現在演示一下普通的發送消息到通知欄,跟取消通知欄的操作。先上效果圖:

佈局
佈局代碼

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.jp.logdemo.MainActivity"
    tools:ignore="MergeRootFrame" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/toast_common"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/shapemore"
            android:text="普通Toast"
            android:textSize="15sp" />

        <Button
            android:id="@+id/toast_seat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/shapemore"
            android:text="位置偏移Toast"
            android:textSize="15sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/toast_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/shapemore"
            android:text="帶圖片的Toast"
            android:textSize="15sp" />

        <Button
            android:id="@+id/toast_div"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/shapemore"
            android:text="自定義的Toast"
            android:textSize="15sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/alertdialog_sure"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/shapemore"
            android:text="確認對話框"
            android:textSize="15sp" />

        <Button
            android:id="@+id/alertdialog_one"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/shapemore"
            android:text="單選對話框"
            android:textSize="15sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/alertdialog_two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/shapemore"
            android:text="多選對話框"
            android:textSize="15sp" />

        <Button
            android:id="@+id/alertdialog_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/shapemore"
            android:text="列表對話框"
            android:textSize="15sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/alertdialog_div"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/shapemore"
            android:text="自定義對話框"
            android:textSize="15sp" />

        <Button
            android:id="@+id/notification_send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/shapemore"
            android:text="發送消息到通知欄"
            android:textSize="15sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/notification_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/shapemore"
            android:text="取消消息到通知欄"
            android:textSize="15sp" />
    </LinearLayout>

</LinearLayout>

接下來獲取佈局中控件的id:

    // 發送消息到通知欄,取消通知欄的消息
    Button notification_send, notification_cancel;
    NotificationManager notificationManager;
    //標識
    int notification_id;
    // 獲取控件id
    private void initView() {       
        // Notification
        notification_send = (Button) findViewById(R.id.notification_send);
        notification_cancel = (Button) findViewById(R.id.notification_cancel);
    }

建立點擊事件

    // 創建點擊事件
    private void initEvent() {
        // Notification點擊事件
        notification_send.setOnClickListener(this);
        notification_cancel.setOnClickListener(this);
    }

發送效果圖:
這裏寫圖片描述
這裏寫圖片描述

    /**
     * 發送通知到通知欄
     */

    @SuppressLint("NewApi")
    private void sendNotification() {

        android.app.Notification.Builder builder = new Notification.Builder(
                this);
        builder.setSmallIcon(R.drawable.ic_launcher);// 設置圖標
        builder.setTicker("你好");// 設置手機狀態欄的提示
        builder.setWhen(System.currentTimeMillis());// 設置時間
        builder.setContentTitle("通知欄通知");// 設置標題
        builder.setContentText("我來自demo");// 設置內容
        Toast.makeText(MainActivity.this, "發送消息到通知欄", Toast.LENGTH_LONG).show();
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pending = PendingIntent.getActivity(this, 0, intent,
                0);
        builder.setContentIntent(pending);// 點擊後的意圖

        builder.setDefaults(Notification.DEFAULT_LIGHTS);// 設置指示燈
        builder.setDefaults(Notification.DEFAULT_SOUND);// 設置提示聲音
        builder.setDefaults(Notification.DEFAULT_VIBRATE);// 設置震動
        builder.setDefaults(Notification.DEFAULT_ALL);// 包含以上三個
        // 通過Builder對象的build()可以創建一個notification對象
        Notification notification = builder.build();// 4.1以上(包括4.1),4.1以下用builder.getNotification();
        // 創建NotificationManager的對象
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // 顯示notification
        notificationManager.notify(notification_id, notification);

    }

取消效果圖:
這裏寫圖片描述
這裏寫圖片描述

取消發送的代碼就相對來說簡潔很多

    /**
     * 取消到通知欄的消息
     */
    private void sendCancelNotification() {
        notificationManager.cancel(notification_id);
        Toast.makeText(MainActivity.this, "取消通知欄的消息", Toast.LENGTH_LONG).show();

    }

最後還需要說一下,如果想讓你的消息發送到通知欄有震動等提示的話,還需要去AndroidManifest.xml中設置指示燈和震動的權限
users Permission
android.permission.FLASHLIGHT
android.permission.VIBRATE

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章