本文实现一个功能:点击一个按钮,发送一个系统通知功能

添加一个Activity

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main36Activity"> <Button
android:id="@+id/button40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示通知" />
</android.support.constraint.ConstraintLayout>

MainActivity.java:

 package com.example.chenrui.app1;

 import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button button = findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); String channelId = "app1";
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,"app1",NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
} Notification notification = new NotificationCompat.Builder(MainActivity.this,channelId)
.setContentTitle("通知标题")
.setContentText("通知正文")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
.build();
manager.notify(1,notification);
}
});
}
}

注意第28-31行的代码,Android8.0及以上版本,要发送通知,需要配置通知频道,不然无法成功发送通知。

上面的通知,在点击的时候,不会有任何反应,下面我们把代码修改一下,允许在点击的时候进入一个Activity活动,注意红色字体为新增的代码:

 package com.example.chenrui.app1;

 import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button button = findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); String channelId = "app1";
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,"app1",NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
} Intent intent = new Intent(MainActivity.this,Main2Activity.class);
PendingIntent pi = PendingIntent.getActivity(MainActivity.this,0,intent,0); Notification notification = new NotificationCompat.Builder(Main2Activity.this,channelId)
.setContentTitle("通知标题")
.setContentText("通知正文")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
.setContentIntent(pi)
.setAutoCancel(true)
.build();
manager.notify(1,notification);
}
});
}
}

PendingIntent可以理解为一种延时执行的Intent,当点击通知项的时候进入到对应的Activity中。

更多属性的使用:

.setDefaults(NotificationCompat.DEFAULT_ALL)

使用通知的默认效果,这个属性会根据当前手机的环境决定播放什么铃声,以及如何震动等

.setPriority(NotificationCompat.PRIORITY_MAX)

设置通知的优先级,设置为NotificationCompat.PRIORITY_MAX是最高级,会以横幅的方式显示通知,部分手机可能禁用了横幅通知,需要手工开启

.setStyle(new NotificationCompat.BigTextStyle().bigText("通知内容"))

如果内容过多的话,默认会显示成省略号,如果要显示大文本信息,可以使用setStyle,用于显示大文本

android中使用通知功能的更多相关文章

  1. Android中的通知—Notification 自定义通知

    Android中Notification通知的实现步骤: 1.获取NotificationManager对象NotificationManager的三个公共方法:①cancel(int id) 取消以 ...

  2. 5分钟实现Android中更换头像功能

    写在前面:更换头像这个功能在用户界面几乎是100%出现的.通过拍摄照片或者调用图库中的图片,并且进行剪裁,来进行头像的设置.功能相关截图如下: 下面我们直接看看完整代码吧: 1 2 3 4 5 6 7 ...

  3. laravel中消息通知功能

    以laravel5.5为例子,这个功能laravel自带的有: 1.生成表文件的migration文件,再migrate一下在数据库里生成表.命令为:php artisan notifications ...

  4. android中实现拨号功能

    1.要实现拨号功能,首先需要开启拨号权限 修改AndroidManifest.xml文件,添加如下内容: <uses-permission android:name="android. ...

  5. iOS实现Android中Gone的功能

    实现隐藏view但不占位置的需求是很常见的(Android里的view.GONE),可iOS里并没有这玩意,只有hidden.于是自己写了一个一般情况下用的category,特殊情况就得看情况做了.其 ...

  6. I.MX6 android 移除shutdown功能

    /************************************************************************ * I.MX6 android 移除shutdown ...

  7. Android中滑屏初探 ---- scrollTo 以及 scrollBy方法使用说明

    今天给大家介绍下Android中滑屏功能的一个基本实现过程以及原理初探,最后给大家重点讲解View视图中scrollTo 与 scrollBy这两个函数的区别 . 首先 ,我们必须明白在Android ...

  8. android中TabHost和RadioGroup

    android底部菜单应用 博客分类: android--UI示例 TabHostMenuRadioGroupButton  在android中实现菜单功能有多种方法. Options Menu:用户 ...

  9. Android 中实现分享和第三方登陆---以新浪微博为例

    第三方登陆和分享功能在目前大部分APP中都有,分享功能可以将自己觉得有意义的东西分享给身边的朋友,而第三方登陆可以借助已经有巨大用户基础的平台(如QQ和新浪微博)的账号,让用户在使用自己APP的时候不 ...

随机推荐

  1. Activity的启动模式详解

    Activity的启动模式详解 Activity有四种载入模式:standard(默认), singleTop, singleTask和 singleInstance. (1).standard(默认 ...

  2. AngularJS表单验证,手动验证或自动验证

    AngularJS的表单验证大致有两种,一种是手动验证,一种是自动验证. 手动验证 所谓手动验证是通过AngularJS表单的属性来验证.而成为AngularJS表单必须满足两个条件:1.给form元 ...

  3. delphi中单独编译pas生成dcu文件

    delphi中单独编译pas生成dcu文件 在网上下载了一个带源码的组件,结果碰到提示说缺少xxx.dcu.一看它的目录下确实没有,那能不能生成一个呢? 当然可以! 方法是使用delphi的安装目录\ ...

  4. Javascript 中的arguments

    arguments是当前正在执行的function的一个参数,它保存了函数当前调用的参数.   使用方法:function.arguments[i]. 其中function.是可选项,是当前正在执行的 ...

  5. Linux学习17-gitlab访问慢502问题优化

    前言 浏览器访问gitlab的web页面,发现非常慢,并且很容易出现502问题.其中一个原因就是8080端口被tomcat占用,前面一篇已经更换了端口,但还是很慢. 后来搜了下,原因是gitlab占用 ...

  6. Linux、apache 无法使用PHP创建目录和文件

    因为项目的需要,这几天搭建了虚拟机,环境是centos7+lamp,可是搭建好网站后,即使把权限放开了(777),我试了改父文件夹权限:重新创建文件夹,改权限再移动文件:更换文件夹的属主.统统不行.这 ...

  7. Java 如何实现在线预览文档及修改(文本文件)

    暂时未解决的问题:多用户并发修改一个文件 测试地址: http://sms.reyo.cn 用户名:aa 密码:123456

  8. 在EditText中添加QQ表情

    本文参考自:http://blog.csdn.net/wulianghuan/article/details/8583921 在输入框中输入表情是每个聊天软件的必备功能,做到这点仅需要将表情放入工程图 ...

  9. SpiderMonkey js引擎的静态编译与使用, SpiderMonkey的使用

    SpiderMonkey js引擎的静态编译与使用 2017年10月02日 02:51:22 yaolixing01 阅读数:536   原文出处: http://yaolixing.oltag.co ...

  10. crc16.c

    static unsigned char auchCRCHi[];static unsigned char auchCRCLo[]; /* CRC 高位字节值表 */static unsigned c ...