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. mysql、mybatis遇到问题集合

    1.错误描述 使用JDBC连接MySql时出现:The server time zone value '�й���׼ʱ��' is unrecognized or represents more th ...

  2. MySQL事务及隔离级别详解

    MySQL事务及隔离级别详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL的基本架构 MySQL的基本架构可以分为三块,即连接池,核心功能层,存储引擎层. 1> ...

  3. Java基础-变量的定义以及作用域详解

    Java基础-变量的定义以及作用域详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.字面量 常量(字面量)表示不能改变的数值(程序中直接出现的值).字面量有时也称为直接量,包 ...

  4. ngx_lua_API 指令详解(六)ngx.thread.spawn、ngx.thread.wait、ngx.thread.kill介绍

    摘要:通过lua-nginx-module中的ngx.thread同时执行多个任务. ngx_lua中访问多个第三方服务 ngx_lua中提供了ngx.socket API,可以方便的访问第三方网络服 ...

  5. .NET面试题系列(六)多线程

    1.多线程的三个特性:原子性.可见性.有序性 原子性:是指一个操作是不可中断的.即使是多个线程一起执行的时候,一个操作一旦开始,就不会被其他线程干扰. 比如,对于一个静态全局变量int i,两个线程同 ...

  6. Flex 数组问题!

    设计一个图形类,来对应一个图形! 这个类大概的代码是: public class ShapeModel extends ... { [bindable] private var _x:Number = ...

  7. Linux学习7-文件操作

    标准I/O库(stdio)及其头文件stdio.h 为底层I/O系统调用提供了一个通用的接口. 在标准I/O库中,与底层文件描述符对应的是流(stream),它被实现为指向结构FILE的指针. 在启动 ...

  8. object-c 数学计算公式

    1. 三角函数  double sin (double);正弦  double cos (double);余弦  double tan (double);正切  2 .反三角函数  double as ...

  9. 灵活、可高度自定义的——Progress进度圈、弹窗、加载进度、小菊花

    DDProgressHUD的介绍 提供了四种类型的展示: 显示无限旋转的加载图(比如小菊花,可以自定义),显示文字信息.网络刷新时经常用到. 显示加载进度的动画,也可以显示文字.网络下载时用的比较多, ...

  10. Bug Bounty Reference

    https://github.com/ngalongc/bug-bounty-reference/blob/master/README.md#remote-code-execution Bug Bou ...