本文实例讲解了通知Notification使用方法,此知识点就是用作通知的显示,包括振动、灯光、声音等效果,分享给大家供大家参考,具体内容如下

效果图:

MainActivity:

import java.io.File;

import android.app.Activity;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.Intent;

import android.graphics.Color;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

private Button sendNotice;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

sendNotice = (Button) findViewById(R.id.send_notice);

sendNotice.setOnClickListener(this);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.send_notice:

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

//创建notification对象来存储通知所需的各种信息

//第一个参数为图标

//第二个参数用于指定通知的ticker内容

//第三个参数用于指定通知被创建的时间,以毫秒为单位

Notification notification = new Notification(

R.drawable.ic_launcher, "This is ticker text",

System.currentTimeMillis());

//此处设置点击的activity的跳转

//第一个参数依旧是Context

//第二个参数一般用不到,所以用0表示取默认值

//第三个参数就是一个Intent对象

//FLAG_CANCEL_CURRENT:如果当前系统中已经存在一个相同的PendingIntent对象,

// 那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。

Intent intent = new Intent(this, NotificationActivity.class);

PendingIntent pi = PendingIntent.getActivity(this, 0, intent,

PendingIntent.FLAG_CANCEL_CURRENT);

//设置通知的布局

//第一个参数为Context

//第二个参数用于指定通知的标题

//第三个参数用于指定通知的征文内容

//第四个参数用于传入PendingIntent对象,用于设置点击效果

notification.setLatestEventInfo(this, "This is content title",

"This is content text", pi);

// //设置在通知发出的时候的音频

// Uri soundUri = Uri.fromFile(new File("/system/media/audio/ringtones/Basic_tone.ogg"));

// notification.sound = soundUri;

//

// //设置手机震动

// //第一个,0表示手机静止的时长,第二个,1000表示手机震动的时长

// //第三个,1000表示手机震动的时长,第四个,1000表示手机震动的时长

// //此处表示手机先震动1秒,然后静止1秒,然后再震动1秒

// long[] vibrates = {0, 1000, 1000, 1000};

// notification.vibrate = vibrates;

//

// //设置LED指示灯的闪烁

// //ledARGB设置颜色

// //ledOnMS指定LED灯亮起的时间

// //ledOffMS指定LED灯暗去的时间

// //flags用于指定通知的行为

// notification.ledARGB = Color.GREEN;

// notification.ledOnMS = 1000;

// notification.ledOffMS = 1000;

// notification.flags = Notification.FLAG_SHOW_LIGHTS;

//如果不想进行那么多繁杂的这只,可以直接使用通知的默认效果

//默认设置了声音,震动和灯光

notification.defaults = Notification.DEFAULT_ALL;

//使用notify将通知显示出来

//第一个参数是id,要爆炸为每个通知所指定的id是不同的

//第二个参数就是Notification对象

manager.notify(1, notification);

break;

default:

break;

}

}

}

activity_main:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/send_notice"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="发出通知"

/>

NotificationActivity:

import android.app.Activity;

import android.app.NotificationManager;

import android.os.Bundle;

public class NotificationActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.notification_layout);

//打开NotificationActivity这个Activity后把通知给关掉

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

manager.cancel(1);

}

}

notification_layout:

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:textSize="24sp"

android:text="这是通知点击后的界面"

/>

AndroidManifest:

package="com.example.notificationtest"

android:versionCode="1"

android:versionName="1.0" >

android:minSdkVersion="14"

android:targetSdkVersion="19" />

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

android:name="com.example.notificationtest.MainActivity"

android:label="@string/app_name" >

