Android service (medium)

Source: Internet
Author: User

Bound service

Before a service is bound to an activity, the activity only allows the service to work, but the service cannot return data to the activity. So here we will introduce the bound service.

Boundservice or service, so you need to configure it in the configuration file:

========= AndroidManifest. xml ==================================

<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. yx. boundservice"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">

<Uses-sdk
Android: minSdkVersion = "8"
Android: targetSdkVersion = "18"/>

<Application
Android: allowBackup = "true"
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name"
Android: theme = "@ style/AppTheme">
<Activity
Android: name = "com. yx. boundservice. MainActivity"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>

<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
<Service android: name = "com. yx. boundservice. SecondService"> </service>
</Application>

</Manifest>
================================= MainActivity. java ========================

Package com. yx. boundservice;

Import com. yx. boundservice. SecondService. FirstBinder;

Import android. OS. Bundle;
Import android. OS. IBinder;
Import android. app. Activity;
Import android. content. ComponentName;
Import android. content. Intent;
Import android. content. ServiceConnection;
Import android. view. Menu;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;

Public class MainActivity extends Activity {

Private Button button;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Button = (Button) findViewById (R. id. button );
Button. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Intent intent = new Intent ();
Intent. setClass (MainActivity. this, SecondService. class );

BindService (intent, conn, BIND_AUTO_CREATE); // The third parameter is the service object automatically created when a service is bound.
}
});
}

@ 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;
}

ServiceConnection conn = new ServiceConnection (){

@ Override
Public void onServiceDisconnected (ComponentName name ){
// TODO Auto-generated method stub

}

@ Override
Public void onServiceConnected (ComponentName name, IBinder service) {//
FirstBinder binder = (FirstBinder) service;
String data = binder. getData ();
System. out. println ("data --->" + data );
}
};
}
========================== SecondService. java ================

Package com. yx. boundservice;

Import android. app. Service;
Import android. content. Intent;
Import android. OS. Binder;
Import android. OS. IBinder;

Public class SecondService extends Service {

// This method is called when other application components (activities) are bound to the current Setvice.
@ Override
Public IBinder onBind (Intent intent ){
IBinder binder = new FirstBinder ();
Return binder;
}

Class FirstBinder extends Binder {
Public String getData (){
Return "test data ";
}
}

}
Finally, let's look at the execution sequence from the code above.

First, execute the onClick event of MainActivity, create an intent object in The onClick event, and associate it with the current activity and SecondService. BindService (intent, conn, BIND_AUTO_CREATE) is used to bind intent and ServiceConnection, And the onServiceConnected method in ServiceConnection is called. In this method, the input parameter IBinder is included, this parameter is the binder object returned from the onBind method in the SecondService file and can be called to the service through this object.

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.