码迷,mamicode.com
首页 > 移动开发 > 详细

Android 前台服务

时间:2018-07-27 15:34:17      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:this   lag   rap   学习   post   wrap   launcher   reg   match   

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 前台服务

标签:this   lag   rap   学习   post   wrap   launcher   reg   match   

原文地址:https://www.cnblogs.com/slyfox/p/9377650.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!