android8 通知呼吸灯_Android中通知Notification使用实例(振动、灯光、声音)相关推荐

  1. android8 通知呼吸灯_android学习笔记----解决兼容8.0以上和8.0之前版本通知栏显示、振动、LED呼吸灯闪烁问题(真机验证)...

    Android 8.0系统的通知栏适配文章讲解(郭霖大神的): 然后开始试验了: 模拟器: 真机(华为荣耀V9,8.0系统),下拉横幅需要手动打开,除非是厂家白名单,比如QQ.微信 我在oppo手机6 ...

  2. android8 通知呼吸灯_Android8.0及以上的Notification

    这篇文章上次修改于 702 天前,可能其部分内容已经发生变化,如有疑问可询问作者. 在新版本上(Android8.0及以上)开发时,会遇到一些问题,比如,不显示通知,Notification 声音不可 ...

  3. android8 通知呼吸灯_android8 通知呼吸灯_说说8.0下 Android 通知(Notification)

    当运用程序不在前台运转,这时便借助关照( Notification )向用户发送少许提醒消息. 发出关照后,手机非常上方的状况栏中就会表现一个关照图标,下拉状况栏就会看到关照的细目. 1 根基用法 / ...

  4. android8 通知呼吸灯_说说8.0下 Android 通知(Notification)

    当运用程序不在前台运转,这时便借助关照( Notification )向用户发送少许提醒消息. 发出关照后,手机非常上方的状况栏中就会表现一个关照图标,下拉状况栏就会看到关照的细目. 1 根基用法 / ...

  5. android8 通知呼吸灯_正在消失的功能,为什么越来越多的手机没有呼吸灯?你知道原因吗...

    正在消失的功能,为什么越来越多的手机没有呼吸灯?你知道原因吗 其实说到手机呼吸灯,大家应该都不陌生,毕竟如果你有心心念念的人儿,每次呼吸灯闪烁,都会迫不及待的打开手机查看消息,想看看是不是TA给自己发 ...

  6. android8 通知呼吸灯_手机没有呼吸灯?这款APP帮你实现手机通知“可视化”

    7月6日消息,现在越来越多的手机因为全面屏设计导致了LED通知灯被移除.对于这一问题,外媒Phone Arena称,一位安卓程序开发人员开发了一款名为"通知好友"(Notify B ...

  7. android8 通知呼吸灯_手机呼吸灯那么好用!为什么要取消它?没有呼吸灯真的好吗?...

    说起手机的呼吸灯相信大家应该都是不会陌生吧,当我们手机内有消息的时候,呼吸灯就会亮起,甚至是有些手机的呼吸灯还可以因为不同的消息设置不同的提示颜色,随着智能手机的发展,相比过去单调的设计,有的厂商为其 ...

  8. xamarin android 通知,在 Xamarin.Android 中使用 Notification.Builder 构建通知

    0 背景 在 Android 4.0 以后,系统支持一种更先进的 Notification.Builder 类来发送通知.但 Xamarin 文档含糊其辞,多方搜索无果,遂决定自己摸索. 之前的代码: ...

  9. android mvp模式例子_Android中mvp模式使用实例详解

    MVP 是从经典的模式MVC演变而来,它们的基本思想有相通的地方:Controller/Presenter负责逻辑的处理,Model提供数据,View负 责显示.作为一种新的模式,MVP与MVC有着一 ...

最新文章

  1. 海康 安全码 修改密码_手机为什么要设置PIN码 手机设置PIN码的原因【介绍】
  2. Flutter快速入门 五步搞定Flutter环境配置
  3. CLOSE_WAIT状态的原因与解决方法 --转
  4. android SQLite查询并显示用户输入的选择信息
  5. 【转载】C#中List集合使用Exists方法判断是否存在符合条件的元素对象
  6. laravel-admin 使用 wangEditor 的一些小方法
  7. Go 的新关键字 any 是个啥
  8. js页面自适应屏幕大小_Web页面适配移动端方案研究
  9. 高大上!手把手教你在京东云擎上部署个人应用!
  10. 外联样式表添加到html中,CSS联样式表之内联式、外联式和嵌入式
  11. Java中,成员内部类的常见修饰符及应用 成员内部类不是静态的,访问的格式...
  12. 数据集下载地址(转)以下内容转自https://baijiahao.baidu.com/s?id=1615853849218131902wfr=spiderfor=pc
  13. java实现屏幕共享功能_屏幕共享功能的应用
  14. 数学分析教程 第十八章学习感受
  15. 强烈推荐一款好用的API接口
  16. ubuntu下使用vscode阅读内核源码或uboot源码使用技巧——search.excludefiles.exclude
  17. spring mysql 事务回滚失败_Spring事务回滚失败
  18. 永恒之蓝 ms17_010漏洞
  19. [转]关于 UTC , GMT 和 BST 夏令时
  20. 红米note3图片剪裁bug

热门文章

  1. Python调整图像亮度和饱和度
  2. ATS 5.3.0命令行工具traffic_via
  3. 两个关于水花的测试。
  4. 继承实现的原理、子类中调用父类的方法、封装
  5. opencv下指定文件夹下的图片灰度化(图片的读取与保存)-------简单记录
  6. netty集成ssl完整参考指南(含完整源码)
  7. 【Python之路】第二篇--初识Python
  8. 挖坑挖到cnblogs.com来...
  9. ref与out的区别
  10. [原]three.js 地形纹理混合