Service前台服务
让服务以notification的形式,显示在前台,不容易被杀死
只需在Service的inCreate里面,构建notification,不是用NotificationManager启动,而是用startForeground来启动即可
构造pengdingIntent使前台可以打开原来的activity
Service代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
|
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null ;
}
@Override
public void onCreate() {
super .onCreate();
Intent intent = new Intent( this ,MainActivity. class );
PendingIntent pendingIntent = PendingIntent.getActivity( this , 0 ,intent, 0 );
Notification notify = new Notification.Builder( this )
.setSmallIcon(R.mipmap.ic_launcher) // 设置状态栏中的小图片,尺寸一般建议在24×24,这个图片同样也是在下拉状态栏中所显示,如果在那里需要更换更大的图片,可以使用setLargeIcon(Bitmap
// icon)
.setTicker( "Service" ) // 设置在status
// bar上显示的提示文字
.setContentTitle( "Notification Title" ) // 设置在下拉status
// bar后Activity,本例子中的NotififyMessage的TextView中显示的标题
.setContentText( "This is Service" ) // TextView中显示的详细内容
.setContentIntent(pendingIntent) // 关联PendingIntent
.setNumber( 1 ) // 在TextView的右方显示的数字,可放大图片看,在最右侧。这个number同时也起到一个序列号的左右,如果多个触发多个通知(同一ID),可以指定显示哪一个。
.build();
startForeground( 1 , notify);
}
|
MainActivity:
1
2
3
4
5
6
7
|
button.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity. this ,MyService. class );
startService(intent);
}
});
|
Service前台服务相关推荐
- Android Foreground Service (前台服务)-by:nixs
一.如何保活后台服务 在Android Services (后台服务) 里面,我们了解了Android四大组件之一的Service,知道如何使用后台服务进行来完成一些特定的任务.但是后台服务在系统内存 ...
- Foreground Service前台服务
Foreground Service前台服务 介绍前台服务 模拟播放器前台服务发送通知 介绍前台服务 前台服务是那些被认为用户知道(用户所认可的)且在系统内存不足的时候不允许系统杀死的服务.前台服务必 ...
- android Foreground Service 前台服务/notification全局通知
android Foreground Service 前台服务/notification全局通知 前言 要素简介 前台服务(Foreground Service) 全局通知(notification) ...
- Android Service前台服务 StartForeground
前言 前面写了一个脚本 来执行自动打卡 效果还不错 又学习了一些android的体系知识点 其中就用到了前台服务 利用播放器的模式 来消除被杀的可能 提高Service的优先级 如何保活Service ...
- Android前台服务讲解二之自定义通知视图(RemoteViews)及数据UI更新
Notification支持文字内容显示.震动.三色灯.铃声等多种提示形式,在默认情况下,Notification仅显示消息标题.消息内容.送达时间这3项内容. 1.更新系统通知Notificatio ...
- Android开发之如何保证Service不被杀掉(前台服务)
序言 最近项目要实现这样一个效果:运行后,要有一个service始终保持在后台运行,不管用户作出什么操作,都要保证service不被kill.参考了现今各种定制版的系统和安全厂商牛虻软件,如何能保证自 ...
- 安卓服务service全解,生命周期,前台服务、后台服务,启动注销、绑定解绑,注册
全栈工程师开发手册 (作者:栾鹏) python教程全解 定义服务(服务的生命周期) 调用context.startService()时依次执行 ->onCreate()- >onStar ...
- android8.1启动前台服务,Android 8.0 + Service开启方式兼容处理
Android 8.0 + ,对后台服务进行了限制了.如果依然采用之前startService()方式. 会导致问题. 前后台服务的一些区别: 有人可能会问,后台服务我们可以自己创建 ONGOING ...
- 前台服务(在通知栏显示服务)
关键代码: public class MyService extends Service {@Nullable@Overridepublic IBinder onBind(Intent intent) ...
最新文章
- 这些神经网络,还可信吗?
- 【车道线检测】一种基于神经网络+结构约束的车道线检测方法
- showModalDialog sesission丢失
- 可变分区存储管理实验报告总结_操作系统第5次实验报告:内存管理
- 学习笔记:验证对称二叉树
- Mac 编译报错 symbol(s) not found for
- 程序员必不可少的firefox插件推荐
- 万万想不到!地球运行的真实轨迹原来是这样子!
- python中split函数_python strip()函数和Split函数的用法总结
- java cygwin,cygwin和java
- Linux学习笔记---Cortex-A7 常用汇编指令
- 内存管理, 对象的生命周期
- Android学习笔记---Android平台1.使用dom解析xml文件
- Kotlin入门(23)适配器的进阶表达
- 变身吧,无人机:多种姿势可以选择,你喜欢X还是O?
- 移动光猫获取管理员密码过程记录(吉比特TEWA-272G)
- Matlab 三角函数输入
- Mac与Linux SSH无密登陆(互信)
- 文件读写处理:古诗问题
- an error occurred while contacting the respository
热门文章
- 企业信息安全应对勒索软件的攻击难度提升
- echarts+bmap地图去掉底图可点功能
- 如何去掉ppt模板中自带的背景音乐(没有小喇叭)
- 前端页面渲染markDown文件
- 西行漫记(18):操作即是数据
- 波罗的海国家同意支持分布式账本技术开发
- 【Wordle】Day9:诶嘿,今天没忘
- Android 应用锁/程序锁(APPLock)的实现 http://shigongbo.blog.163.com/blog/static/976090201452572535715
- 单个页面多个按钮调用同一个弹窗,和滚屏展示效果,关闭后,5秒自动弹出滚屏展示!
- laravel mysql超时时间_连接到数据库时Laravel连接超时