Service Simple Instance

Source: Internet
Author: User

1. Inheriting the service class, overriding the Onbinder () method of the method service

2. Register the service in manifest, set the filtered URL (public static final String ACTION = "Com.lql.service.ServiceDemo";//Custom)

3. Start Service StartService (new Intent (servicedemo.acition));

<service android:name= "Com.example.androidtest.ServiceDemo" >
<intent-filter>
<action android:name= "Com.lql.service.ServiceDemo"/>
</intent-filter>
</service>

4. Summary: Service life cycle

First boot:

04-09 21:46:09.637:i/servicedemo (9280): onCreate
04-09 21:46:09.637:i/servicedemo (9280): Onstartcommand
04-09 21:46:09.637:i/servicedemo (9280): OnStart

Second boot:

04-09 21:46:49.369:i/servicedemo (9413): Onstartcommand
04-09 21:46:49.369:i/servicedemo (9413): OnStart

Servicedemo.java

public class Servicedemo extends Service {private static final String TAG = "Servicedemo";   public static final String ACTION = "Com.lql.service.ServiceDemo";  @Overridepublic ibinder onbind (Intent arg0) {//TODO auto-generated method Stubreturn null;} @Overridepublic void OnCreate () {//TODO auto-generated method stublog.i (TAG, "onCreate"); Super.oncreate ();} @Overridepublic void OnDestroy () {//TODO auto-generated method stublog.i (TAG, "OnDestroy"); Super.ondestroy ();} @Override @deprecatedpublic void OnStart (Intent Intent, int startid) {//TODO auto-generated method stublog.i (TAG, "OnStar T "); Super.onstart (Intent, Startid);} @Overridepublic int Onstartcommand (Intent Intent, int flags, int startid) {//TODO auto-generated method stublog.i (TAG, "O Nstartcommand "); return Super.onstartcommand (Intent, flags, Startid);}     }

Startservice.java

public class StartService extends Activity {  Button start_service; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_start_service); Start_ Service = (Button) Findviewbyid (R.id.start_service); Start_service.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View arg0) {//TODO auto-generated method Stubstartservice (New Intent (servicedemo.action));}} );}}

Start_service_activity.xml

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". StartService "> <textview android:id=" @+id/textview1 "android:layout_width=" Wrap_content "an droid:layout_height= "Wrap_content" android:text= "@string/hello_world"/> <button android:id= "@+id/s Tart_service "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:layout _below= "@+id/textview1" android:layout_centerhorizontal= "true" android:layout_margintop= "110DP" Andro id:text= "Open Service"/></relativelayout>

Service Simple Instance

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.