Android (service)

Source: Internet
Author: User
There are several important components in Android, one of which is service. This is a component without a UI and can be used as a backend service. Of course, you can use intent to start it. It can also be bound to a host object (caller, usually activity) for use,
Note:
1. The service in Android is in the same thread as the caller, so it is necessary to open a new thread in the service for time-consuming operations.
2. In Android services, we mainly implement functions such as oncreate, onstart, ondestroy, onbind, and onunbind to implement the functions we need.

Simple call:
Simple calls can be called using context. startservice In the caller object. Intent is used as the parameter. Of course, intent search matches the target in the same way as in intent usage.
Let's look at a routine:

1. Declare the service subclass.

Public class testservice extends Service {

@ Override
Public void oncreate (){
// Todo auto-generated method stub
Super. oncreate ();
Toast. maketext (this, "service created! ", Toast. length_short). Show ();
}

@ Override
Public void ondestroy (){
// Todo auto-generated method stub
Super. ondestroy ();
Toast. maketext (this, "Service destroyed! ", Toast. length_long). Show ();
}

@ Override
Public void onstart (intent, int startid ){
// Todo auto-generated method stub
Super. onstart (intent, startid );
Toast. maketext (this, "service started...", Toast. length_long). Show ();
}

@ Override
Public Boolean onunbind (intent ){
// Todo auto-generated method stub
Super. onunbind (intent );
Toast. maketext (this, "service unbind! ", Toast. length_long). Show ();
Return true;
}

@ Override
Public ibinder onbind (intent ){
// Todo auto-generated method stub
Toast. maketext (this, "service bound! ", Toast. length_long). Show ();
Return NULL;
}

}

Configure this service in manifest. xml.
....
<Service android: Label = "@ string/Hello" Android: Name = "testservice">
<Intent-filter>
<Action Android: Name = "start_service"> </Action>
<Category Android: Name = "android. Intent. Category. Default"> </Category>
</Intent-filter>
</Service>
....
Create caller class:

Mport Android. App. activity;
Import Android. content. intent;
Import android.net. Uri;
Import Android. OS. Bundle;
Import Android. View. Menu;
Import Android. View. menuitem;
Import Android. widget. textview;

Public class helloactivity extends activity {
 
@ Override
Public Boolean oncreateoptionsmenu (menu ){
// Todo auto-generated method stub
Super. oncreateoptionsmenu (menu );
Menu. Add (0, menu. First + 1, 1, R. String. menu_open );
Menu. Add (0, menu. First + 2, 2, "startservice ");
Menu. Add (0, menu. First + 3, 3, "stopservice ");
Menu. Add (0, menu. First + 4, 4, R. String. menu_close );
Return true;
}

@ Override
Public Boolean onoptionsitemselected (menuitem item ){
// Todo auto-generated method stub
Super. onoptionsitemselected (item );
Switch (item. getitemid ())
{
Case menu. First + 1:
{
This. settitle ("Switch activity ");
Intent I = new intent ();
I. setaction ("test_action ");
If (tools. isintentavailable (this, I ))
This. startactivity (I );
Else
This. settitle ("the intent is unavailable !!! ");
Break;
}
Case menu. First + 2:
{
This. settitle ("Start Service ");
// Intent I = new intent (this, testservice. Class );
Intent I = new intent ();
I. setaction ("start_service ");
This. startservice (I );
Break;
}
Case menu. First + 3:
{
This. settitle ("Stop Service ");
Intent I = new intent (this, testservice. Class );
This. stopservice (I );
Break;
}
Case menu. First + 4:
{
This. settitle ("Close text! ");
Break;
}
}
Return true;
}

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
This. setcontentview (R. layout. Main );
}
}

Note: When operating the service, I use two intent methods, one is to display and directly write testservice. class, this method can only operate services in the current application, one is implicit, according to manifest. XML to operate services in different processes.

Binding method:
1. Declare a service interface, which provides the function to the caller.

Package test. phello;

Public interface itestservice {
Public void showname ();

}

2. Define the service, implement the onbind function, and return an implementation object of itestservice.

Package test. phello;

