Android creates a front-end service

Source: Internet
Author: User

If you want to prevent the service from being killed by the system as much as possible, you must set it to run on the foreground.

The method for setting the frontend to run the service changes before and after 2.0.

Therefore, it must be differentiated based on different versions; or the reflection mechanism should be used completely, so that the corresponding method can be used as long as there is a corresponding method; otherwise, the method of other versions should be used.

The following is an example of how to set the front-end running of servcie. Refer to the service description in the API.

Http://developer.android.com/reference/android/app/Service.html #

Import Java. lang. reflect. invocationtargetexception; import Java. lang. reflect. method; import android. app. notification; import android. app. icationicationmanager; import android. app. pendingintent; import android. app. service; import android. content. context; import android. content. intent; import android. OS. ibinder; import android. OS. build. version; import android. util. log; public class foregroundservice extends servi Ce {Private Static final string tag = "foregroundservice"; private Boolean mreflectflg = false; Private Static final int icationication_id = 1; // If the ID is set to 0, cannot be set as front-end service Private Static final class <?> [] Msetforegroundsignature = new class [] {Boolean. Class}; Private Static final class <?> [] Mstartforegroundsignature = new class [] {Int. Class, notification. Class}; Private Static final class <?> [] Mstopforegroundsignature = new class [] {Boolean. class}; private icationicationmanager mnm; private method msetforeground; private method mstartforeground; private method mstopforeground; private object [] msetforegroundargs = new object [1]; private object [] mstartforegroundargs = new object [2]; private object [] mstopforegroundargs = new object [1]; @ override public void oncreate () {super. oncreate (); Log. D (TAG, "oncreate"); mnm = (icationicationmanager) getsystemservice (context. notification_service); try {mstartforeground = foregroundservice. class. getmethod ("startforeground", mstartforegroundsignature); mstopforeground = foregroundservice. class. getmethod ("stopforeground", mstopforegroundsignature);} catch (nosuchmethodexception e) {mstartforeground = mstopforeground = NULL;} Try {msetfore Ground = getclass (). getmethod ("setforeground", rule);} catch (nosuchmethodexception e) {Throw new illegalstateexception ("OS doesn't have service. startforeground or service. setforeground! ");} Notification. builder = new notification. builder (this); pendingintent contentintent = pendingintent. getactivity (this, 0, new intent (this, mainactivity. class), 0); builder. setcontentintent (contentintent); builder. setsmallicon (R. drawable. icon); builder. setticker ("foreground service start"); builder. setcontenttitle ("foreground service"); builder. setcontenttext ("Make this service run in th E foreground. "); Notification = builder. build (); startforegroundcompat (icationication_id, notification) ;}@ override public int onstartcommand (intent, int flags, int startid) {super. onstartcommand (intent, flags, startid); log. D (TAG, "onstartcommand"); Return start_sticky ;}@ override public ibinder onbind (intent) {return NULL ;}@ override public void ondestroy () {super. Ondestroy (); log. D (TAG, "ondestroy"); stopforegroundcompat (icationication_id);} void invokemethod (method, object [] ARGs) {try {method. invoke (this, argS);} catch (invocationtargetexception e) {// shocould not happen. log. W ("apidemos", "unable to invoke method", e);} catch (illegalaccessexception e) {// shocould not happen. log. W ("apidemos", "unable to invoke method", e) ;}}/*** THIS IS Wrapper around und the new startforeground method, using the older * APIs if it is not available. */void startforegroundcompat (int id, Notification) {If (mreflectflg) {// if we have the new startforeground API, then use it. if (mstartforeground! = NULL) {mstartforegroundargs [0] = integer. valueof (ID); mstartforegroundargs [1] = notification; invokemethod (mstartforeground, mstartforegroundargs); return;} // fall back on the old API. msetforegroundargs [0] = Boolean. true; invokemethod (msetforeground, msetforegroundargs); mnm. notify (ID, notification);} else {/* can also use the following method. When the SDK is greater than or equal to 5, call the existing SDK method startforeground to set the foreground to run, * otherwise, the SDK Level 5 obtained by calling reflection (corresponding to Droid 2.0) the old method setforeground below sets the foreground to run */If (version. sdk_int> = 5) {startforeground (ID, notification);} else {// fall back on the old API. msetforegroundargs [0] = Boolean. true; invokemethod (msetforeground, msetforegroundargs); mnm. policy (ID, notification) ;}}/ *** this is a wrapper around the new stopforeground method, using the older * APIs if it is not available. */void stopforegroundcomp At (int id) {If (mreflectflg) {// if we have the new stopforeground API, then use it. If (mstopforeground! = NULL) {mstopforegroundargs [0] = Boolean. true; invokemethod (mstopforeground, mstopforegroundargs); return;} // fall back on the old API. note to cancel before changing the // foreground state, since we cocould be killed at that point. mnm. cancel (ID); msetforegroundargs [0] = Boolean. false; invokemethod (msetforeground, msetforegroundargs);} else {/* can also use the following method. When the SDK is greater than or equal to 5, call the existing SDK method stopforeground to stop running on the foreground, * If (Version. sdk_int> = 5) {stopforeground (true);} else {// fall back on the old API. note to cancel before changing the // foreground state, since we cocould be killed at that point. mnm. cancel (ID); msetforegroundargs [0] = Boolean. false; invokemethod (msetforeground, msetforegroundargs );}}}}

Result After running the front-end service

(1). The notification bar displays the following content:

(2). The drop-down notification bar displays the following content:

Related Article

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.