Integrate Android service with runnable

Source: Internet
Author: User

There is no activity to start a service. Even if an activity is used to start a service, it is regarded as an independent event and the focus should be handled.

The service is inherited from Android. App. Service.

The service ecosystem begins with oncreate () (if any), and then enters the onstart Service (), which is inherited by default, onstart () is not required, but the public ibinder onbind (intent) method must be rewritten.

Package CN. iimob;

Import Android. App. activity;
Import Android. content. intent;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. widget. Button;

Public class demo extends activity {
Private button btnstartservice, btnstopservice;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Btnstartservice = (button) findviewbyid (R. Id. btnstartservice );
Btnstopservice = (button) findviewbyid (R. Id. btnstopservice );
Btnstartservice. setonclicklistener (New button. onclicklistener (){
@ Override
Public void onclick (view v ){
// Construct an intent object and specify the object to open as myservice
Intent I = new intent (Demo. This, myservice. Class );
// Set the method of the new task
I. setflags (intent. flag_activity_new_task );
// Start intent using startservice
Startservice (I );
}
});
Btnstopservice. setonclicklistener (New button. onclicklistener (){

@ Override
Public void onclick (view v ){
// Construct an intent object and specify the closed object as the myservice Service
Intent I = new intent (Demo. This, myservice. Class );

// Disable intent using stopservice
Stopservice (I );
}
});
}
}

Package CN. iimob;

Import Android. App. Service;
Import Android. content. intent;
Import Android. OS. Handler;
Import Android. OS. ibinder;
Import Android. util. log;

/**
*
* @ Project: servicedemo
* @ Program name: CN. iimob. myservice. Java
* @ Class Name: myservice
* @ Description: The Custom myservice class inherits the service class.
* @ Author: ZH
* @ Creation date: 2011-11-3 09:49:00 AM
* @ Modificationhistory
* Who when what
*-----------------------------------------------------
* Username 2011-11-3 todo
*/
Public class myservice extends Service {

/**
* Create a handler object and use it as a process to pass postdelayed.
*/
Private handler myhandler = new handler ();

/**
* To confirm System Service Running Conditions
*/
Private int intcounter = 0;

/**
* The member variable mytasks is a runnable object and is used as a timer.
*/
Private runnable mytasks = new runnable (){
/**
* Process running
*/
@ Override
Public void run (){
// Todo auto-generated method stub
// Increase the counter integer to identify the backend service running time
Intcounter ++;
// Output log information in logcat as a log object to monitor service running status
Log. I ("Run service", "counter:" + integer. tostring (intcounter ));
Myhandler. postdelayed (mytask, 1000 );
}
};

@ Override
Public ibinder onbind (intent ){
Return NULL;
}

@ Override
Public void onstart (intent, int startid ){
Myhandler. postdelayed (mytask, 1000 );
Super. onstart (intent, startid );
Log. I ("Start service", "onstart ");
}

@ Override
Public void oncreate (){
Super. oncreate ();
Log. I ("create service", "oncreate ");
}

@ Override
Public void ondestroy (){
// When the service ends, delete the mtasks running thread
Myhandler. removecallbacks (mytasks );
Super. ondestroy ();
Log. I ("Destroy service", "ondestroy ");
}


}

 

<? XML version = "1.0" encoding = "UTF-8"?>

<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
<Button Android: text = "Start Service" Android: Id = "@ + ID/btnstartservice" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </button>
<Button Android: text = "Terminate service" Android: Id = "@ + ID/btnstopservice" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </button>
</Linearlayout>

 

<? XML version = "1.0" encoding = "UTF-8"?>

<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "cn. iimob"
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">
<Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">
<Activity Android: Name = ". Demo"
Android: Label = "@ string/app_name">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>
<! -- Create a service and give the class name -->
<! -- Create Android: exported is true, indicating that this service can be accessed by other programs -->
<Service android: Name = ". myservice" Android: exported = "true" Android: Process = ": Remote"> </service>
</Application>
<Uses-SDK Android: minsdkversion = "8"/>

</Manifest>

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.