Package com.android.service;
Import Android.app.IntentService;
Import android.content.Intent;
public class Hellointentservice extends intentservice{
Public Hellointentservice () {
Super ("Hellointentservice");
TODO auto-generated Constructor stub
}
@Override
protected void Onhandleintent (Intent Intent) {
TODO auto-generated Method Stub
System.out.println ("Rest 8 seconds");
try{
Thread.Sleep (8000);
}catch (Interruptedexception e) {
E.printstacktrace ();
}
}
public void Ondestory () {
System.out.println ("will be called automatically after executing onhandleintent!") ");
Super.ondestroy ();
}
}
Package com.android.service;
Import Android.app.Service;
Import android.content.Intent;
Import Android.os.IBinder;
public class HelloService extends service{
@Override
Public IBinder Onbind (Intent Intent) {
TODO auto-generated Method Stub
return null;
}
public void OnCreate () {
Super.oncreate ();
}
public void Ondestory () {
Super.ondestroy ();
}
public void OnStart (Intent intent,int startid) {
TODO auto-generated Method Stub
System.out.println ("Start service, hibernate 10 seconds");
try{
Thread.Sleep (8000);
}catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
Package com.android.service;
Import Android.os.Bundle;
Import android.app.Activity;
Import android.content.Intent;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
public class Mainactivity extends Activity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Button button= (button) Findviewbyid (R.id.button1);
Button.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Intent intent=new Intent (
Mainactivity.this,hellointentservice.class);
StartService (Intent);
}
});
Button button2= (button) Findviewbyid (R.id.button2);
Button.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Intent intent=new Intent (
Mainactivity.this,hellointentservice.class);
StopService (Intent);
}
});
Button button3= (button) Findviewbyid (R.id.button3);
Button.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Intent intent=new Intent (
Mainactivity.this,helloservice.class);
StartService (Intent);
}
});
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}
}
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical"
Tools:context= ". Mainactivity ">
<button
Android:id= "@+id/button1"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Start Intentservice"
/>
<button
Android:id= "@+id/button2"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Stop Intentservice"
/>
<button
Android:id= "@+id/button3"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Start Service"
/>
</LinearLayout>
Example of an Android service