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