Android learning notes: The activity calls the service across processes.

Source: Internet
Author: User

1. Create an android project as follows. Click Finish. If you only need to provide the service and do not need the activity, you can remove the Create activity check.

 

2. Add a Java file.

Class Name: longexistservice,

Superclass: Android. App. Service

 

3. Open longexistservice. Java and click source-> override/implement method in the menu.

Select oncreate (), ondestroy (), onstart (), onstartcommand (), onunbind ().

 

4. The longexistservice. Java file contains the following content:

Package com. Pyk. Les;

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

Public class longexistservice extends Service {

Public int getcount (){
Log. D (TAG, "longexistservice: getcount ");
Return 0;
}

Public String gettime (){
Java. util. Date = new java. util. Date ();
Log. D (TAG, "longexistservice: oncreate ");
Return date. tolocalestring ();
}

Private Final ilongexistservice. Stub binder = new ilongexistservice. Stub (){
Public int getcount (){
Log. D (TAG, "ilongexistservice: getcount ");
Return longexistservice. This. getcount ();
}

Public String gettime (){
Log. D (TAG, "ilongexistservice: gettime ");
Return longexistservice2.this. gettime ();
}
};

Final string tag = "longexistservice ";

@ Override
Public void oncreate (){
Log. D (TAG, "oncreate ");
Super. oncreate ();
}

@ Override
Public void ondestroy (){
Log. D (TAG, "ondestroy ");
Super. ondestroy ();
}

@ Override
Public void onstart (intent, int startid ){
Log. D (TAG, "onstart ");
Super. onstart (intent, startid );
}

@ Override
Public int onstartcommand (intent, int flags, int startid ){
Log. D (TAG, "onstartcommand ");
Return super. onstartcommand (intent, flags, startid );
}

@ Override
Public Boolean onunbind (intent ){
Log. D (TAG, "onunbind ");
Return super. onunbind (intent );
}

@ Override
Public void onrebind (intent ){
Log. D (TAG, "onrebind ");
Super. onrebind (intent );
}

@ Override
Public ibinder onbind (intent arg0 ){
Log. D (TAG, "onbind ");
Return binder;
}
}

5. Because the aidl interface is added, we need to first define an aidl file, for example, ilongexistservice. aidl. The file content is:

Package com. Pyk. Les;

 

Interface ilongexistservice {

Int getcount ();

String gettime ();

}

It must be suffixed with aidl. I have tried to define the same interface with the. Java file, and the result cannot run properly. This seems to be a requirement that must be named aidl.

6. Modify androidmanifest. xml and add the following definition.

<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "com. Pyk. les2" Android: versioncode = "1" Android: versionname = "1.0">
<Application Android: Label = "single service">
<Service android: Name = ". longexistservice">
<Intent-filter>
<Action Android: Name = "com. Pyk. Les. ilongexistservice"> </Action>
</Intent-filter>
</Service>
</Application>
<Uses-SDK Android: minsdkversion = "8"/>
</Manifest>

 

 

7. At this point, the service has been implemented. Next we will generate another project activity to bind this service.

Follow the steps 1 above to generate an activity. Use another package name. The file content of activity. Java is:

Package com. Pyk. client;

Import com. Pyk. Les. ilongexistservice;
Import Android. App. activity;
Import Android. App. alertdialog;
Import Android. content. componentname;
Import Android. content. intent;
Import Android. content. serviceconnection;
Import Android. OS. Bundle;
Import Android. OS. ibinder;
Import Android. OS. RemoteException;
Import Android. util. log;
Import Android. View. view;

Public class myactivity extends activity {
Final string tag = "myactivity ";
Final intent myintent = new intent ("com. Pyk. Les. ilongexistservice ");
Private Boolean startedservice = false;
Private ilongexistservice leservice = NULL;
Serviceconnection myserviceconnection = new serviceconnection ()
{
Public void onserviceconnected (componentname name, ibinder Service ){
Leservice = ilongexistservice. stub. asinterface (service );
}

Public void onservicedisconnected (componentname name ){
Leservice = NULL;
}

};
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
// Myintent. setclass (this, longexistservice. Class );
Startedservice = bindservice (new intent ("com. Pyk. Les. ilongexistservice"), myserviceconnection, bind_auto_create );
Try {
Log. I (TAG, "getcount value:" + leservice. getcount ());
Log. I (TAG, "gettime value:" + leservice. gettime ());
} Catch (RemoteException ex)
{}
}

@ Override
Protected void ondestroy (){
If (startedservice)
{
Unbindservice (myserviceconnection );
}
Log. I (TAG, "ondestroy ");
Super. ondestroy ();
}
 
}

8. Copy ilongexistservice. aidl together with the package directory. Refresh the activity project and you will see it in project explorer.

 

In this way, when the activity is started, it will bind the service and call the service method. When the activity exits, it will be unbind.

 

 

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.