Android 前台服務

來源:互聯網
上載者:User

標籤:this   lag   rap   學習   post   wrap   launcher   reg   match   

Android 前台服務學習自

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.