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 ...
随机推荐
- Linux掉电处理
在嵌入式设备中,掉电处理一直是一项比较麻烦的工作,在具有Linux系统的设备中,系统的种种数据的处理更是增加掉电处理的难度.现在做以下几点总结,再遇到类似问题可以做个参考. 1,系统启动的处理 在系统 ...
- C# 基于MySQL的数据层基类(MySQLHelper)
这里介绍下比较简单的方式,引用MySql.Data.dll然后添加一个MySqlHelper类来对MySql数据库进行访问和操作. 1.将MySql.Data.dll引用到你的项目中 下载地址:MyS ...
- webpack+express实现“热更新”和“热加载”(webpack3.6以前的做法)
“热更新”:对应的是 'webpack-dev-middleware' 中间件 “热加载”:对应的是 'webpack-hot-middleware' 中间件 为了使用这两个中间件,必须修改“webp ...
- 查找和替换img src
$("#imgId")[0].src; //获取 $("#imgId").attr('src',path); //修改
- Java访问权限控制
访问权限控制 java提供了访问权限修饰词,以供类库开发人员向客户端程序员指明哪些是可用的,哪些是不可用的.访问权限控制的等级,从最大权限到最小权限依次是:public.prote ...
- S折交叉验证(S-fold cross validation)
S折交叉验证(S-fold cross validation) 觉得有用的话,欢迎一起讨论相互学习~Follow Me 仅为个人观点,欢迎讨论 参考文献 https://blog.csdn.net/a ...
- Spark记录-Scala多线程
Scala多线程 多线程是同时执行多个线程的过程. 它允许您独立执行多个操作.可以通过使用多线程来实现多任务.线程是轻量级的子进程,占用较少的内存.多线程用于在Scala中开发并发应用程序. Scal ...
- bzoj千题计划261:bzoj3294: [Cqoi2011]放棋子
http://www.lydsy.com/JudgeOnline/problem.php?id=3294 如果一个颜色的棋子放在了第i行第j列,那这种颜色就会占据第i行第j列,其他颜色不能往这儿放 设 ...
- SpringMvc数据校验@Valid等注解的使用与工具类抽取
最近在重构老项目的代码,发现校验入参占用了很多代码,之前我对这一块的认识局限于使用StringUtils等工具来多个if块进行判断,代码是没什么问题,但是总写这些令人生烦,毕竟写代码也要讲究优雅的嘛, ...
- 各种奇妙的hack
Android Selector Hacks WebKit .selector:not(*:root) {} Chrome * Safari * Opera ≥ 14 Android * # Java ...