Professional Documents
Culture Documents
package com.example.localbound;
import
import
import
import
android.app.Service;
android.content.Intent;
android.os.Binder;
android.os.IBinder;
MyLocalBinder class is created and assigned to the myBinder IBinder reference (since Binder is a subclass of
IBinder there is no type mismatch in this assignment).
Next, the onBind() method needs to be modified to return a reference to the myBinder object and a new public
method implemented to return the current time when called by any clients that bind to the service:
package com.example.localbound;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import
import
import
import
android.app.Service;
android.content.Intent;
android.os.Binder;
android.os.IBinder;
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.localbound"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.localbound.LocalBound"
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=".BoundService" />
</application>
</manifest>
The next phase is to implement the necessary code within the activity to bind to the service and call the
getCurrentTime() method.
package com.example.localbound;
import
import
import
import
import
import
android.app.Activity;
android.app.ActionBar;
android.app.Fragment;
android.os.Bundle;
android.view.LayoutInflater;
android.view.Menu;
import
import
import
import
android.view.MenuItem;
android.view.View;
android.view.ViewGroup;
android.os.Build;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_local_bound);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Intent intent = new Intent(this, BoundService.class);
bindService(intent, myConnection, Context.BIND_AUTO_CREATE);
}
Save the file once the above changes have been made.
Next, switch to the XML view and modify the view elements to declare an onClick property for the button:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".LocalBoundActivity" >
<TextView
android:id="@+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/myTextView"
android:layout_centerHorizontal="true"
android:layout_marginTop="27dp"
android:onClick="showTime"
android:text="@string/mystring" />
</RelativeLayout>
Once the changes have been made to the XML, be sure to save the file before proceeding.
Finally, edit the code in the LocalBoundActivity.java file to implement the showTime() method. This method
simply calls the getCurrentTime() method of the service (which, thanks to the onServiceConnected() method, is
now available from within the activity via the myService reference) and assigns the resulting string to the
TextView:
package com.example.localbound;
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
import
android.app.Activity;
android.app.ActionBar;
android.app.Fragment;
android.os.Bundle;
android.view.LayoutInflater;
android.view.Menu;
android.view.MenuItem;
android.view.View;
android.view.ViewGroup;
android.os.Build;
android.os.IBinder;
android.content.Context;
android.content.Intent;
android.content.ComponentName;
android.content.ServiceConnection;
android.widget.TextView;
import com.example.localbound.BoundService.MyLocalBinder;
public class LocalBound extends Activity {
BoundService myService;
boolean isBound = false;
public void showTime(View view)
{
Summary
When a bound service is local and private to an application, components within that application can interact
with the service without the need to resort to inter-process communication (IPC). In general terms, the services
onBind() method returns an IBinder object containing a reference to the instance of the running service. The
client component implements a ServiceConnection subclass containing callback methods that are called when
the service is connected and disconnected. The former method is passed the IBinder object returned by the
onBind() method allowing public methods within the service to be called.
Note:
The difference between remote service and local service is: Local service runs in the same process and
remote service runs in different process and may be in different application.
You can access a remote service which is running in the different application but you can't access a local
service that is running in a different application.