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

相关文章
相关标签/搜索