Android Front Desk Service

Source: Internet
Author: User

The Android front desk service learns from

11952435#t3

The front desk service ramble

The service we learned before is running with the background, the natural relative priority will be lower, when the memory is low, it is easy to be killed. But who wants their service to be killed? It is natural to think of ways to improve the priority of our services, if the priority of service is to use---front desk service, which is the theme of our chapter.

Common front-desk services

A variety of music playback app in the front desk service is the most common, such as my favorite NetEase cloud music, when playing music, in the notification bar will, there is a similar notice of the view, which is actually a front service, is also service+remoteview+ A combination of notification. NetEase Cloud music through the front desk service can not only guarantee the operation of the service, but also in real-time explicit music playback information, and also very convenient for us to switch music.

Because there is no phone at hand, the picture comes from the network.

Realize front desk Service

Layout of the front desk 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" an Droid:textsize= "18sp"/> <textview android:id= "@+id/singernametv" android:layout_width= "Wrap_conten T "android:layout_height=" wrap_content "android:layout_below=" @id/singnametv "Android:layout_marginle ft= "8DP" Android:layout_margintop= "3DP" android:layout_torightof= "@id/posteriv" android:text= "Sing Name" android:textcolor= "#2b 2b2b "android:textsize=" 12sp "/> <imageview android:id=" @+id/previousiv "Android:layout_widt H= "20DP" android:layout_height= "20DP" android:layout_below= "@id/singernametv" Android:layout_marginle ft= "8DP" android:layout_margintop= "5DP" android:layout_torightof= "@id/posteriv" android:src= "@drawabl E/previous "/> <imageview android:id=" @+id/pauseiv "android:layout_width=" 20DP "Android:layo ut_height= "20DP" android:layout_below= "@id/singernametv" android:layout_marginleft= "8DP" Android:layo ut_margintop= "5DP" android:layout_torightof= "@id/previousiv" android:src= "@drawable/pause"/> <imag Eview android:id= "@+id/nextiv" android:layout_width= "20DP" android:layout_height= "20DP" Androi d:layout_below= "@id/singErnametv "android:layout_marginleft=" 8DP "android:layout_margintop=" 5DP "android:layout_torightof=" @i D/pauseiv "android:src=" @drawable/next "/></relativelayout>

Service

Class Musicservice:service () {Override fun Onbind (intent:intent.): IBinder {TODO ("not Implemented")//to CH Ange body of created functions use File | Settings |    File Templates. } Override Fun OnCreate () {super.oncreate () Val remoteviews = Remoteviews (This.packagename, R.layout.mus Ic_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 ()//Open front desk service 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.stud Yservice. Start "Start playing music". LogE () Else, "Unkow operation". LogE ()} return Super.onst Artcommand (Intent, Flags, Startid)} Override Fun OnDestroy () {Super.ondestroy ()} Private Fun Createi Ntent (action:string): pendingintent {val Intent = intent (this, musicservice::class.java) intent.action = a ction return Pendingintent.getservice (this, 0, intent, 0)}}

Configuring services in the manifest file

<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>

The effect is as follows

Android Front Desk Service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.