android---Notification通知解析

Notification是顯示在手機狀態欄的通知,Notification通知是具備全局性的通知,通常經過NotificationManager來進行管理.

通常運用Notification的步驟以下:
1.調用getSysytemService(NOTIFICATION_SERVICE)來獲取系統的NotificationManager,進行Notification的發送和回收
2.經過構造器創建一個Notification
3.爲Notification set各類屬性,而後builder()創建
4.經過NotificationManager發送通知java

下面經過一個實例來演示上面的用法,先看一張效果圖

這裏寫圖片描述

一.獲取系統的NotificationManagerandroid

private NotificationManager nm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //獲取系統的通知管理
        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    }

二.爲主佈局的兩個按鈕添加監聽事件,而後分別設置啓動通知,並設置各類屬性和取消通知
各類屬性代碼中介紹的很詳細,具體能夠參考APImarkdown

啓動通知
public void send(View view){
        //用於打開通知啓動另外一個Activity
        Intent intent = new Intent(MainActivity.this,OtherActivity.class);
        //用於延遲啓動
        PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
        //設置通知
        Notification notify = new Notification.Builder(this)
                //設置打開該通知,通知自動消失
                .setAutoCancel(true)
                //設置顯示在狀態欄的通知提示消息
                .setTicker("新消息")
                //設置通知欄圖標
                .setSmallIcon(R.mipmap.ic_launcher)
                //設置通知內容的標題
                .setContentTitle("一條新通知")
                //設置通知內容
                .setContentText("恭喜你通知欄測試成功")
                //設置使用系統默認的聲音,默認的led燈
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS)
                //ALL的話則是所有使用默認,聲音,震動,閃光燈,須要添加相應權限
// .setDefaults(ALL)
                //或者自定義聲音
                //setSound(Uri.parse())
                //設置要啓動的程序
                .setContentIntent(pi)
                //最後用build來創建通知
                .build();
        //發送當前通知,經過NotificationManager來管理
        nm.notify(1,notify);
    }

這裏用的OtherActivity是經過通知啓動的另外一個Activity,爲了啓動須要在清單文件中加入此Activity,而且由於用到了閃光燈和振動器,因此也須要添加相應的權限ide

<activity android:name=".OtherActivity"> </activity>
    <uses-permission android:name="android.permission.FLASHLIGHT"/>
    <uses-permission android:name="android.permission.VIBRATE"/>
取消通知
//取消通知
    public void closed(View view){
        nm.cancel(1);
    }

用起來至關很方便.最後附上主界面佈局佈局

<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin" android:orientation="horizontal" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">


    <Button  android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="開啓通知" android:onClick="send" android:id="@+id/btnstartnotification" />

    <Button  android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="關閉通知" android:onClick="closed" android:id="@+id/btnstopnotification" />
</LinearLayout>

版權聲明:本文爲博主原創文章,未經博主容許不得轉載。測試

相關文章
相關標籤/搜索