从零开始学android<Notification通知.四十四.>

从零开始学android<Notification通知.四十四.>在android中有时会在主界面上收到某些应用的推送,有的可以包含图片,声音或者震动效果,当点击这些提示时,有时还可以进入到发送提示的的应用。这些提示的推送就是通知,当然通知早根本上也是你一种服务。首先想要使用通知就必须使用到Notification.Builder和NotificationManager这两个类使用Notification.Builder来取

大家好,又见面了,我是你们的朋友全栈君。

在android中有时会在主界面上收到某些应用的推送,有的可以包含图片,声音或者震动效果,当点击这些提示时,有时还可以进入到发送提示的的应用。

这些提示的推送就是通知,当然通知早根本上也是你一种服务。

首先想要使用通知就必须使用到Notification.Builder 和NotificationManager这两个类

使用Notification.Builder来取得Notification对象,使用NotificationManager来取得操作通知的对象

当然,我们也可以使用RemoteViews来获得自定义View来创建自定义的通知

接下来就让我们一起用例子来学习通知的使用吧

主界面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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:text="原始通知" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="53dp"
        android:text="自定通知" />

</RelativeLayout>

通知界面

<?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=" 主界面哦"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

自定义通知界面

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/b12" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/imageView1"
        android:text="大学橙色预警"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_toRightOf="@+id/imageView1"
        android:text="部分地区降雨达到500ml" />

</RelativeLayout>

主文件

package com.example.notification1;

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.widget.Button;
import android.widget.RemoteViews;

public class MainActivity extends Activity {
	private Button button1, button2;
	private Notification.Builder builder;
	private NotificationManager manager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button1 = (Button) this.findViewById(R.id.button1);
		button2 = (Button) this.findViewById(R.id.button2);
		builder = new Notification.Builder(this);// 创建一个通知设置
		manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

		button1.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				PendingIntent pendingIntent = PendingIntent.getActivity(
						MainActivity.this, 0, new Intent(MainActivity.this,
								Main.class), 0);// 获得pendingIntent意图
				builder.setContentIntent(pendingIntent);
				builder.setSmallIcon(R.drawable.b12);
				builder.setTicker("您有新通知");
				builder.setContentTitle("降雨橙色预警");
				builder.setContentText("下午5时降雨将会达到500ml,请市民注意防范");
				builder.setContentInfo("暴雨,大风");
				builder.setSubText("本月底7次大雨");
				builder.setDefaults(Notification.DEFAULT_SOUND);// 设置默认提示生啊
				long[] vibrate = { 100l, 1000l, 100l, 1000l, 10l, 1000l };// 设置震动的频率
				builder.setVibrate(vibrate);// 设置自定义震动
				Notification notification = builder.build();
				manager.notify(1000, notification);// 开启通知
			}
		});
//自定义通知
		button2.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				RemoteViews contentRemoteViews = new RemoteViews(
						getPackageName(), R.layout.custom);// 获取自定义的layout
				Intent intent = new Intent(MainActivity.this, Main.class);
				PendingIntent conPendingIntent = PendingIntent.getActivity(
						MainActivity.this, 0, intent, 0);
				builder.setContent(contentRemoteViews);
				builder.setContentIntent(conPendingIntent);
				Notification notification = builder.build();
				manager.notify(1001, notification);
			}
		});
	}

}

通知界面JAVA文件

package com.example.notification1;

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

public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	super.setContentView(R.layout.main);
}
}


从零开始学android<Notification通知.四十四.>

从零开始学android<Notification通知.四十四.>

点击通知

从零开始学android<Notification通知.四十四.>

点击自定义通知

从零开始学android<Notification通知.四十四.>

通过本节的学习,希望读者能够掌握原始通知的使用和自定义通知的配置和使用。

下节预报:BroadCast广播

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/149348.html原文链接:https://javaforall.net

(0)
上一篇 2022年6月16日 下午9:16
下一篇 2022年6月16日 下午9:16


相关推荐

  • Flyway的使用

    Flyway的使用Flyway 的使用环境 SpringBoot2 0 4 RELEASE 为什么要用 Flyway 开发人员在合作的时候经常遇到以下场景 1 开发人员 A 在自己的本地数据库做了一些表结构的改动 并根据这些改动调整了 DAO 层的代码 然后将代码上传到 svn 或 git 等版本控制服务器上 此时如果开发人员 B 拉取了 A 的代码改动 在运行项目的时候很可能会报错 因为 B 的本地 SQL 数据库并没有修改 2 在项目上

    2026年3月16日
    1
  • [转]深邃之思想,纯粹之灵魂——我所了解的柳智宇学长

    [转]深邃之思想,纯粹之灵魂——我所了解的柳智宇学长https://zhuanlan.zhihu.com/p/32340797柳智宇学长文集前言部分深邃之思想,纯粹之灵魂——我所了解的柳智宇学长——2017年12月25日中国.湖北.武汉.华中科技大学【陆秋宇】正文部分读到自己高二写的这篇文章——2005年中国.湖北.武汉.华中师大一附中远慰风雨夕——2006年7月斯洛文尼亚.卢布尔雅那.第47届IMO前夜我在大学的成长历程…

    2022年4月30日
    50
  • vue 修饰符

    vue 修饰符防止执行预设的行为 如果事件可取消 则取消该事件 而不停止事件的进一步传播 只会触发自己范围内的事件 不包含子元素 事件目标对象是自己 才会触发 vue 中 number 修饰符的作用是将输入框内的字符串转换成数字 vue 中 trim 修饰符的作用是将输入框内中前后的空格去掉 在 change 而非 input 时更新 与事件冒泡的方向相反 等同于 js 中的

    2026年3月18日
    2
  • OpenClaw网络安全及系统安全完整指南

    OpenClaw网络安全及系统安全完整指南

    2026年3月13日
    3
  • java进程通信方式_Java进程间通信方式

    java进程通信方式_Java进程间通信方式进程间通信又称 IPC Inter ProcessCommu 指多个进程之间相互通信 交换信息的方法 根据进程通信时信息量大小的不同 可以将进程通信划分为两大类型 1 低级通信 控制信息的通信 主要用于进程之间的同步 互斥 终止和挂起等等控制信息的传递 2 高级通信 大批数据信息的通信 主要用于进程间数据块数据的交换和共享 常见的高级通信有管道 消息队列 共享内存等 进程间

    2026年3月18日
    2
  • redis五大类型用法

    redis五大类型用法

    2022年3月2日
    36

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号