安卓多媒体应用-通知
介绍:通知(Notification)是安卓系统中比较有特色的一个功能,当某一个应用程序希望向用户发送一些提示信息,而该应用程序又不在前台运行的时,就可以借助通知来实现。发出一条通知后,手机最上方的状态栏中会显示一个通知的图标,下拉状态栏可以看到通知的详细内容。
1)通知的基本用法
创建通知步骤:1、需要一个NotificationManager对通知进行管理,调用Context的getSystemService()方法获取到。
2、getSystemService()方法接收一个字符串参数用于确定获取系统哪个服务,传入Context.NOTIFICATION_SERVICE即可。
3、实例:NotificationManager manager =(NotificatrionManager)getSystemService(Context.NOTIFICATION_SERVICE);
4、使用Builder构造器来创建一个空的Notification对象
Notification notification = new NotificationCompat.Builder(context).build();//Notification需要导入API,support-v4提供了一个Notification类
5、在最终的build()方法之前可以连缀任意多的设置方法来创建一个丰富的Notification对象如下所示:
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("This is content title")//指定标题内容
.setContentText("This is content text")//指定通知的正文内容
.setWhen(System.currentTimeMillis())//通知创建时间
.setSmallIcon(R.drawable.big_image)//设置通知的小图标
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.drawable.big_image))//设置通知大图标
.build();
6、最后调用NotificationManager的notify()方法就可以让通知显示出来
manager.notify(1, notification);//其中第一个参数是id(要保证每一个通知所制定的id都不同),第二个参数是Notification对象。
说了那么多,接下来进行实战训练一下:首先新建一个NotificationTest项目,修改activity_main.xml中的代码,
功能:添加一个Send Notice按钮,用来发出一条通知
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/sendNotice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送消息" />
</LinearLayout>
接着修改MainActivity中的代码,
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendNotice = (Button) findViewById(R.id.sendNotice);
sendNotice.setOnClickListener(this);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void onClick(View v) {
switch (v.getId()) {
case R.id.sendNotice:
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("This is content title")
.setContentText("This is content text")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.big_image)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.drawable.big_image))
.build();//显示一张大图片
manager.notify(1, notification);
break;
default:
break;
}
}
}
2)通知进阶
学完了通知的基本用法之后,我们发现发出的这条通知不可以点击!所以下一步我们来完善它!要想实现通知的点击效果,需要在代码中进行相应的设置,就要用到PendingIntent。
PendingIntent跟Intent很类似他们都可以用于启动活动、启动服务以及发送广播等。而PendingIntent倾向于在某个时机去执行某个动作 。所以可以将PendingIntent简单理解为延迟执行的Intent.
PendingIntent的用法:PendingIntent提供了几个静态方法用于获取PendingIntent的实例,可以根据需求来选择是使用getActivity()方法、getBroadcast()方法,还是getService()方法。要注意的是这几个方法所接受的参数都是一样的,第一个参数是Context,第二个参数一般用不到传入0,第三个参数是Intent对象,我们可以通过这个对象构建出PendingIntent的“意图”。第四个参数用于确定PendingIntent的行为,行为有FLAG_ONE_SHOT、FLAG_NO_CREATE、FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT这四种值可选,通常情况这个参数传入0.
NotificationCompat.Builder这个构造器还可以连缀一个setContentIntent()方法,接受的参数是一个PendingIntent对象。因此可以通过PendingIntent构建出一个延迟执行的“意图”,当用户点击这条通知时就会执行相应的逻辑。
说到这相信对PendingIntent有一定了解,首先在新建一个活动 NotificationActivity,布局起名为notification_layout.然后修改布局文件代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="24sp"
android:text="This is notification layout"/>
</RelativeLayout>
接着开始修改MainActivity的代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendNotice = (Button) findViewById(R.id.sendNotice);
sendNotice.setOnClickListener(this);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void onClick(View v) {
switch (v.getId()) {
case R.id.sendNotice:
Intent intent = new Intent(this,NotificationActivity.class);//创建视图
PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);//获得PendingIntent 对象
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("This is content title")
.setContentText("This is content text")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.big_image)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.drawable.big_image))
.setContentIntent(pi)//传入PendingIntent对象
// .setAutoCancel(true)//第一种写法:当点击了这个通知的时候,通知会自动取消
// .setSound(Uri.fromFile(new File("/system/media/audio/ringtones/luna.ogg")))//允许播放音频
.setVibrate(new long[] {0,1000,1000,1000})//手机震动一秒,然后禁止一秒
.setLights(Color.GREEN,1000,1000)//实现LED灯一绿色灯光一闪一闪的效果
// .setDefaults(NotificationCompat.DEFAULT_ALL)//使用通知默认效果
/* .setStyle(new NotificationCompat.BigTextStyle().bigText("大家好大家好大家好大家好大家好大家好大家好大家好大家好大" +
"家好大家好学习安卓,安卓入门与精通"))//显示所有的文字内容*/
// .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.big_image)))
.setPriority(NotificationCompat.PRIORITY_MAX)//通知重要程度 MAX表示最高的重要程度
.build();//显示一张大图片
manager.notify(1, notification);
break;
default:
break;
}
}
}
安卓多媒体应用-通知相关推荐
- 安卓接收消息通知——从后台发出到显示消息弹窗详解
系统:win10 64bit 使用工具及技术:IDEA rabbitmq3.7.15 otp_win64_21.0.1 spring-boot-starter-amqp-2.0.6.RELEAS ...
- 手把手教你开发安卓推送通知服务(使用阿里云 emas)
0 前言 0.1 痛点:安卓推送通知无统一标准 安卓是开源的手机操作系统,各大手机硬件厂商都会在它的基础上定制自己的操作系统. 在中国,用户因为各种原因无法使用 Google 官方服务框架,所以,在中 ...
- 安卓多媒体API——Vitamio框架
之前学习了MediaPlayer https://blog.csdn.net/nishigesb123/article/details/89479084 但MediaPlayer支持的媒体格式其实是有 ...
- 安卓 多条通知_安卓11第一版发布:原生滚动截屏、屏幕录像、抄国内ROM这么多...
自2008年第一部Android智能手机HTC G1发布,安卓手机系统已经走过十几个年头.虽然系统存在一些大家吐槽较多的问题,但安卓一直活跃在智能手机系统前沿,不断发展完善着. 昨天谷歌刚刚发布了全新 ...
- 推送通知_谷歌宣布为安卓带来声音通知功能 可监测周围声音如婴儿哭声推送通知...
谷歌日前宣布为安卓系统带来新的无障碍功能,该功能被称为声音通知并且该功能会集成到此前推出的实时转录.声音通知无障碍功能目前正在逐步向安卓用户推送,借助该应用程序可以将某些真实的语音和某些声音转录文字. ...
- 安卓手机高级通知管理 | 对某一应用的部分通知进行屏蔽 - 安卓9及以上
问题 想必我们都被手机应用繁杂的通知所困扰过,简单粗暴的解决方式便是关闭该应用的通知权限 ,但此方法仍有不足,就是一旦关闭该权限,则该应用的所有通知都会被屏蔽,包括一些有用的通知, 例如音乐软件的通知 ...
- 安卓清除所有通知后系统自动重启
自从手贱把我的主力机 OnePlus 8 Pro 从氢 OS 升级成 ColorOS 12,已经快半年了.现在也时常感叹:"唉-我大氢怎么就亡了啊!"对于 ColorOS,论坛上一 ...
- 安卓 多条通知_盘点安卓手机上那些让苹果手机用户羡慕不已的超实用功能
众所都知安卓和苹果的系统各有各的优势和劣势,安卓系统相对开放自由一点,对应的安全性和流畅度方面就稍差一点.而苹果的系统是封闭的,所以安全性和流畅度方面更好一筹,相对的开放和自由度就差了一点,安卓系统许 ...
- 安卓中Notification通知的详解
在消息通知时,我们经常用到两个组件Toast和Notification.特别是重要的和需要长时间显示的信息,用Notification就最 合适不过了.当有消息通知时,状态栏会显示通知的图标和文字,通 ...
最新文章
- 使用PHP导入Excel和导出数据为Excel文件
- 近世代数--环同态--环的扩张定理
- oracle比较好的链接记录
- CCS5.5环境下使用clock()函数测试程序段运行时间
- 2018-3-24Linux系统管理(13)程序包管理(3)yum前端配置及编译
- 【makefile系列】全平台编译脚本makefile
- 大数据平台应用开发的痛点有哪些
- kx3552驱动最佳连线图_意甲新赛季5大看点:C罗连线苏牙,皮尔洛执教初体验
- Instruments-Automation: 通过命令行执行测试用例
- 迁安职中计算机专业,迁安职业技术教育中心2021年招生简章
- Nginx与TCP协议的关系
- 微信小程序登录 php后台
- 计算机高效课堂建设,基于信息技术的小学音乐高效课堂的构建
- RainMeter — 使用 NotePad ++ 编辑皮肤文件
- 如何在dw中html字体变大,DW网页的字体大小怎么设置
- python课程结课感悟_python听课的感悟与建议
- 初谈“信息安全审计”
- C#中Winform操作百度地图
- 数据结构单向链表(C++)
- 《深入理解JAVA虚拟机》周志明 第三版 - 第四章 虚拟机性能监控、故障处理工具