Import Android. App. Service;
Import Android. content. context;
Import Android. content. intent;
Import Android. OS. Binder;
Import Android. OS. ibinder;
Import Android. widget. Toast;

Public class testservice extends Service {
Public class test extends binder implements itestservice
{
Private context mcontext = NULL;
Public void showname (){
Toast. maketext (mcontext, "myname is testservice", Toast. length_long). Show ();
}
Public void setcontext (context c) {mcontext = C ;};
}
Test mtestimplement = new test ();
@ Override
Public void oncreate (){
// Todo auto-generated method stub
Super. oncreate ();
Toast. maketext (this, "service created! ", Toast. length_short). Show ();
}

@ Override
Public void ondestroy (){
// Todo auto-generated method stub
Super. ondestroy ();
Toast. maketext (this, "Service destroyed! ", Toast. length_long). Show ();
}

@ Override
Public void onstart (intent, int startid ){
// Todo auto-generated method stub
Super. onstart (intent, startid );
Toast. maketext (this, "service started...", Toast. length_long). Show ();
}

@ Override
Public Boolean onunbind (intent ){
// Todo auto-generated method stub
Super. onunbind (intent );
Toast. maketext (this, "service unbind! ", Toast. length_long). Show ();
Return true;
}

@ Override
Public ibinder onbind (intent ){
// Todo auto-generated method stub
Mtestimplement. setcontext (this );
Toast. maketext (this, "service bound! ", Toast. length_long). Show ();
Return mtestimplement;
}

}

3. Bind the caller to obtain reference from the itestservice interface.

Package test. phello;

Import Android. App. activity;
Import Android. content. componentname;
Import Android. content. context;
Import Android. content. intent;
Import Android. content. serviceconnection;
Import android.net. Uri;
Import Android. OS. Bundle;
Import Android. OS. ibinder;
Import Android. View. Menu;
Import Android. View. menuitem;
Import Android. widget. textview;

Public class helloactivity extends activity {
 
Itestservice mservice = NULL;
Serviceconnection sconnection = new serviceconnection ()
{
Public void onserviceconnected (componentname name, ibinder Service)
{
Mservice = (itestservice) service;
If (mservice! = NULL)
{
Mservice. showname ();
}
}

Public void onservicedisconnected (componentname name)
{

}
};
@ Override
Public Boolean oncreateoptionsmenu (menu ){
// Todo auto-generated method stub
Super. oncreateoptionsmenu (menu );
Menu. Add (0, menu. First + 1, 1, "openactivity ");
Menu. Add (0, menu. First + 2, 2, "startservice ");
Menu. Add (0, menu. First + 3, 3, "stopservice ");
Menu. Add (0, menu. First + 4, 4, "bindservice ");
Return true;
}

@ Override
Public Boolean onoptionsitemselected (menuitem item ){
// Todo auto-generated method stub
Super. onoptionsitemselected (item );
Switch (item. getitemid ())
{
Case menu. First + 1:
{
This. settitle ("Switch activity ");
Intent I = new intent ();
I. setaction ("test_action ");
If (tools. isintentavailable (this, I ))
This. startactivity (I );
Else
This. settitle ("the intent is unavailable !!! ");
Break;
}
Case menu. First + 2:
{
This. settitle ("Start Service ");
// Intent I = new intent (this, testservice. Class );
Intent I = new intent ();
I. setaction ("start_service ");
This. startservice (I );
Break;
}
Case menu. First + 3:
{
This. settitle ("Stop Service ");
Intent I = new intent (this, testservice. Class );
This. stopservice (I );
Break;
}
Case menu. First + 4:
{
This. settitle ("bind service! ");
Intent I = new intent (this, testservice. Class );
This. bindservice (I, this. sconnection, context. bind_auto_create );

Break;
}
}
Return true;
}

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
This. setcontentview (R. layout. Main );
}
}

Compile and execute. You will find that oncreate is executed first and then onbind is executed. When context. bindservice of the caller returns, onconnected of serviceconnection is not executed immediately.
Remote binding:
The above binding is the case where the caller and service are in the same application. If the caller and service are in different programs, the call method is another case. Let's take a look.

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.