详解Android中Notification通知提醒
作者:lijiao 发布时间:2023-09-10 09:57:19
标签:Android,Notification
在消息通知时,我们经常用到两个组件Toast和Notification。特别是重要的和需要长时间显示的信息,用Notification就最 合适不过了。当有消息通知时,状态栏会显示通知的图标和文字,通过下拉状态栏,就可以看到通知信息了,Android这一创新性的UI组件赢得了用户的一 致好评,就连苹果也开始模仿了。今天我们就结合实例,探讨一下Notification具体的使用方法。 首先说明一下我们需要实现的功能是:在程序启动时,发出一个通知,这个通知在软件运行过程中一直存在,相当于qq的托盘一样。
然后再演示一下普通的通知和自定义视图通知, 那我们就先建立一个安卓项目。
然后编辑/res/layout/main.xml文件,代码如下:
<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:background="@android:color/black"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/title"
android:textColor="#0f0"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="30dp"
android:onClick="normal"
android:text="@string/notification"
android:textColor="#0f0"
android:textSize="20sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="30dp"
android:onClick="custom"
android:text="@string/custom"
android:textColor="#0f0"
android:textSize="20sp" />
</LinearLayout>
上面的布局很简单,有两个按钮分别用于启动普通的notification和自定义的notification。
接下来自定义一个布局用于显示自定义的通知的。
<?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:background="#000"
android:orientation="vertical" >
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/action_settings"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textColor="#0f0"
android:textSize="15sp" />
</LinearLayout>
接下来就是上代码。
package com.itfom.notification;
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.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.RemoteViews;
public class MainActivity extends Activity {
private NotificationManager mNotificationManager;
private Context context;
private Notification notification;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//普通的通知
@SuppressWarnings("deprecation")
public void normal(View v){
//创建通知
createNotification("普通的通知");
//把通知放在正在运行栏目中
notification.flags|=Notification.FLAG_ONGOING_EVENT;
//设定默认声音
notification.defaults|=Notification.DEFAULT_SOUND;
//设定默认震动
notification.defaults|=Notification.DEFAULT_VIBRATE;
//设定默认LED灯提醒
notification.defaults|=Notification.DEFAULT_LIGHTS;
//设置点击后通知自动清除
notification.defaults|=Notification.FLAG_AUTO_CANCEL;
String textTitle="Notification示例";
String textContent="程序正在运行,点击此处跳转到演示界面";
Intent it=new Intent(context, MainActivity.class);
PendingIntent pendintent=PendingIntent.getActivity(context, 0, it, 0);
notification.setLatestEventInfo(context, textTitle, textContent, pendintent);
mNotificationManager.notify(0, notification);
}
//自定义的通知
public void custom(View v){
//创建通知
createNotification("个性化的通知");
//自定义通知的声音
notification.sound=Uri.parse(Environment.getExternalStorageDirectory()+"/non.mp3");
//自定义震动参数分别为多长时间开始震动、第一次震动的时间、停止震动的时间
long[] vibrate={0,100,200,300};
notification.vibrate=vibrate;
//自定义闪光灯的方式
notification.ledARGB=0xff00ff00;
notification.ledOnMS=500;
notification.ledOffMS=500;
notification.flags|=Notification.FLAG_SHOW_LIGHTS;
RemoteViews contentView=new RemoteViews(this.getPackageName(),R.layout.notify);
contentView.setTextViewText(R.id.tv, "这是个性化的通知");
//指定个性化的视图
notification.contentView=contentView;
Intent it=new Intent(context, MainActivity.class);
PendingIntent pendintent=PendingIntent.getActivity(context, 0, it, 0);
//指定内容视图
notification.contentIntent=pendintent;
mNotificationManager.notify(1, notification);
}
//自定义一个方法创建通知
@SuppressWarnings("deprecation")
public Notification createNotification(String text){
context = this;
mNotificationManager=(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
int icon=R.drawable.ic_launcher;
long when=System.currentTimeMillis();
return notification = new Notification(icon, text, when);
}
//重写onBackpressed事件
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
//取消通知
mNotificationManager.cancel(0);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
}
上面的代码我们定义了一个方法createNotification(String text).该方法是用于创建一个通知。注意之所以这样写是因为。不管是普通的通知还是自定义的通知。前面的创建过程都是一样的。然后我们实现了两个按钮的点击事件。
运行结果如下所示:
当点击两个按钮时会出现以下的情况:
注 :这里是有声音效果的。如果手机支持闪光,还有LED效果。


猜你喜欢
- Java内部类(Inner Class),类似的概念在C++里也有,那就是嵌套类(Nested Class),乍看上去内部类似乎有些多余,它
- 1.背景倒计时的效果在网站或其他平台看到的很多了吧,今天就让我们来看看在OpenHarmony中如何实现它吧!2.效果预览视频效果演示传送门
- 定义:结点的带权路径长度为从该结点到树根之间的路径长度与结点上权的乘积。树的带权路径长度为树中所有叶子结点的带权路径长度之和。假设有n个权值
- 本文实例为大家分享了Java文件操作的具体代码,供大家参考,具体内容如下简介本程序主要采用了FileInputStream和FileOutp
- 综述Android中的事件分发机制也就是View与ViewGroup的对事件的分发与处理。在ViewGroup的内部包含了许多View,而V
- java 闰年判断前言:给定一个年份,判断这一年是不是闰年。当以下情况之一满足时,这一年是闰年:1. 年份是4的倍数而不是100的倍数;2.
- 本文实例讲述了Android编程实现分页加载ListView功能。分享给大家供大家参考,具体如下:package eoe.listview;
- 本例子演示如何添加一个简单的单页导航,在此基础上,再演示如何在第2个页面中显示第1个页面中拨打过的所有电话号码。(1)通过该例子理解Andr
- springboot集成开发实现商场秒杀加入主要依赖<dependency> <groupId>org.spring
- 1. 编写索引内容节点解释:settings:配置信息"number_of_replicas": 0 不需要备份(单节点
- Spring-Data-Redis项目(简称SDR)对Redis的Key-Value数据存储操作提供了更高层次的抽象,类似于Spring F
- C#判断数据类型的简单示例代码:int i = 5; Console
- (1)对于public修饰符,它具有最大的访问权限,可以访问任何一个在CLASSPATH下的类、接口、异常等。它往往用于对外的情况,也就是对
- Android webview旋转屏幕导致页面重新加载问题解决办法1. 在create时候加个状态判断protected void onCr
- 本文实例讲述了Android编程使用LinearLayout和PullRefreshView实现上下翻页功能的方法。分享给大家供大家参考,具
- 这里给大家带来的是动态webservice调用接口并读取解析返回结果的具体示例,非常的简单,注释也很详细,小伙伴们可以参考下。using S
- 1. 前言不知道小伙伴对于日期字段,在项目中都是如何处理的,是单独给每个字段都自定义日期格式还是做全局格式设置?这个我之前啊,是
- 老大让我check out 一个分支,可我在idea 右下角找了半天也没找到最后才发现:因为是刚创建的分支,我得先更新一下项目,连这个都不懂
- 在android开发中,通常使用xml格式来描述布局文件。就目前而言,熟悉android布局及美化的人员少之又少,出现了严重的断层。大部分企
- 最近项目里面有个地方是在前面用glide加载图片后,后面再另外一个地方加载相同图片时没有复用glide的缓存,而是自己另外又重新缓存了一套。