Notification的基本使用

跟風遠走 提交于 2019-12-06 18:21:52

   Notification是显示在手机状态的通知,一般显示手机当前的网络状态、电池状态、时间等。

设置Notification涉及到两个类,一个类是NotificationManager,一个类是Notification。可以这样理解这两个类,NotificationManager相当于顺丰快递小哥,notification代表的就是我们送的信件,我们要发送信件首先打电话给顺丰小哥,相当于初始化NotificationManager,然后填写好信件,相当于初始化Notifiaction,然后小哥发送信件,就是notificationmanager.notify(notifaction)

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

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


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"
    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:

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不显示。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!