Notification的基本使用

   Notification是顯示在手機狀態的通知,通常顯示手機當前的網絡狀態、電池狀態、時間等。 java

設置Notification涉及到兩個類,一個類是NotificationManager,一個類是Notification。能夠這樣理解這兩個類,NotificationManager至關於順豐快遞小哥,notification表明的就是咱們送的信件,咱們要發送信件首先打電話給順豐小哥,至關於初始化NotificationManager,而後填寫好信件,至關於初始化Notifiaction,而後小哥發送信件,就是notificationmanager.notify(notifaction) android

1)初始化NotificationManager: this.getSystemService(Context.NOTIFICATION_SERVICE); 網絡

2)初始化Notification:new Notification.Builder(this).builder(); app


main.xml: ide

<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"
    tools:context=".Main" >

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="發送notification" />

    <Button
        android:id="@+id/deletebtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="200sp"
        android:text="取消notification" />

</RelativeLayout>


Main.java: ui

package com.app.main;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class Main extends Activity {

	NotificationManager manager = null;

	Button button, btn1;

	int responseCode = 0x123;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

		button = (Button) this.findViewById(R.id.btn);

		btn1 = (Button) this.findViewById(R.id.deletebtn);

		manager = (NotificationManager) this
				.getSystemService(Context.NOTIFICATION_SERVICE);

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				send();

			}

		});

		btn1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {

				deleteMsg();
			}

		});

	}

	/**
	 * 
	 */
	@SuppressLint("NewApi")
	public void send() {

		Intent intent = new Intent(Main.this, OtherActivity.class);

		PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);

		Notification notification = new Notification.Builder(this)
				.setAutoCancel(true).setTicker("aaaa")
				.setSmallIcon(R.drawable.ic_launcher).setContentTitle("bbbb")
				.setContentText("ccccc")
				.setDefaults(Notification.DEFAULT_VIBRATE).build();

		manager.notify(responseCode, notification);

	}

	public void deleteMsg() {
		manager.cancel(responseCode);
	}
}
實現效果:


tips:在實例化Notification.Builder()實例的時候,必須setSmallIcon()或者setLargerIcon(),不然notifaction不顯示。 this

相關文章
相關標籤/搜索