Android 前台服务
Android 前台服务
学习自
https://blog.csdn.net/guolin_blog/article/details/11952435#t3
前台服务漫谈
我们之前学习的Service都是运行与后台的,自然相对优先级会比较低一点,当内存不足的时候很容易被杀死。但是谁又希望自家的Service被杀死呢。那自然是想办法将自家的服务的优先级提高了,如果提高Service的优先级那当然是用---前台服务,也就是我们本章的主题。
常见的前台服务
各种音乐播放APP中的前台服务是最常见的了,比如我最喜欢的网易云音乐,在播放音乐的时候,在通知栏都会,存在一个类似通知的视图,这其实就是一个前台Service,也是Service+RemoteView+Notification的结合体。网易云音乐通过前台服务不仅可以保证Service的运行,还实时地显式了音乐的播放信息,并且也非常方便我们来切换音乐。
因为手头没有手机,图片来源于网络。
实现前台服务
前台服务的布局
<?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="100dp">
<ImageView
android:id="@+id/posterIV"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/singNameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/posterIV"
android:text="Sing Name"
android:textColor="#2b2b2b"
android:textSize="18sp" />
<TextView
android:id="@+id/singerNameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/singNameTV"
android:layout_marginLeft="8dp"
android:layout_marginTop="3dp"
android:layout_toRightOf="@id/posterIV"
android:text="Sing Name"
android:textColor="#2b2b2b"
android:textSize="12sp" />
<ImageView
android:id="@+id/previousIV"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_below="@id/singerNameTV"
android:layout_marginLeft="8dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/posterIV"
android:src="@drawable/previous" />
<ImageView
android:id="@+id/pauseIV"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_below="@id/singerNameTV"
android:layout_marginLeft="8dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/previousIV"
android:src="@drawable/pause" />
<ImageView
android:id="@+id/nextIV"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_below="@id/singerNameTV"
android:layout_marginLeft="8dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/pauseIV"
android:src="@drawable/next" />
</RelativeLayout>
Service
class MusicService : Service() {
override fun onBind(intent: Intent?): IBinder {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onCreate() {
super.onCreate()
val remoteViews = RemoteViews(this.packageName, R.layout.music_remote)
remoteViews.setOnClickPendingIntent(R.id.previousIV, createIntent("top.littledavid.studyservice.PREVIOUS"))
remoteViews.setOnClickPendingIntent(R.id.pauseIV, createIntent("top.littledavid.studyservice.PAUSE"))
remoteViews.setOnClickPendingIntent(R.id.nextIV, createIntent("top.littledavid.studyservice.NEXT"))
val notification = Notification.Builder(this).apply {
setSmallIcon(R.mipmap.ic_launcher)
setCustomContentView(remoteViews)
}.build()
//开启前台服务
startForeground(1, notification)
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
when (intent!!.action) {
"top.littledavid.studyservice.PREVIOUS" -> "Previous".logE()
"top.littledavid.studyservice.PAUSE" -> "PAUSE".logE()
"top.littledavid.studyservice.NEXT" -> "NEXT".logE()
"top.littledavid.studyservice.START" -> "Start playing music".logE()
else -> "UNKOW Operation".logE()
}
return super.onStartCommand(intent, flags, startId)
}
override fun onDestroy() {
super.onDestroy()
}
private fun createIntent(action: String): PendingIntent {
val intent = Intent(this, MusicService::class.java)
intent.action = action
return PendingIntent.getService(this, 0, intent, 0)
}
}
Manifest文件中配置服务
<service android:name=".MusicService">
<intent-filter>
<action android:name="top.littledavid.studyservice.PREVIOUS" />
<action android:name="top.littledavid.studyservice.PAUSE" />
<action android:name="top.littledavid.studyservice.NEXT" />
<action android:name="top.littledavid.studyservice.START" />
</intent-filter>
</service>
效果如下
Android 前台服务的更多相关文章
- android 前台服务不显示通知
原因可以在哪里写了执行完成后就自动结束的吧 导致前台服务没有出现 如我 @Override public int onStartCommand(Intent intent, int flags, in ...
- Android开发之如何保证Service不被杀掉(前台服务)
序言 最近项目要实现这样一个效果:运行后,要有一个service始终保持在后台运行,不管用户作出什么操作,都要保证service不被kill.参考了现今各种定制版的系统和安全厂商牛虻软件,如何能保证自 ...
- Android Foreground Service (前台服务)
一.如何保活后台服务 在Android Services (后台服务) 里面,我们了解了Android四大组件之一的Service,知道如何使用后台服务进行来完成一些特定的任务.但是后台服务在系统内存 ...
- Android通知栏前台服务
一.前台服务的简单介绍 前台服务是那些被认为用户知道且在系统内存不足的时候不允许系统杀死的服务.前台服务必须给状态栏提供一个通知,它被放到正在运行(Ongoing)标题之下--这就意味着通知只有在这个 ...
- android: 使用前台服务
9.5.1 使用前台服务 服务几乎都是在后台运行的,一直以来它都是默默地做着辛苦的工作.但是服务的系统 优先级还是比较低的,当系统出现内存不足的情况时,就有可能会回收掉正在后台运行的服 务.如果 ...
- 服务 IntentService 前台服务 定时后台服务
Activity public class MainActivity extends ListActivity { private int intentNumber = 0; @Ove ...
- [Android] Service服务详解以及如何使service服务不被杀死
排版上的细节有些不好看,主要是我用的MarkDown编辑器预览和这里的不一样,在那个上面的样式很舒服.这里要改的地方太多就不想改了,将就看吧.下次写的时候注意.还有看到错误给我提啊. 本文链接:htt ...
- 适配 通知 Notification 通知渠道 前台服务 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 3D语音天气球(源码分享)——在Unity中使用Android语音服务
转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 开篇废话: 这个项目准备分四部分介绍: 一:创建可旋转的"3D球":3 ...
随机推荐
- python实现windows Service服务程序
python实现windows Service服务程序 win32serviceutil.ServiceFramework是封装得很好的Windows服务框架,本文通过继承它来实现. 通过SvcDoR ...
- vim基本技巧
一.无插件vim使用 1.查看修改代码 1)光标移动 h j k l 前下上后 w b 词首.词尾 ^ $ 句首.句尾 2)编辑 x d r y p a i o . ...
- JAVA-Servlet高级应用
会话只是指一段指定的时间间隔. 会话跟踪是维护用户状态(数据)的一种方式.它也被称为servlet中的会话管理. Http协议是一个无状态的,所以我们需要使用会话跟踪技术来维护用户状态. 每次用户请求 ...
- 项目引入非配置的文件,打成war包后测试报错的可能原因
写在前边 这阵子有点忙,开发一个微服务项目中读取配置文件的时候在本地测试是可以的,但是一到测试环境就报错,经查看发现是因为发布的时候是用的war包,使用java -jar xxx.war启动的,所以用 ...
- MySql数据库表设计规范
建表规约 索引规约 SQL 语句 其他实战建议 选用utf8编码 建议使用InnoDB存储引擎 建议每张表都设置一个主键 建议字段定义为NOT NULL 唯一值字段要指定唯一性约束 ALTER TAB ...
- [整理]Error: [ngRepeat:dupes]的解决方法
sdfsadf <div class="pageNum middle PT10"> <a href="javascript:void(0);" ...
- ftp 服务
ftp 上传下载 yum install ftp -y ftp:192.168.1.1 上传 put file1 下载 get file2 直接方式 ftp get test.tar.gz 文件 ft ...
- 在maven 2工程中加入iTextAsian支持(maven添加自定义jar包到本地仓库)
最近需要在工程中加入JasperReports,其中要用到把报表导出为pdf文件的功能.JasperReports内部使用iText来输出pdf文档,而iText对中文是放在单独的包iTextAsia ...
- ubuntu16.04 caffe(GPU模式)安装
历时5天终于完成了,配置中出现了各种各样的Error,这里记录一下,希望能为正在安装的人提供一点帮助. 配置中主要参考博客:http://blog.csdn.net/yhaolpz/article/d ...
- VAE(Variational Autoencoder)的原理
Kingma, Diederik P., and Max Welling. "Auto-encoding variational bayes." arXiv preprint ar ...