ObjectNotificationService(v0.1)
- 格式:ppt
- 大小:99.00 KB
- 文档页数:8
android studio notice用法
在Android Studio中,通知(Notification)是一种常用的提醒用户的方式,可以在App运行过程中主动向用户推送消息。
Notification类描述了消息通知的组成内容,包括消息图标、消息标题、消息内容等基本元素,以及附加文本、进度条、计时器等额外元素。
实际推送工作还需要由通知管理器NotificationManager执行。
以下是一个简单的使用Notification的示例:
1. 创建一个Notification对象,并设置其各种属性,如标题、内容、图标等。
2. 创建一个PendingIntent对象,该对象指定了当用户点击通知时应该打开的Activity或执行的操作。
3. 使用NotificationManager来发送Notification。
需要注意的是,由于Android系统版本的更新,Notification的使用方式也在不断变化。
因此,在编写代码时需要考虑到不同版本的兼容性。
同时,对于Android 及以上版本,由于引入了新的通知渠道(Notification Channels)的概念,因此需要在创建通知前先创建一个通知渠道。
通知渠
道用于定义通知的属性和行为,例如声音、振动和优先级等。
此外,为了确保通知能够正常显示,还需要在文件中添加必要的权限和声明。
例如,如果要在后台播放音乐或发送网络请求时显示通知,需要在Manifest中添加相应的权限。
总之,在Android Studio中正确使用Notification需要考虑到多个方面,包括API的调用、权限的声明以及不同版本的兼容性等。
Notification(Notification的通知栏常驻、Notification的各种样式、Notification点击无效、Notification禁止滑动删除)Android的Notification是android系统中很重要的一个机制,产品人员常常利用通知栏的方式,跟用户进行弱沟通。
拥有推送通知的app要比没有此类功能的app活跃率要高很多。
另外类似于墨迹天气,清理大师等app,也会将通知栏常驻,利用自定义的布局,方便用户及时快捷的查看所需的信息和使用快捷的功能。
所以Notification的使用,也在开发当中,使用的越来越频繁。
今天我就来跟大家分享一下Notification的常用事项。
我不了解大家平时怎么使用Notification,我常常看到有些人的代码是这样写的:1.Notification notification=new Notification(notificationIcon, notificationTitle, when);2.3. notification.defaults=Notification.DEFAULT_ALL;4.5. Intent intent=new Intent(MainActivity.this,SecondActivity.class);6. PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this, 0, intent, 0);7. notification.setLatestEventInfo(this,"测试展开title", "测试展开内容",pendingIntent);8.。
具体的代码我就不贴全了,因为大家如果注意IDE的提示的话,就会发现,其实这是一种不推荐的用法,API的支持已经过时了。
最新的Notification的用法,是推荐使用V4包下的NotificationCompat.Builder,利用它,进行各种设置,具体的用法先别着急,我们慢慢道来。
AndroidStudioNotification(状态栏通知)详解1.设计⽂档部分解读1)Notification的基本布局上⾯的组成元素依次是:Icon/Photo:⼤图标Title/Name:标题Message:内容信息Timestamp:通知时间,默认是系统发出通知的时间,也可以通过setWhen()来设置Secondary Icon:⼩图标内容⽂字,在⼩图标的左⼿边的⼀个⽂字2)扩展布局在 Jelly Bean 中你可以为通知提供更多事件的细节。
你可以通过扩展布局显⽰消息的前⼏⾏或者图⽚的预览。
这样⽤户可以看多更多的内容 - 有时甚⾄可以看到整个消息。
⽤户可以通过 pinch-zoom 或者双⼿指滑动来打开扩展布局。
Android 为单条消息提供了两种扩展布局 (⽂字和图像) 供你开发应⽤时使⽤。
关于其他⼀些设计的东西,就不⼀⼀提及了,有兴趣的⾃⾏查看上⾯提供的API⽂档,知道下这个Notification在4.x以上的版本可以多种多样就好!我们更多的时候关注的是如何写代码使⽤这个东西,下⾯我们就来学习下Notification的⽤法!2.Notification的基本使⽤流程状态通知栏主要涉及到2个类:Notification 和NotificationManagerNotification:通知信息类,它⾥⾯对应了通知栏的各个属性NotificationManager:是状态栏通知的管理类,负责发通知、清除通知等操作。
使⽤的基本流程:Step 1. 获得NotificationManager对象: NotificationManager mNManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);Step 2. 创建⼀个通知栏的Builder构造类: Notification.Builder mBuilder = new Notification.Builder(this);Step 3. 对Builder进⾏相关的设置,⽐如标题,内容,图标,动作等!Step 4.调⽤Builder的build()⽅法为notification赋值Step 5.调⽤NotificationManager的notify()⽅法发送通知!PS:另外我们还可以调⽤NotificationManager的cancel()⽅法取消通知3.设置相关的⼀些⽅法:Notification.Builder mBuilder = new Notification.Builder(this);后再调⽤下述的相关的⽅法进⾏设置:(官⽅API⽂档:) 常⽤的⽅法如下:setContentTitle(CharSequence):设置标题setContentText(CharSequence):设置内容setSubText(CharSequence):设置内容下⾯⼀⼩⾏的⽂字setTicker(CharSequence):设置收到通知时在顶部显⽰的⽂字信息setWhen(long):设置通知时间,⼀般设置的是收到通知时的System.currentTimeMillis()setSmallIcon(int):设置右下⾓的⼩图标,在接收到通知的时候顶部也会显⽰这个⼩图标setLargeIcon(Bitmap):设置左边的⼤图标setAutoCancel(boolean):⽤户点击Notification点击⾯板后是否让通知取消(默认不取消)setDefaults(int):向通知添加声⾳、闪灯和振动效果的最简单、使⽤默认(defaults)属性,可以组合多个属性,Notification.DEFAULT_VIBRATE(添加默认震动提醒);Notification.DEFAULT_SOUND(添加默认声⾳提醒);Notification.DEFAULT_LIGHTS(添加默认三⾊灯提醒)Notification.DEFAULT_ALL(添加默认以上3种全部提醒)setVibrate(long[]):设置振动⽅式,⽐如:setVibrate(new long[] {0,300,500,700});延迟0ms,然后振动300ms,在延迟500ms,接着再振动700ms,关于Vibrate⽤法后⾯会讲解!setLights(int argb, int onMs, int offMs):设置三⾊灯,参数依次是:灯光颜⾊,亮持续时间,暗的时间,不是所有颜⾊都可以,这跟设备有关,有些⼿机还不带三⾊灯;另外,还需要为Notification设置flags为Notification.FLAG_SHOW_LIGHTS 才⽀持三⾊灯提醒!setSound(Uri):设置接收到通知时的铃声,可以⽤系统的,也可以⾃⼰设置,例⼦如下:.setDefaults(Notification.DEFAULT_SOUND) //获取默认铃声.setSound(Uri.parse("file:///sdcard/xx/xx.mp3")) //获取⾃定义铃声.setSound(Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "5")) //获取Android多媒体库内的铃声setOngoing(boolean):设置为ture,表⽰它为⼀个正在进⾏的通知。
金笛短信产品维护手册北京春笛网络信息技术服务有限公司 地址:北京海淀区知春路23号863软件园量子银座九层 邮编:100191电话:************,82356576,82356577金笛短信产品维护手册 产品版本V1.2 修订日期2020.04.01 作者 Zhangwei,s0z0y目录金笛短信产品维护手册 (1)一、VC中间件常见问题 (4)1.VC中间件安装问题 (4)1.1 创建注册表失败。
(4)1.2 安装路径问题。
(6)1.3在64位系统安装VC中间件mysql驱动程序时报缺少sfc.dll文件。
.. 82.VC中间件使用问题 (9)2.1 VC中间件配置好了ODBC源但是VC中间件界面找不到ODBC源名称 (9)2.2 ODBC源连接正常,启动服务异常 (9)2.3 VC中间件如何配置数据库的ODBC源 (11)2.4 VC中间件如何配置MySQL数据库的ODBC 源 (12)2.5 VC中间件如何配置Oracle 数据库的ODBC 源 (13)2.6 点击界面上的自动检测按钮,提示短信猫是否正常连接或短信猫已处于工作状态 (14)2.7 VC中间件单击日志表记录按钮,点击多次未响应 (15)2.8 如何查看VC中间件的运行日志 (15)2.9 VC中间件界面提交短信测试时,报向数据库表T_sendtask表添加记录失败 (16)2.10 5.1.101版本VC中间件日志表记录报cms error 304错误,短信卡住,不发送。
(16)2.11 VC中间件配置好数据库ODBC源后,在待发送列表短信显示是乱码 (16)2.12 一次性提交几百条信息时,VC中间件日志表记录报错误码34 (17)2.13 VC中间件安装,安装后启动服务,关闭界面,服务后台自动运行,如果在运行过程中非正常停止服务,则后台停止运行 (17)2.14 VC服务配置中间件,在提交短信后一直在在待发列表,显示准备发送或正在发送 (17)2.15 VC中间件使用,ODBC数据源配置正常,在VC界面上数据库测试连接正常,但点击“保存退出“时,提示拒绝访问 (18)2.16 查看VC中间件日志表记录时,报cms/cme error 常见错误码 (18)2.17短信猫设备装上之后,VC扫描不到com口 (19)2.18 短信猫设备装上之后,怎样判断检测设备状态是否正常 (20)2.19 短信猫设备装上之后,怎样测试设备能否发送短信 (21)2.20短信猫设备装上之后,检测工具检测AT标识,读卡部分未读卡,信号无信号 (23)2.21 以前老版本VC中间件启动服务时,弹窗提示“没有连接可用的短信设备” (23)2.22 配置mysql的odbc源的时候,列表中没有mysql驱动的选项 (23)2.23 VC中间件可以发,致远OA不能发 (24)2.24 VC中间件发短信看日志都是发送成功了,但是没收到短信 (24)二、WEB中间件常见问题 (25)1.WEB中间件安装问题 (25)2. WEB中间件使用常见问题 (27)2.1 Web中间件中启用设备时,设备状态一直在"启动中" (27)2.2 Web中间件在发送短信页面,点击"提交短信"按钮会弹出对话框提示"访问数据源失败,请确认数据源是否配置正确" (28)2.3 短信猫收到短信后,发现会不断地往数据库中重复插入数据 (28)2.4 web中间件设备配置界面检测不到短信猫 (29)2.5 授权码没注册 (30)2.6 web中间件如果启用/停止设备 (30)2.7 web中间件如何使用手机app (31)2.8 web中间件数据源设置界面连接测试数据库失败 (35)2.9 web中间件如何定时发送短信 (36)2.10 如何查看web中间件日志 (37)2.11 web中间件支持的txt文件格式 (37)2.12 web中间件支持的XLS电子表格文件格式 (38)2.13 web中间件支持的工资条电子表格文件格式 (39)2.14 web中间件服务程序如何重启 (40)2.15 web中间件URL发送短信接口密码在哪设置 (41)2.16 如何从表里删除web中间件的日志发送记录 (42)2.17 linux版web中间件安装后,Mysql启动失败,提示Starting MySQL. ERROR! Manager of pid-file quit without updatingfile! (42)2.18 web中间件运行发送短信一段时间后,短信一直在待发列表中 (43)2.19 web中间件发短信,待发时间和本地时间不一致 (43)三、二次开发包 (44)四、短信盒子 (44)一、VC中间件常见问题1.VC中间件安装问题1.1 创建注册表失败。
rio枚举过程以下是标题为"RIO枚举过程"的内容:RIO枚举过程RIO (Remote Insecure Object)枚举是一种针对Java RMI(Remote Method Invocation)服务的攻击方式,它利用Java序列化数据的特性来执行任意代码。
RIO枚举的过程主要分为以下几个步骤:1. 扫描目标主机需要扫描目标主机开放的TCP端口,通常Java RMI服务默认监听在1099端口。
可以使用Nmap等工具进行端口扫描。
2. 获取RMI注册表信息如果1099端口开放,就可以尝试连接RMI注册表并获取注册的服务信息。
可以使用各种工具或自己编写代码连接注册表并列出绑定的服务。
3. 下载RMI stub文件从注册表获取到绑定服务的stub文件路径后,就可以下载stub文件。
stub文件包含了服务的接口定义和远程引用信息。
4. 反编译stub文件下载的stub文件是Java字节码格式,需要使用反编译工具(如javap)将其反编译为可读的Java源代码。
5. 寻找可利用的gadget反编译后的Java代码中可能包含一些可利用的gadget(小工具),这些gadget可以被链接在一起形成利用链,从而实现任意代码执行。
6. 构造利用链根据找到的gadget,构造出可以执行任意代码的利用链。
利用链通常是一系列特殊构造的Java对象序列。
7. 触发利用链将构造好的利用链通过RMI服务传递给远程服务端,从而在服务端触发任意代码执行。
RIO枚举过程利用了Java RMI和序列化的特性,需要一定的Java安全知识和代码审计能力。
成功的RIO攻击可以在目标服务器上执行任意命令,从而控制整个系统。
因此,及时修补Java组件漏洞并限制不必要的RMI服务是防御RIO攻击的关键。
详解Android中IntentService的使⽤⽅法为什么我们需要IntentService ?Android中的IntentService是继承⾃Service类的,在我们讨论IntentService之前,我们先想⼀下Service的特点: Service的回调⽅法(onCreate、onStartCommand、onBind、onDestroy)都是运⾏在主线程中的。
当我们通过startService启动Service之后,我们就需要在Service的onStartCommand⽅法中写代码完成⼯作,但是onStartCommand是运⾏在主线程中的,如果我们需要在此处完成⼀些⽹络请求或IO等耗时操作,这样就会阻塞主线程UI⽆响应,从⽽出现ANR现象。
为了解决这种问题,最好的办法就是在onStartCommand中创建⼀个新的线程,并把耗时代码放到这个新线程中执⾏。
可以参考之前的⽂章,这篇⽂章在onStartCommand中开启了新的线程作为⼯作线程去执⾏⽹络请求,所以这样不会阻塞主线程。
由此看来,创建⼀个带有⼯作线程的Service是⼀种很常见的需求(因为⼯作线程不会阻塞主线程),所以Android为了简化开发带有⼯作线程的Service,Android额外开发了⼀个类——–IntentService。
IntentService的特点IntentService具有以下特点:1. IntentService⾃带⼀个⼯作线程,当我们的Service需要做⼀些可能会阻塞主线程的⼯作的时候可以考虑使⽤IntentService。
2. 我们需要将要做的实际⼯作放⼊到IntentService的onHandleIntent回到⽅法中,当我们通过startService(intent)启动了IntentService之后,最终Android Framework会回调其onHandleIntent⽅法,并将intent传⼊该⽅法,这样我们就可以根据intent去做实际⼯作,并且onHandleIntent运⾏在IntentService所持有的⼯作线程中,⽽⾮主线程。
1.创建一个Notification的对象Notification(icon, tickerText, when)2.setLatestEventInfo(context,ContentTitle,contentText,contentIntent)第一个参数上下文环境第二个参数标题第三个参数内容第四个参数要跳转到的Activity其中的Intent 是PendingIntent类型的3.创建PendingIntent的对象通过PendingIntent contentIntent=PendingIntent.getActivity (context, requestCode, intent, flags);第一个参数上下文环境第二个参数请求号第三个参数intent对象要跳转到的Activity 第四个参数标志位4.激活Notification要通过NotificationManager对象调用notify(id, notification)方法,因此Notification的激活需要使用NotificationManager对象创建NotificationManager对象getSystemService(Context.NOTIFICATION_SERVICE); default属性为Notification设置震动、声音、灯光,取值是Notification中的常量以DEFAULT 开头,如果只想设置其中的两个可以用位或| 符号进行分割;flag属性设置Notification能否被清除,取值是Notification中的常量以FLAG 开头设置震动要加权限uses-permission android:name="android.permission.VIBRATE"一个应用对用着一个ActivityManager的对象,同时对应着一个NotificationManager的对象,管理时只通过ID区分,其他的不能标示NotificationManager可以让我们的Notification消失,cancle(ID),cancleall()。
Android中Notification通知⽤法详解Notification的作⽤通知(Notification)是Android系统中⽐较有特⾊的⼀个功能。
当某个应⽤程序希望向⽤户发出⼀些提⽰信息,⽽该应⽤程序⼜不在前台运⾏时,就可以⽤通知来实现⽤法⾸先我们需要⼀个NotificationManager来对通知进⾏管理,可以调⽤getSystemService()⽅法得到,⽅法接收⼀个字符串参数⽤于确定获取系统的哪个服务,这⾥我们传⼊NOTIFICATION_SERVICE。
NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);接下来⽤⼀个Builder构造器来创建Notification对象Intent intent=new Intent(this,NotificationActivity.class);//⽤intent表现出我们要启动Notification的意图PendingIntent pi=PendingIntent.getActivity(this,0,intent,0);//将Intent对象传⼊PendingIntent对象的getActivity⽅法中NotificationManager manager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);Notification notification=new NotificationCompat.Builder(this).setContentTitle("This is content title")//设置通知栏中的标题.setContentText("hello world!")//设置通知栏中的内容.setWhen(System.currentTimeMillis())//设置通知出现的时间,此时为事件响应后⽴马出现通知.setSmallIcon(R.mipmap.ic_launcher)//设置通知出现在⼿机顶部的⼩图标.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))//设置通知栏中的⼤图标.setContentIntent(pi)//将PendingIntent对象传⼊该⽅法中,表明点击通知后进⼊到NotificationActivity.class页⾯.setAutoCancel(true)//点击通知后,通知⾃动消失.setDefaults(NotificationCompat.DEFAULT_ALL)//默认选项,根据⼿机当前的环境来决定通知发出时播放的铃声,震动,以及闪光灯.setPriority(NotificationCompat.PRIORITY_MAX)//设置通知的权重.build();manager.notify(1,notification);//⽤于显⽰通知,第⼀个参数为id,每个通知的id都必须不同。
NotiFication 详解下面来谈谈notification,这个notification一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个快讯,这时手从上方滑动状态栏就可以展开并处理这个快讯。
发现这个功能特别好用,所以我就根据我的理解来谈谈。
摘自帮助文档 : notification类表示一个持久的通知,将提交给用户使用NotificationManager。
已添加的Notification.Builder,使其更容易构建通知。
notification是一种让你的应用程序在没有开启情况下或在后台运行警示用户。
它是看不见的程序组件(Broadcast Receiver,Service和不活跃的Activity)警示用户有需要注意的事件发生的最好途径。
先来区分以下状态栏和状态条的区别:1、状态条就是手机屏幕最上方的一个条形状的区域;在状态条有好多信息量:比如usb连接图标,手机信号图标,电池电量图标,时间图标等等;2、状态栏就是手从状态条滑下来的可以伸缩的view;在状态栏中一般有两类(使用FLAG_标记):(1)正在进行的程序;(2)是通知事件;大概来描述创建一个Notification传送的信息有:1、一个状态条图标;2、在拉伸的状态栏窗口中显示带有大标题,小标题,图标的信息,并且有处理该点击事件:比如调用该程序的入口类;3、闪光,LED,或者震动;快速创建一个Notification的步骤简单可以分为以下四步:第一步:通过getSystemService()方法得到NotificationManager对象;第二步:对Notification的一些属性进行设置比如:内容,图标,标题,相应notification的动作进行处理等等;第三步:通过NotificationManager对象的notify()方法来执行一个notification 的快讯;第四步:通过NotificationManager对象的cancel()方法来取消一个notificatioin 的快讯;下面对Notification类中的一些常量,字段,方法简单介绍一下:常量:DEFAULT_ALL 使用所有默认值,比如声音,震动,闪屏等等 DEFAULT_LIGHTS 使用默认闪光提示DEFAULT_SOUNDS 使用默认提示声音DEFAULT_VIBRATE 使用默认手机震动【说明】:加入手机震动,一定要在manifest.xml中加入权限:<uses-permissionandroid:name="android.permission.VIBRATE" />以上的效果常量可以叠加,即通过mNotifaction.defaults =DEFAULT_SOUND | DEFAULT_VIBRATE ;或mNotifaction.defaults |=DEFAULT_SOUND (最好在真机上测试,震动效果模拟器上没有)//设置flag位FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉FLAG_NO_CLEAR 该通知能被状态栏的清除按钮给清除掉FLAG_ONGOING_EVENT 通知放置在正在运行FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应常用字段:contentIntent 设置PendingIntent对象,点击时发送该Intentdefaults 添加默认效果flags 设置flag位,例如FLAG_NO_CLEAR 等icon 设置图标sound 设置声音tickerText 显示在状态栏中的文字when 发送此通知的时间戳Notification.build构造Notification方法介绍:void setLatestEventInfo(Context context ,CharSequencecontentTitle,CharSequence contentText,PendingIntent contentIntent)功能:显示在拉伸状态栏中的Notification属性,点击后将发送PendingIntent 对象参数: context 上下文环境contentTitle 状态栏中的大标题contentText 状态栏中的小标题contentIntent 点击后将发送PendingIntent对象说明:要是在Notification中加入图标,在状态栏和状态条中显示图标一定要用这个方法,否则报错!最后说一下NotificationManager类的常用方法:通过获取系统服务来获取该对象:NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE) ;NotificationManager常用方法介绍:public voidcancelAll() 移除所有通知 (只是针对当前Context下的Notification)public void cancel(intid) 移除标记为id的通知(只是针对当前Context下的所有Notification)public void notify(String tag ,int id, Notification notification) 将通知加入状态栏, 标签为tag,标记为idpublic void notify(int id, Notificationnotification) 将通知加入状态栏,,标记为id下面看一下demo的效果图:图(1)图(2)图(3)图(4)图(5)源码奉上:在NotificationApp工程里面:一、在.notification.daming包下面NotificationMainActivity.java中的代码:package .notification.daming;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.content.SharedPreferences;import android.media.RingtoneManager;import .Uri;import android.os.Bundle;import android.preference.PreferenceManager;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class NotificationMainActivity extends Activity implements OnClickListener {private Button setNotificationSoundBtn = null;private Button showNotificatioBtn = null;private Button cancelNotificationBtn = null;private Intent mIntent = null;private PendingIntent mPendingIntent = null;private Notification mNotification = null;private NotificationManager mNotificationManager = null;private static final int NOTIFICATION_STATE = 1;private static final int RINGTONE_PICKED = 2;private Uri notifiSounds = null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.main);mNotificationManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);setNotificationSoundBtn = (Button)findViewById(R.id.button0);setNotificationSoundBtn.setOnClickListener(this);showNotificatioBtn = (Button)findViewById(R.id.button1);showNotificatioBtn.setOnClickListener(this);cancelNotificationBtn = (Button)findViewById(R.id.button2);cancelNotificationBtn.setOnClickListener(this);mIntent = new Intent(this, ToNotificationActivity.class);mPendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);mNotification = new Notification();}public void onClick(View v) {// TODO Auto-generated method stubswitch(v.getId()){case R.id.button0:SharedPreferences sharedPreferences =PreferenceManager.getDefaultSharedPreferences(this);Intent intent = newIntent(RingtoneManager.ACTION_RINGTONE_PICKER);// Allow user to pick 'Default'intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));// Show only ringtonesintent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);// Don't show 'Silent'intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);String notifi_sound =sharedPreferences.getString("notification_sounds", null);if(notifi_sound != null){intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_UR I, Uri.parse(notifi_sound));}// Launch!startActivityForResult(intent, RINGTONE_PICKED);break;case R.id.button1:mNotification.icon = R.drawable.daming;mNotification.tickerText = "大明ZeroSon Notification";mNotification.sound = notifiSounds;mNotification.defaults = Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS;mNotification.flags = Notification.FLAG_INSISTENT ;mNotification.setLatestEventInfo(this, "大明Notification", "This is Daming`s Notification Test!", mPendingIntent);mNotificationManager.notify(NOTIFICATION_STATE, mNotification);break;case R.id.button2:mNotificationManager.cancel(NOTIFICATION_STATE);break;default:break;}}@Overrideprotected void onResume() {super.onResume();}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {SharedPreferences sharedPreferences =PreferenceManager.getDefaultSharedPreferences(this);SharedPreferences.Editor editor = sharedPreferences.edit();if (resultCode != RESULT_OK) {return;}switch (requestCode) {case RINGTONE_PICKED: {notifiSounds =data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);editor.putString("notification_sounds",notifiSounds.toString());mit();break;}default: break;}}}package .notification.daming;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.content.SharedPreferences;import android.media.RingtoneManager;import .Uri;import android.os.Bundle;import android.preference.PreferenceManager;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class NotificationMainActivity extends Activity implements OnClickListener {private Button setNotificationSoundBtn = null;private Button showNotificatioBtn = null;private Button cancelNotificationBtn = null;private Intent mIntent = null;private PendingIntent mPendingIntent = null;private Notification mNotification = null;private NotificationManager mNotificationManager = null;private static final int NOTIFICATION_STATE = 1;private static final int RINGTONE_PICKED = 2;private Uri notifiSounds = null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.main);mNotificationManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);setNotificationSoundBtn = (Button)findViewById(R.id.button0);setNotificationSoundBtn.setOnClickListener(this);showNotificatioBtn = (Button)findViewById(R.id.button1);showNotificatioBtn.setOnClickListener(this);cancelNotificationBtn = (Button)findViewById(R.id.button2);cancelNotificationBtn.setOnClickListener(this);mIntent = new Intent(this, ToNotificationActivity.class);mPendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);mNotification = new Notification();}public void onClick(View v) {// TODO Auto-generated method stubswitch(v.getId()){case R.id.button0:SharedPreferences sharedPreferences =PreferenceManager.getDefaultSharedPreferences(this);Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);// Allow user to pick 'Default'intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));// Show only ringtonesintent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);// Don't show 'Silent'intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false); String notifi_sound =sharedPreferences.getString("notification_sounds", null);if(notifi_sound != null){intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,Uri.parse(notifi_sound));}// Launch!startActivityForResult(intent, RINGTONE_PICKED);break;case R.id.button1:mNotification.icon = R.drawable.daming;mNotification.tickerText = "大明ZeroSon Notification";mNotification.sound = notifiSounds;mNotification.defaults = Notification.DEFAULT_VIBRATE |Notification.DEFAULT_LIGHTS;mNotification.flags = Notification.FLAG_INSISTENT ;mNotification.setLatestEventInfo(this, "大明Notification", "This is Daming`s Notification Test!", mPendingIntent);mNotificationManager.notify(NOTIFICATION_STATE, mNotification);break;case R.id.button2:mNotificationManager.cancel(NOTIFICATION_STATE);break;default:break;}}@Overrideprotected void onResume() {super.onResume();}protected void onActivityResult(int requestCode, int resultCode, Intent data) { SharedPreferences sharedPreferences =PreferenceManager.getDefaultSharedPreferences(this);SharedPreferences.Editor editor = sharedPreferences.edit();if (resultCode != RESULT_OK) {return;}switch (requestCode) {case RINGTONE_PICKED: {notifiSounds =data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);editor.putString("notification_sounds", notifiSounds.toString());mit();break;}default: break;}}}二、在.notification.daming包下面ToNotificationActivity.java中的代码:package .notification.daming;import android.app.Activity;import android.content.SharedPreferences;import android.media.Ringtone;import android.media.RingtoneManager;import .Uri;import android.os.Bundle;import android.preference.PreferenceManager;import android.widget.TextView;public class ToNotificationActivity extends Activity{private TextView textview = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.main1);SharedPreferences sharedPreferences =PreferenceManager.getDefaultSharedPreferences(this);textview = (TextView)findViewById(R.id.textview);String notificationsound =sharedPreferences.getString("notification_sounds", null);if(notificationsound == null){textview.setText("默认Notification声音");} else{Ringtone ringtone= RingtoneManager.getRingtone(ToNotificationActivity.this,Uri.parse(notificationsound));String title = ringtone.getTitle(ToNotificationActivity.this);textview.setText(title);}}}package .notification.daming;import android.app.Activity;import android.content.SharedPreferences;import android.media.Ringtone;import android.media.RingtoneManager;import .Uri;import android.os.Bundle;import android.preference.PreferenceManager;import android.widget.TextView;public class ToNotificationActivity extends Activity{private TextView textview = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(yout.main1);SharedPreferences sharedPreferences =PreferenceManager.getDefaultSharedPreferences(this);textview = (TextView)findViewById(R.id.textview);String notificationsound = sharedPreferences.getString("notification_sounds", null);if(notificationsound == null){textview.setText("默认Notification声音");} else{Ringtone ringtone = RingtoneManager.getRingtone(ToNotificationActivity.this, Uri.parse(notificationsound));String title = ringtone.getTitle(ToNotificationActivity.this);textview.setText(title);}}}三、在layout下main.xml布局文件的代码<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"android:gravity="center"android:layout_marginBottom="10dip"/><Buttonandroid:id="@+id/button0"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="设置Notification的sounds"android:layout_marginBottom="10dip"/><Buttonandroid:id="@+id/button1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="发送Notification"android:layout_marginBottom="10dip"/><Buttonandroid:id="@+id/button2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="取消Notification"android:layout_marginBottom="10dip"/></LinearLayout><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android" android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/hello"android:gravity="center"android:layout_marginBottom="10dip"/><Buttonandroid:id="@+id/button0"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="设置Notification的sounds"android:layout_marginBottom="10dip"/><Buttonandroid:id="@+id/button1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="发送Notification"android:layout_marginBottom="10dip"/><Buttonandroid:id="@+id/button2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="取消Notification"android:layout_marginBottom="10dip"/></LinearLayout>四、在layout下main1.xml布局文件的代码<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android" android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center"android:textSize="10pt"android:text="大明原创"android:layout_marginTop="10dip"android:layout_marginBottom="10dip"/><TextViewandroid:id="@+id/textview"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center"android:textSize="10pt"android:layout_marginTop="10dip"android:layout_marginBottom="10dip"/></LinearLayout><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android" android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center"android:textSize="10pt"android:text="大明原创"android:layout_marginTop="10dip"android:layout_marginBottom="10dip"/><TextViewandroid:id="@+id/textview"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center"android:textSize="10pt"android:layout_marginTop="10dip"android:layout_marginBottom="10dip"/></LinearLayout>五、manifest.xml中的代码:<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="/apk/res/android"package=".notification.daming"android:versionCode="1"android:versionName="1.0"><uses-sdk android:minSdkVersion="8" /><uses-permission android:name="android.permission.VIBRATE" /><application android:icon="@drawable/icon"android:label="@string/app_name"><activity android:name=".NotificationMainActivity"android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="UNCHER" /> </intent-filter></activity><activity android:name=".ToNotificationActivity"></activity></application></manifest>。