android中使用通知功能
本文实现一个功能:点击一个按钮,发送一个系统通知功能
添加一个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中使用通知功能的更多相关文章
- Android中的通知—Notification 自定义通知
Android中Notification通知的实现步骤: 1.获取NotificationManager对象NotificationManager的三个公共方法:①cancel(int id) 取消以 ...
- 5分钟实现Android中更换头像功能
写在前面:更换头像这个功能在用户界面几乎是100%出现的.通过拍摄照片或者调用图库中的图片,并且进行剪裁,来进行头像的设置.功能相关截图如下: 下面我们直接看看完整代码吧: 1 2 3 4 5 6 7 ...
- laravel中消息通知功能
以laravel5.5为例子,这个功能laravel自带的有: 1.生成表文件的migration文件,再migrate一下在数据库里生成表.命令为:php artisan notifications ...
- android中实现拨号功能
1.要实现拨号功能,首先需要开启拨号权限 修改AndroidManifest.xml文件,添加如下内容: <uses-permission android:name="android. ...
- iOS实现Android中Gone的功能
实现隐藏view但不占位置的需求是很常见的(Android里的view.GONE),可iOS里并没有这玩意,只有hidden.于是自己写了一个一般情况下用的category,特殊情况就得看情况做了.其 ...
- I.MX6 android 移除shutdown功能
/************************************************************************ * I.MX6 android 移除shutdown ...
- Android中滑屏初探 ---- scrollTo 以及 scrollBy方法使用说明
今天给大家介绍下Android中滑屏功能的一个基本实现过程以及原理初探,最后给大家重点讲解View视图中scrollTo 与 scrollBy这两个函数的区别 . 首先 ,我们必须明白在Android ...
- android中TabHost和RadioGroup
android底部菜单应用 博客分类: android--UI示例 TabHostMenuRadioGroupButton 在android中实现菜单功能有多种方法. Options Menu:用户 ...
- Android 中实现分享和第三方登陆---以新浪微博为例
第三方登陆和分享功能在目前大部分APP中都有,分享功能可以将自己觉得有意义的东西分享给身边的朋友,而第三方登陆可以借助已经有巨大用户基础的平台(如QQ和新浪微博)的账号,让用户在使用自己APP的时候不 ...
随机推荐
- c#开发activex注册问题
最近使用c#开发activex,遇到一个问题,生成的dll文件在本地可以嵌入到web里面,但是到其他机器上就会出现activex无法加载的情况,页面里面出现一个红色的X.mfc开发的activex是使 ...
- WIN8系统中 任务管理器 性能栏 显示CPU利用率(已暂停)怎么回事?
解决办法: 点上方的 查看--更新速度--普通
- Android的学习之路(三)项目的启动过程和安装过程具体解释
应用的安装和启动过程: 安装:第一步:java的编译器会把这个.java文件编译成.class文件 第二部:Android的SDK提供了一个dx工具,这个工具把.class文件转义 ...
- 设计模式之代理模式之二(Proxy)
from://http://www.cnblogs.com/xwdreamer/archive/2012/05/23/2515306.html 设计模式之代理模式之二(Proxy) 0.前言 在前 ...
- h5语音录制及上传(Java版语音聊天系统)
Since Chrome version 47, Voice Recording works only on HTTPS sites 目前基于webikit(谷歌之类的webikit)和Gecko(F ...
- pio 背景色
This example shows you Excel cell fills and colors using Apache POI. In our example i have used all ...
- [转] OpenStack IPSec VPNaaS
OpenStack IPSec VPNaaS ( by quqi99 ) 作者:张华 发表于:2013-08-03版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声 ...
- SpiderMonkey js引擎的静态编译与使用, SpiderMonkey的使用
SpiderMonkey js引擎的静态编译与使用 2017年10月02日 02:51:22 yaolixing01 阅读数:536 原文出处: http://yaolixing.oltag.co ...
- 使用强大的 Mockito 测试框架来测试你的代码
原文链接 : Unit tests with Mockito - Tutorial 译文出自 : 掘金翻译计划 译者 : edvardhua 校对者: hackerkevin, futureshine ...
- Java Run-Time Data Areas
前言 本文主要介绍JVM的运行时数据区 来自Oracle文档 Java Virtual Machine Specification -- Chapter 2. The Structure of the ...