提示通知框Notification

本文介绍了如何使用Notification实现手机消息通知,包括创建通知、设置通知属性、取消通知等基本操作,并通过示例代码展示了在Android应用中实现消息通知的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 昨天晚上看了一本书上的Notification提示框,然后学了一下,就是我们手机又消息时,它会在手机最上面提示你,你有什么消息!那么接下来就学学怎么使用Notification来通知吧!</span>

首先如图,我写了4个按钮来使用Notification,因为有改进了,所以使用方法也稍稍改进了一下。

下面是实现这4个按钮的代码:

package com.example.uidesign;


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

@SuppressLint("NewApi")
public class MainActivity extends Activity {
	private static final int NOTIFYID_1 = 1;
	private Button btn1;
	private Button btn2;
	private Button btn3;
	private Button btn4;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//使用Notification的第一步需要获取NotificationManager的服务
				//获取通知管理器,用于发送通知
		final NotificationManager notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		init();                //初始化各个按钮
		btn1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//创建第一个Notification的对象
				Notification notification1=new Notification();
				
				 // 以下为设置notification的各种 属性
				 
				notification1.icon=R.drawable.noti;
				notification1.tickerText="显示第一个通知";
				notification1.when=System.currentTimeMillis();           //设置发送时间
				//设置默认声音、默认振动和默认闪光灯
				notification1.defaults=Notification.DEFAULT_ALL;         
				notification1.setLatestEventInfo(MainActivity.this, 
						"内容题目", "使用notification1", null);            //设置事件信息
				//打开应用程序后图标消失
				notification1.flags=Notification.FLAG_AUTO_CANCEL;    
				notificationManager.notify(NOTIFYID_1, notification1);
			}
		});
		
		btn2.setOnClickListener(new OnClickListener() {
			
			@SuppressLint("NewApi")
			@Override
			public void onClick(View v) {

				Intent intent=new Intent(MainActivity.this, SecondActivity.class);
				PendingIntent pendingIntent=PendingIntent
				.getActivity(MainActivity.this, 0, intent, 0);
				//创建第二个Notification的对象
				Notification notification2=new Notification.Builder(MainActivity.this)
				         .setSmallIcon(R.drawable.noti)
				         .setTicker("显示第二个通知")
				         .setContentTitle("本例子的通知标题")
				         .setContentText("设置第二个例子的通知内容")
				         .setContentIntent(pendingIntent)
				         //若是同时触发多个通知,编号可以清楚的标识哪一个
				         .setNumber(1)   
				         .getNotification();
				//设置默认声音、默认振动和默认闪光灯
				notification2.defaults=Notification.DEFAULT_ALL;   
				//打开应用程序后图标消失
				notification2.flags=Notification.FLAG_AUTO_CANCEL;    
				notificationManager.notify(NOTIFYID_1, notification2);	
				
			}
		});
		
		btn3.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent2=new Intent(MainActivity.this, SecondActivity.class);
				PendingIntent pendingIntent2=PendingIntent
				.getActivity(MainActivity.this, 0, intent2, 0);
				//创建第二个Notification的对象
				Notification notifiCation3=new Notification.Builder(MainActivity.this)
				         .setSmallIcon(R.drawable.noti)
				         .setTicker("显示第三个通知")
				         .setContentTitle("本例子的通知标题")
				         .setContentText("设置第三个例子的通知内容")
				         .setContentIntent(pendingIntent2)
				         //若是同时触发多个通知,编号可以清楚的标识哪一个
				         .setNumber(1)  
				         //build()为子啊API16后才可使用
				         .build();
				//设置默认声音、默认振动和默认闪光灯
				notifiCation3.defaults=Notification.DEFAULT_ALL;   
				//打开应用程序后图标消失
				notifiCation3.flags=Notification.FLAG_AUTO_CANCEL;    
				notificationManager.notify(NOTIFYID_1, notifiCation3);	
			}
		});
		
		btn4.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//清楚ID号为常量NOTIFYID_1的通知
				notificationManager.cancel(NOTIFYID_1);
				//清除全部通知
				notificationManager.cancelAll();
			}
		});

		
		
	}
	
	
	public void init() {
		btn1=(Button) findViewById(R.id.button1);
		btn2=(Button) findViewById(R.id.button2);
		btn3=(Button) findViewById(R.id.button3);
		btn4=(Button) findViewById(R.id.button4);
		
	}
		
}
由于上面还添加了一个SecondActivity类:
package com.example.uidesign;

import android.app.Activity;
import android.os.Bundle;

public class SecondActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.content);
	}
}
其content.xml为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/content1" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/content2" />

</LinearLayout>

其中主activity的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="com.example.uidesign.MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="25dp"
        android:text="系统原始,通用" />

    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="清除通知" />

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button4"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="16dp"
        android:text="通知提示(在API16后可用)" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button3"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="14dp"
        android:text="通知提示(在API11后可用)" />

</RelativeLayout>

因为使用了两个activity,所以在AndroidManifest.xml文件中需要设置一下:
 <activity 
            android:name=".SecondActivity"
            android:label="详细内容"
            android:theme="@android:style/Theme.Dialog"></activity>

另外由于在主activity中我们使用了
//设置默认声音、默认振动和默认闪光灯
notifiCation3.defaults=Notification.DEFAULT_ALL;   
所以也是需要在AndroidManifest.xml文件中设置一下权限:
    <!-- 添加操作闪光灯的权限 -->
    <uses-permission android:name="android.permission.FLASHLIGHT"/>
    <!-- 添加操作振动器的权限 -->
    <uses-permission android:name="andorid.permission.VIBRATE"/>
</pre><pre name="code" class="html">好了以上就是使用这个notification所需要使用的方法了,当然这只是基础的,需要更深一点的使用就要靠自己的运用了。
以下是效果图:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值