Wednesday, September 25, 2013

using IBinder class to Bind service

There are total 3 ways to bind a service with application components
  1. Using IBinder class
  2. Using Messanger class
  3. Using AIDL
The life is a process of learning even after some bad happening we are going on and learning things. someone asked me about the Binding Services and i was unable to answer  and that after i tried to work on the same.

Create a project and have a main activity with Name MainActivity.

Implementing the Binder

to implement the binder creat a class SomeServic which extends Service class, i create an inner class with name LocalBinder which is inner class and inside SomeService.

package com.example.bindserviceusingbinderclass;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class SomeService extends Service {

IBinder mBinder = new LocalBinder();

@Override
public IBinder onBind(Intent intent) {
return mBinder;
}

public class LocalBinder extends Binder {
public SomeService getServerInstance() {
return SomeService.this;
}
}

public String getTime() {
SimpleDateFormat mDateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
return mDateFormat.format(new Date());
}
}


finally we need to start service at Mainactivity

Binding the Client to the Service

package com.example.bindserviceusingbinderclass;

import com.example.bindserviceusingbinderclass.SomeService.LocalBinder;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;



public class MainActivity extends Activity {

boolean mBounded;
SomeService mServer;
TextView text;
Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

text = (TextView) findViewById(R.id.text);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
text.setText(mServer.getTime());
}
});
}

@Override
protected void onStart() {
super.onStart();
Intent mIntent = new Intent(this, SomeService.class);
bindService(mIntent, mConnection, BIND_AUTO_CREATE);
};

ServiceConnection mConnection = new ServiceConnection() {

public void onServiceDisconnected(ComponentName name) {
Toast.makeText(MainActivity.this, "Service is disconnected", 1000).show();
mBounded = false;
mServer = null;
}

public void onServiceConnected(ComponentName name, IBinder service) {
Toast.makeText(MainActivity.this, "Service is connected", 1000).show();
mBounded = true;
LocalBinder mLocalBinder = (LocalBinder) service;
mServer = mLocalBinder.getServerInstance();
}
};

@Override
protected void onStop() {
super.onStop();
if (mBounded) {
unbindService(mConnection);
mBounded = false;
}
};
@Override
protected void onDestroy() {
super.onStop();
if (mBounded) {
unbindService(mConnection);
mBounded = false;
}
};
}


do not forget to work with manifest for the service.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bindserviceusingbinderclass"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.bindserviceusingbinderclass.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=".SomeService" />
    </application>
    

</manifest>

the activity_main.xml looks like

<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=".MainActivity" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/text"
        android:layout_below="@+id/text"
        android:layout_marginLeft="36dp"
        android:layout_marginTop="54dp"
        android:text="Button" />

</RelativeLayout>



No comments:

Post a Comment