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 前台服务的更多相关文章

  1. android 前台服务不显示通知

    原因可以在哪里写了执行完成后就自动结束的吧 导致前台服务没有出现 如我 @Override public int onStartCommand(Intent intent, int flags, in ...

  2. Android开发之如何保证Service不被杀掉(前台服务)

    序言 最近项目要实现这样一个效果:运行后,要有一个service始终保持在后台运行,不管用户作出什么操作,都要保证service不被kill.参考了现今各种定制版的系统和安全厂商牛虻软件,如何能保证自 ...

  3. Android Foreground Service (前台服务)

    一.如何保活后台服务 在Android Services (后台服务) 里面,我们了解了Android四大组件之一的Service,知道如何使用后台服务进行来完成一些特定的任务.但是后台服务在系统内存 ...

  4. Android通知栏前台服务

    一.前台服务的简单介绍 前台服务是那些被认为用户知道且在系统内存不足的时候不允许系统杀死的服务.前台服务必须给状态栏提供一个通知,它被放到正在运行(Ongoing)标题之下--这就意味着通知只有在这个 ...

  5. android: 使用前台服务

    9.5.1    使用前台服务 服务几乎都是在后台运行的,一直以来它都是默默地做着辛苦的工作.但是服务的系统 优先级还是比较低的,当系统出现内存不足的情况时,就有可能会回收掉正在后台运行的服 务.如果 ...

  6. 服务 IntentService 前台服务 定时后台服务

    Activity public class MainActivity extends ListActivity {     private int intentNumber = 0;     @Ove ...

  7. [Android] Service服务详解以及如何使service服务不被杀死

    排版上的细节有些不好看,主要是我用的MarkDown编辑器预览和这里的不一样,在那个上面的样式很舒服.这里要改的地方太多就不想改了,将就看吧.下次写的时候注意.还有看到错误给我提啊. 本文链接:htt ...

  8. 适配 通知 Notification 通知渠道 前台服务 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  9. 3D语音天气球(源码分享)——在Unity中使用Android语音服务

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 开篇废话: 这个项目准备分四部分介绍: 一:创建可旋转的"3D球":3 ...

随机推荐

  1. Advanced RESTClient插件安装 google浏览器

    在csdn下载插件,并解压. 然后在设置里找到扩展程序,打开并开启开发者模式,加载你刚刚解压的插件就可以. 我是用了蓝灯打开google的,成功后这样的

  2. debian及ubuntu挂载本地硬盘的ISO镜像文件

    1.定位Debian ISO镜像的位置,比如说sda3 fdisk -l 2.挂载: # mount -t auto /dev/sda3 /media/mnt 生成isodebian路径 /mnt# ...

  3. Hadoop生态圈-Flume的主流Sinks源配置

    Hadoop生态圈-Flume的主流Sinks源配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客只是配置的是Flume主流的Sinks,想要了解更详细的配置信息请参考官 ...

  4. nova-api源码分析(APP的创建)

    目录结构如下: 上面介绍了nova-api发布所用到的一些lib库,有了上面的基础知识,再来分析nova-api的发布流程,就比较轻松了.nova-api可以提供多种api服务:ec2, osapi_ ...

  5. spring框架学习(二)使用注解代替xml配置

    注解 1.使用注解配置spring 1)开启使用注解代理配置文件 <?xml version="1.0" encoding="UTF-8"?> &l ...

  6. javascript柯里化

    function curry(fn){ var slice = Array.prototype.slice; var arr = slice.call(arguments,1); return fun ...

  7. 在Emacs中启用Fcitx输入法

    安装fcitx输入法,在 ~/.xinitrc文件中添加如下内容 (我用startx启动图形环境,所以在~/.xinitrc中配置X会话) export LC_CTYPE="zh_CN.UT ...

  8. svn使用笔记

    一.checkout:第一次下载trunk里面的代码到本地 二.commit:提交一些修改* out of date : 本地版本号 < 服务器版本号* 如果过期,就update,可能会出现co ...

  9. waven 常用构建命令

    常用命令 mvn compile : 编译maven项目 mvn test : 运行项目测试用例 mvn package : 将项目打成jar包 mvn clean : 删除target目录下生成的文 ...

  10. 通过图片获取gps地理位置

    别人说通过一张照片就可以定位你的位置,看来个视频,仔细研究了一下自己的照片没想到真的可以做到,想想真的有点可怕. 如何通过一张照片去定位这张照片的经纬度下面我以我手机中的照片为例. 我们通过pytho ...