每天进步一点点-<Notification>

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">使用Notification,必须了解三个类:</span>

import android.app.PendingIntent;
import android.app.NotificationManager;
import android.app.Notification;
首先先来看一段代码,根据这段代码来具体学习这三个类:

NotificationManager manager = (NotificationManager) this
	.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.icon = R.drawable.ic_launcher;
notification.tickerText = "注意了,我出现在状态栏上了";
notification.defaults=Notification.DEFAULT_SOUND;
notification.audioStreamType= android.media.AudioManager.ADJUST_LOWER;
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(this, "内容提示:", "我就是一个测试文件", pendingIntent);
manager.notify(1, notification);

这里把三个类全部使用上了,下面就具体介绍下每个类的使用方法:

NotificationManager,一个系统后台服务,用于显示,关闭Notification。

获取实例:
NotificationManager nm = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);

使用方法:


很简单,明了,不再多说。

Notification

设置 Notification 的各种属性:
   //设置通知在状态栏显示的图标
   m_Notification.icon = R.drawable.icon;
   //当我们点击通知时显示的内容
   m_Notification.tickerText = "Button1 通知内容.....";     
   //通知时发出的默认声音
   m_Notification.defaults = Notification.DEFAULT_SOUND;
   //设置通知显示的参数
   Intent m_Intent = new Intent(this, DesActivity.class); 
   PendingIntent m_PIntent = PendingIntent.getActivity(NotificationDemo.this, 0, m_Intent, 0);
   m_Notification.setLatestEventInfo(this, "Button1", "Button1通知", m_PIntent );
   //这个可以理解为开始执行这个通知
   m_NotificationManager.notify(0, m_Notification);
 
提醒的更新
每一个提醒都对应有一个唯一标识符ID,在发布提醒时指定的:
NotificationManager.notify(ID, notification)   
在更新提醒的时候,只需要在修改Notification的一些属性之后,再调用其setLatestEventInfo(),然后重新发送一次通知即可。
 
Notification丰富的提示方式
a) tickerText :显示文本提示
    notification.tickerText = "hello";
b) sound :发出提示音
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
    notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
c) vibrate :手机振动
     notification.defaults |= Notification.DEFAULT_VIBRATE;
     long[] vibrate = {0,100,200,300};
     notification.vibrate = vibrate;
d) ledARGB 、ledOnMS 、ledOffMS :LED灯闪烁
     notification.defaults |= Notification.DEFAULT_LIGHTS;
     notification.ledARGB = 0xff00ff00;
     notification.ledOnMS = 300;
     notification.ledOffMS = 1000;
     notification.flags |= Notification.FLAG_SHOW_LIGHTS;
 
声音提醒
·使用默认声音
  notification.defaults |= Notification.DEFAULT_SOUND;
·使用自定义声音
  notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
·注:如果定义了默认声音,那么自定义声音将被覆盖
 
振动提醒
·使用默认振动
  notification.defaults |= Notification.DEFAULT_VIBRATE;
·使用自定义振动
  long[] vibrate = {0,100,200,300}; 
  notification.vibrate = vibrate;
·注:如果定义了默认振动,那么自定义振动将被覆盖
 
灯光闪烁提醒
·使用默认闪烁
  notification.defaults |= Notification.DEFAULT_LIGHTS;
·使用自定义闪烁
  notification.ledARGB = 0xff00ff00; // LED灯的颜色,绿灯
  notification.ledOnMS = 300; // LED灯显示的毫秒数,300毫秒
  notification.ledOffMS = 1000; // LED灯关闭的毫秒数,1000毫秒
  notification.flags |= Notification.FLAG_SHOW_LIGHTS; // 必须加上这个标志
 
更多特性
可以通过 Notification 的相关字段或标志(flags)为提醒设置更多的特性。
·FLAG_AUTO_CANCEL 标志:当提醒被用户点击之后会自动被取消(cancel);
·FLAG_INSISTENT 标志:在用户响应之前会一直重复提醒音;
·FLAG_ONGOING_EVENT 标志:Add this to the flags field to group the notification under
  the "Ongoing" title in the Notifications window.
·FLAG_NO_CLEAR 标志:当在提醒栏中点击“清除提醒”按钮时,该提醒将不会被清除;

PendingIntent

参见:http://blog.sina.com.cn/s/blog_48a45b950100udor.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你帮我检查下我的代码吧! plugin.xml内容如下: ``` <idea-plugin> <id>com.your.company.unique.plugin.id</id> <name>wkx_plugin</name> <version>1.0</version> <vendor email="wangkexin6@yourcompany.com" url="http://www.hikvision.com">HikVision</vendor> <!-- description><![CDATA[ xml和excel互相转换插件.<br> www.wkx666.com</description --> <!-- change-notes><![CDATA[ Add change notes here.<br> most HTML tags may be used ]]> </change-notes--> <!-- please see https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description --> <idea-version since-build="173.0"/> <!-- please see https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html on how to target different products --> <depends>com.intellij.modules.platform</depends> <extensions defaultExtensionNs="com.intellij"> <!-- Add your extensions here --> <actions> <action id="myAction" class="com.example.HelloWorldAction" text="My Action" description="My description"> <add-to-group group-id="MainMenu.Tools" anchor="last"/> <keyboard-shortcut first-keystroke="control alt A"/> </action> </actions> </extensions> <actions> <!-- Add your actions here --> </actions> </idea-plugin> ``` HelloWorldAction.kt内容如下: ``` import com.intellij.notification.NotificationDisplayType import com.intellij.notification.NotificationGroup import com.intellij.notification.NotificationType import com.intellij.openapi.actionSystem.AnAction import com.intellij.openapi.actionSystem.AnActionEvent class HelloWorldAction : AnAction() { override fun actionPerformed(event: AnActionEvent) { //这里创建了一个消息提示弹窗,在IDE中展示“Hello World” val notificationGroup = NotificationGroup( "myActionId", NotificationDisplayType.BALLOON, true ) val notification = notificationGroup.createNotification( "chentao Demo", "Hello World", "", NotificationType.INFORMATION ).notify(event.project) //从方法的Event对象中获取到当前IDE正在展示的project,在该project中展示弹窗 } } ```
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值