请问为什么现在很多人都玩手机现金久久捕鱼电玩城现金版?

8281人阅读
[Android UI](4)
本文将根据个人经验对Notification做个总结,以供参考!
什么是通知(Notification)
通知是一个可以在应用程序正常的用户界面之外显示给用户的消息。
通知发出时,它首先出现在状态栏的通知区域中,用户打开通知抽屉可查看通知详情。通知区域和通知抽屉都是用户可以随时查看的系统控制区域。
作为安卓用户界面的重要组成部分,通知有自己的设计指南。在Android 5.0(API level 21)中引入的
的变化是特别重要的,更多信息请阅读 。
如何创建通知
随着Android系统不断升级,Notification的创建方式也随之变化,主要变化如下:
Android 3.0之前
Android 3.0 (API level 11)之前,使用new Notification()方式创建通知:
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(
this, 0, new Intent(this, ResultActivity.class), 0);
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(this, title, content, contentIntent);
mNotifyMgr.notify(NOTIFICATIONS_ID, notification);
Android 3.0 (API level 11)及更高版本
Android 3.0开始弃用new Notification()方式,改用Notification.Builder()来创建通知:
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(
this, 0, new Intent(this, ResultActivity.class), 0);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(&My notification&)
.setContentText(&Hello World!&)
.setContentIntent(contentIntent)
mNotifyMgr.notify(NOTIFICATIONS_ID, notification);
这里需要注意: &build()& 是Androdi 4.1(API level 16)加入的,用以替代
&getNotification()&。API level 16开始弃用&getNotification()&
兼容Android 3.0之前的版本
为了兼容API level 11之前的版本,v4 Support Library中提供了
NotificationCompat.Builder()这个替代方法。它与Notification.Builder()类似,二者没有太大区别。
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(
this, 0, new Intent(this, ResultActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(&My notification&)
.setContentText(&Hello World!&)
.setContentIntent(contentIntent);
mNotifyMgr.notify(NOTIFICATIONS_ID, mBuilder.build());
注:除特别说明外,本文将根据 NotificationCompat.Builder() 展开解析,
Notification.Builder()类似。
通知基本用法
通知的必要属性
一个通知必须包含以下三项属性:
小图标,对应 setSmallIcon()通知标题,对应 setContentTitle()详细信息,对应 setContentText()
其他属性均为可选项,更多属性方法请参考。
尽管其他都是可选的,但一般都会为通知添加至少一个动作(Action),这个动作可以是跳转到Activity、启动一个Service或发送一个Broadcas等。 通过以下方式为通知添加动作:
使用PendingIntent通过大视图通知的 Action Button //仅支持Android 4.1 (API level 16)及更高版本,稍后会介绍
1、实例化一个NotificationCompat.Builder对象
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(&My notification&)
.setContentText(&Hello World!&);
NotificationCompat.Builder自动设置的默认值:
priority: PRIORITY_DEFAULTwhen: System.currentTimeMillis() audio stream: STREAM_DEFAULT
2、定义并设置一个通知动作(Action)
Intent resultIntent = new Intent(this, ResultActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(
this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
3、生成Notification对象
Notificatioin notification = mBuilder.build();
4、使用NotificationManager发送通知
int mNotificationId = 001;
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, notification);
更新通知很简单,只需再次发送相同ID的通知即可,如果之前的通知依然存在则会更新通知属性,如果之前通知不存在则重新创建。
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
int notifyID = 1;
NotificationCompat.Builder mNotifyBuilder =
new NotificationCompat.Builder(this)
.setContentTitle(&New Message&)
.setContentText(&You've received new messages.&)
.setSmallIcon(R.drawable.ic_notify_status);
int numMessages = 0;
mNotifyBuilder.setContentText(&new content text&)
.setNumber(++numMessages);
mNotifyMgr.notify(notifyID, mNotifyBuilder.build());
取消通知有如下4种方式:
点击通知栏的清除按钮,会清除所有可清除的通知设置了 setAutoCancel() 或 FLAG_AUTO_CANCEL的通知,点击该通知时会清除它通过 NotificationManager 调用 cancel() 方法清除指定ID的通知通过 NotificationManager 调用 cancelAll() 方法清除所有该应用之前发送的通知
大视图通知
通知有两种视图:普通视图和大视图。
默认情况下为普通视图,可通过NotificationCompat.Builder.setStyle()设置大视图。
注: 大视图(Big Views)由Android 4.1(API level 16)开始引入,且仅支持Android 4.1及更高版本。
构建大视图通知
以上图为例:
1、构建Action Button的PendingIntent
Intent dismissIntent = new Intent(this, PingService.class);
dismissIntent.setAction(CommonConstants.ACTION_DISMISS);
PendingIntent piDismiss = PendingIntent.getService(
this, 0, dismissIntent, 0);
Intent snoozeIntent = new Intent(this, PingService.class);
snoozeIntent.setAction(CommonConstants.ACTION_SNOOZE);
PendingIntent piSnooze =
PendingIntent.getService(this, 0, snoozeIntent, 0);
2、构建NotificatonCompat.Builder对象
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_notification)
.setContentTitle(getString(R.string.notification))
.setContentText(getString(R.string.ping))
.setDefaults(Notification.DEFAULT_ALL)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.addAction (R.drawable.ic_stat_dismiss,
getString(R.string.dismiss), piDismiss)
.addAction (R.drawable.ic_stat_snooze,
getString(R.string.snooze), piSnooze);
3、其他步骤与普通视图相同
进度条通知
明确进度的进度条
使用setProgress(max, progress, false)来更新进度。
max: 最大进度值
progress: 当前进度
false: 是否是不明确的进度条
模拟下载过程,示例如下:
int id = 1;
mNotifyManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle(&Picture Download&)
.setContentText(&Download in progress&)
.setSmallIcon(R.drawable.ic_notification);
new Thread(
new Runnable() {
public void run() {
for (incr = 0; incr &= 100; incr+=5) {
mBuilder.setProgress(100, incr, false);
mNotifyManager.notify(id, mBuilder.build());
Thread.sleep(5*1000);
} catch (InterruptedException e) {
Log.d(TAG, &sleep failure&);
mBuilder.setContentText(&Download complete&)
.setProgress(0,0,false);
mNotifyManager.notify(id, mBuilder.build());
).start();
上图,分别为下载过程中进度条通知 和 下载完成移除进度条后的通知。
不确定进度的进度条
使用setProgress(0, 0, true)来表示进度不明确的进度条
mBuilder.setProgress(0, 0, true); mNotifyManager.notify(id, mBuilder.build());
浮动通知(Heads-up Notifications)
Android 5.0(API level 21)开始,当屏幕未上锁且亮屏时,通知可以以小窗口形式显示。用户可以在不离开当前应用前提下操作该通知。
NotificationCompat.Builder mNotifyBuilder =
new NotificationCompat.Builder(this)
.setContentTitle(&New Message&)
.setContentText(&You've received new messages.&)
.setSmallIcon(R.drawable.ic_notify_status)
.setFullScreenIntent(pendingIntent, false);
以下两种情况会显示浮动通知:
setFullScreenIntent(),如上述示例。通知拥有高优先级且使用了铃声和振动
Android 5.0(API level 21)开始,通知可以显示在锁屏上。用户可以通过设置选择是否允许敏感的通知内容显示在安全的锁屏上。
你的应用可以通过setVisibility()控制通知的显示等级:
VISIBILITY_PRIVATE : 显示基本信息,如通知的图标,但隐藏通知的全部内容VISIBILITY_PUBLIC : 显示通知的全部内容VISIBILITY_SECRET : 不显示任何内容,包括图标
自定义通知
Android系统允许使用来自定义通知。
自定义普通视图通知高度限制为64dp,大视图通知高度限制为256dp。同时,建议自定义通知尽量简单,以提高兼容性。
自定义通知需要做如下操作:
1、创建自定义通知布局
2、使用RemoteViews定义通知组件,如图标、文字等
3、调用setContent()将RemoteViews对象绑定到NotificationCompat.Builder
4、同正常发送通知流程
注意: 避免为通知设置背景,因为兼容性原因,有些文字可能看不清。
定义通知文本样式
通知的背景颜色在不同的设备和版本中有所不同,Android2.3开始,系统定义了一套标准通知文本样式,建议自定义通知使用标准样式,这样有助于通知文本可见。
通知文本样式:
Android 5.0之前可用:
android:style/TextAppearance.StatusBar.EventContent.Title
android:style/TextAppearance.StatusBar.EventContent
Android 5.0及更高版本:
android:style/TextAppearance.Material.Notification.Title
android:style/TextAppearance.Material.Notification
更多通知的标准样式和布局,可参考源码frameworks/base/core/res/res/layout路径下的通知模版如:
5.0 及更高版本:
保留Activity返回栈
常规Activity
默认情况下,从通知启动一个Activity,按返回键会回到主屏幕。但某些时候有按返回键仍然留在当前应用的需求,这就要用到TaskStackBuilder了。
1、在manifest中定义Activity的关系
Android 4.0.3 及更早版本
android:name=&.ResultActivity&&
android:name=&android.support.PARENT_ACTIVITY&
android:value=&.MainActivity&/&
Android 4.1 及更高版本
android:name=&.ResultActivity&
android:parentActivityName=&.MainActivity&&
2、创建返回栈PendingIntent
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());
上述操作后,从通知启动ResultActivity,按返回键会回到MainActivity,而不是主屏幕。
特殊Activity
默认情况下,从通知启动的Activity会在近期任务列表里出现。如果不需要在近期任务里显示,则需要做以下操作:
1、在manifest中定义Activity
android:name=&.ResultActivity&
android:launchMode=&singleTask&
android:taskAffinity=&&
android:excludeFromRecents=&true&&
2、构建PendingIntent
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Intent notifyIntent = new Intent(this, ResultActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent notifyPendingIntent =
PendingIntent.getActivity(this, 0, notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(notifyPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());
上述操作后,从通知启动ResultActivity,此Activity不会出现在近期任务列表中。
通知常见属性和常量
通知的提醒方式
1、声音提醒
notification.defaults |= Notification.DEFAULT_SOUND;自定义声音
notification.sound = Uri.parse(&file:///sdcard0/notification.ogg&);
2、震动提醒
notification.defaults |= Notification.DEFAULT_VIBRATE;自定义振动
long[] vibrate = {100, 200, 300, 400}; //震动效果
// 表示在100、200、300、400这些时间点交替启动和关闭震动 notification.vibrate =
3、闪烁提醒
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; // 必须加上这个标志
常见的Flags
FLAG_AUTO_CANCEL
当通知被用户点击之后会自动被清除(cancel)FLAG_INSISTENT
在用户响应之前会一直重复提醒音FLAG_ONGOING_EVENT
表示正在运行的事件FLAG_NO_CLEAR
通知栏点击“清除”按钮时,该通知将不会被清除FLAG_FOREGROUND_SERVICE
表示当前服务是前台服务
更多Notification属性详见。
来一些基础知识:
publicBuilder
setTicker(CharSequence tickerText)
设置状态栏开始动画的文字
publicBuilder
setContentTitle(CharSequence title)
设置内容区的标题,必须设置
publicBuilder
setContentText(CharSequence text)
设置内容区的内容,必须设置
publicBuilder
setContentIntent(PendingIntent intent)
设置点击通知后操作(可以跳转Activity,打开Service,或者发送广播)
publicBuilder
setColor(@ColorIntint
这个可以设置smallIcon的背景色
publicBuilder
setSmallIcon(@DrawableResint
设置小图标,必须设置
publicBuilder
setLargeIcon(Bitmap b)
设置打开通知栏后的大图标
publicBuilder
setWhen(long when)
设置显示通知的时间,不设置默认获取系统时间,这个值会在Notification上面显示出来
publicBuilder
setAutoCancel(boolean autoCancel)
设置为true,点击该条通知会自动删除,false时只能通过滑动来删除
publicBuilder
setPriority(int pri)
设置优先级,级别高的排在前面
publicBuilder
setDefaults(int defaults)
设置上述铃声,振动,闪烁用|分隔,常量在Notification里
publicBuilder
setOngoing(boolean ongoing)
设置是否为一个正在进行中的通知,这一类型的通知将无法删除
通知的提醒方式
notification.defaults|=Notification.DEFAULT_SOUND;
自定义声音
notification.sound=Uri.parse(&file:///sdcard0/notification.ogg&);
notification.defaults|=Notification.DEFAULT_VIBRATE;
自定义振动
vibrate ={100,200,300,400};//震动效果,表示在100、200、300、400这些时间点交替启动和关闭震动
notification.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;//
必须加上这个标志
PendingIntent
PendingIntent pendingIntent=PendingIntent.getActivity(MainActivity.this,(int)SystemClock.uptimeMillis(),newIntent(MainActivity.this,OpenActivity.class),PendingIntent.FLAG_UPDATE_CURRENT);
flags有四种不同的值:
FLAG_CANCEL_CURRENT:如果构建的PendingIntent已经存在,则取消前一个,重新构建一个。
FLAG_NO_CREATE:如果前一个PendingIntent已经不存在了,将不再构建它。
FLAG_ONE_SHOT:表明这里构建的PendingIntent只能使用一次。
FLAG_UPDATE_CURRENT:如果构建的PendingIntent已经存在,那么系统将不会重复创建,只是把之前不同的传值替换掉。通常做法就是在构建PendingIntent的时候传入不一样的requestCode来更新PendingIntent
最简单的通知
将之前提到的那些基础点串起来,就可以发送一条一行文本的通知了
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
现在来进行实际操作
Android通知有两种,默认通知与自定义通知。默认通知简单调用系统接口就能实现,如下:
发送默认通知
默认通知效果
自定义通知就稍微麻烦一些,需要定义一个layout文件,使用RemoteViews加载它并设置一些点击事件,再设置到builder,如下:
自定义通知代码
自定义通知效果
这个通知很简单,就是两行文本加上一个按钮,按钮具有单独的点击事件,点击后跳转到AnotherActivity。
注意:smallIcon对于自定义通知和默认通知都是必须的,否则通知显示不出来。道理很简单,smallIcon需要在状态栏上显示,不设置怎么行。在5.0及以上,smallIcon必须符合Material Design风格,即白色内容,透明背景。不然系统会使用默认的图片替换。具体可参考。后面我会有一篇更详细的文章来介绍这个。contentIntent对于2.3及以下的系统是必须的,否则发送通知时会抛异常。道理也很简单,Android 2.3及以下系统不支持给自定义通知上的元素绑定单独的点击事件,因此必须设置整个通知的点击事件。
为什么要进行样式适配?
默认通知不存在样式适配的问题,因为默认通知的布局、颜色、背景什么的都是系统的,系统总会正确的显示默认通知。但自定义通知就不一样了,自定义通知的布局完全由我们自己掌控,我们可以为元素设置任何背景、颜色。那么,问题来了。Android通知栏的背景各种各样,不同的ROM有不同的背景,白色、黑色、透明等。不同的Android版本通知栏背景也不一样,一旦我们为自定义通知上的元素设置了特定背景或颜色,就肯定会带来兼容性问题(主要是文本啦)。这样的应用一大把,贴个图大家就明白了:
未适配的自定义通知
怎么适配?
适配的方式大概有两种,一种简单粗暴:为自定义通知设置固定的背景(上图中的360卫士就这么干的),比如黑色。那么内容自然就是白色或近似白色。这样,在所有的手机上都能正常显示,不会出现在黑色背景通知栏上显示良好,到了白色背景通知栏上就几乎啥也看不见。使用这种方案的应用太多了。我个人很不推崇这种方式,这样会使得自定义通知在将近一半的手机上显示得很突兀,和系统的通知栏不够沉浸,影响整体美观。另一种方案就稍微合理一些:通过读取系统的通知栏样式文件,获取到title和content的颜色,进而将这个颜色设置到自定义通知上。读取通知栏样式文件本身有兼容性问题,不同Android版本的样式文件有变,具体可参考这篇博客
,这种方式也不是在所有手机上生效,实际测试发现,还是有小部分机型没法读取或是读取到的是错误的。拿到title和content的颜色后,还可以通过算法(后面细说)判断这个颜色是近似白色还是近似黑色,进而能判断出通知栏的背景是近似黑色还是近似白色,这样就能根据不同的通知栏背景加载不同的自定义通知布局。进而做到良好的适配。
更好的适配
现在切入主题,谈谈如何来更好的适配自定义通知。有过锁屏开发经验的人应该知道,如果你的应用有读取系统通知栏的权限,那么每当应用程序发出一个通知,你的应用都会收到对应的notification对象,这个时候,我们一般会执行以下操作:
获取并展示app通知
调用addView之后,应用程序的通知就会显示在我们的应用里。显然,上面的代码并没有对apply返回的notificationItemLayout做任何其他操作,但确实这个View显示出来时就是样式良好的,可见,notificationItemLayout本身就是带有样式的,即便是默认通知。那么方案来了!我们先构造一个默认通知:
获取通知栏title的颜色
通知并不发送出去,只是用来获取通知栏title的颜色,如果你还想获取content的颜色,抱歉,不能通过查找android.R.id.text来获取,这个字段是访问不到的。可通过反射获取,更好的办法是先预先设置一个content,然后遍历viewGoup根据content内容找到对应的TextView再获取颜色。
拿到颜色后,可根据算法判断这个颜色是近似白色还是近似黑色,我们使用黑色作为基准色,使用方差来计算这个颜色是否近似黑色:
比较两个颜色是否近似
baseColor传入Color.BLACK,color传入刚刚获取到的title的颜色,根据我实测,阈值为180.0较为合理。上述方法返回true,即表示title的颜色近似黑色,也就是说通知栏背景近似白色。
额,经验丰富的同学应该已经洞察到第二段代码存在的兼容性问题了:根据android.R.id.title去找到title对应的TextView是不靠谱的,因为有些ROM厂商会把id改掉,导致找到的title为空。
同时还有另外一个问题:使用上述方法,Activity不能继承自AppCompatActivity(实测5.0以下机型可以,5.0及以上机型不行),大致的原因是默认通知布局文件中的ImageView(largeIcon和smallIcon)被替换成了AppCompatImageView,而在5.0及以上系统中,AppCompatImageView的setBackgroundResource(int)未被标记为RemotableViewMethod,导致apply时抛异常。
为了解决这两个问题,我们改进getNotificationColor方法:
改进后的方法
在getNotificationColorInternal中,设置一个默认的title文本,如果根据id找不到title,则遍历notificationRoot根据设置的title文本找到title:
兼容厂商改id
在getNotificationColorCompat中,我们先构造一个默认通知,获取到默认通知的布局文件id,并将布局加载到notificationRoot,此时,如果根据id找不到title,显然设置默认title的办法已经失效了。如何从notificationRoot中找到title是个问题。我的解决办法是:反正都已经拿到notificationRoot了,不如就遍历它,先找到其中的所有TextView,取字体最大的TextView作为title(这是合理的,因为默认通知中最多也就4个TextView,分别是title、content、info、when,title肯定是字体最大,最显眼的),并返回其颜色:
兼容AppCompatActivity
拿到了通知栏背景的颜色后,我们就可以加载不同样式的布局,达到适配的目的。代码如下:
Android 4.4黑色背景的通知栏
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:300020次
积分:3580
积分:3580
排名:第7807名
原创:54篇
转载:14篇
评论:32条1619人阅读
【Android】多媒体(8)
MainActivity:
import java.io.F
import android.app.A
import android.app.N
import android.app.NotificationM
import android.app.PendingI
import android.content.I
import android.graphics.C
import android.net.U
import android.os.B
import android.view.V
import android.view.View.OnClickL
import android.widget.B
public class MainActivity extends Activity implements OnClickListener {
private Button sendN
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendNotice = (Button) findViewById(R.id.send_notice);
sendNotice.setOnClickListener(this);
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 = soundU
//设置手机震动
//第一个,0表示手机静止的时长,第二个,1000表示手机震动的时长
//第三个,1000表示手机震动的时长,第四个,1000表示手机震动的时长
//此处表示手机先震动1秒,然后静止1秒,然后再震动1秒
long[] vibrates = {0, , 1000};
notification.vibrate =
//设置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);
activity_main:
&LinearLayout xmlns:android=&/apk/res/android&
xmlns:tools=&/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=&发出通知&
&/LinearLayout&
NotificationActivity:
import android.app.A
import android.app.NotificationM
import android.os.B
public class NotificationActivity extends Activity {
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:
&?xml version=&1.0& encoding=&utf-8&?&
&RelativeLayout xmlns:android=&/apk/res/android&
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=&这是通知点击后的界面&
&/RelativeLayout&
AndroidManifest:
&?xml version=&1.0& encoding=&utf-8&?&
&manifest xmlns:android=&/apk/res/android&
package=&com.example.notificationtest&
android:versionCode=&1&
android:versionName=&1.0& &
android:minSdkVersion=&14&
android:targetSdkVersion=&19& /&
&application
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& &
&intent-filter&
&action android:name=&android.intent.action.MAIN& /&
&category android:name=&android.intent.category.LAUNCHER& /&
&/intent-filter&
&/activity&
&activity android:name=&.NotificationActivity& &
&/activity&
&/application&
&/manifest&
最后附上源码:
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:354559次
积分:5663
积分:5663
排名:第3881名
原创:149篇
转载:92篇
评论:224条
文章:13篇
阅读:28905
文章:44篇
阅读:116900
(7)(2)(2)(9)(9)(6)(4)(9)(3)(30)(15)(21)(25)(5)(16)(20)(30)(26)(5)(2)(4)

我要回帖

更多关于 现金捕鱼电玩 的文章

 

随机推荐