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. BSGS 算法

    求解 A^x ≡ B mod C  C是质数 的最小非负整数解 证明:A^x ≡ A^(x%φ(C)) mod C A^(x%φ(C))  ≡ A^(x-k*φ(C)) ≡ (A^x)/ A^(k*φ ...

  2. 织梦 dedecms 首页调用公司简介的内容

    首页调用公司简介的代码: {dede:sql sql='Select content,substring(content,1,300) as content from dede_arctype whe ...

  3. [转载]CSS Tools: Reset CSS

    http://meyerweb.com/eric/tools/css/reset/ The goal of a reset stylesheet is to reduce browser incons ...

  4. phpStorm 8.0.3 设置

    phpstorm 8 license key Learn Programming===== LICENSE BEGIN =====63758-1204201000000Ryqh0NCC73lpRm!X ...

  5. 20155225 2016-2017-2 《Java程序设计》第八周学习总结

    20155225 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 通用API 日志API 国际化基础 规则表达式 JDK8增强功能 教材学习中的问题和解决过程 ...

  6. <header><footer>引用

    示例:http://www.w3school.com.cn/html/html_layout.asp

  7. Javascript - Vue - 请求

    本地增删查的一个例子 <div id="box">    <div class="panel panel-primary">       ...

  8. 基于theano的多层感知机的实现

    1.引言 一个多层感知机(Multi-Layer Perceptron,MLP)可以看做是,在逻辑回归分类器的中间加了非线性转换的隐层,这种转换把数据映射到一个线性可分的空间.一个单隐层的MLP就可以 ...

  9. python3之模板pycurl探测web服务质量

    1.pycurl简介 pycURL是libcurl多协议文件传输库的python接口,与urllib模块类似,PycURL可用于从python程序中获取由URL标识的对象,功能很强大,libcurl速 ...

  10. 如何提升mysql replication的性能&amp;多线程传输二进制日志

    1,最好使用内网或者专线链路传输binlog数据 (千兆网卡.还不够的话,bounding 技术,扩展带宽) 在my.cnf中强制使用内网ip传输数据bind-address=ip2,将二进制保存在独 ...