1. 程式人生 > >Android 前臺服務

Android 前臺服務

body extc startid 來源 operation class iou little 信息

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 前臺服務