Bind Service
Method for starting the bind service:
BindService(intent,service connection,flags);
Method for disconnect the bind service:
UnbindService(Service connected);
Implement ServiceConnection:
Your implementation must override two callback methods:
Method for starting the bind service:
BindService(intent,service connection,flags);
Method for disconnect the bind service:
UnbindService(Service connected);
Implement ServiceConnection:
Your implementation must override two callback methods:
onServiceConnected()
onServiceDisconnected()
Example program :
It is a xml file:
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bindservice"
android:onClick="bind"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
In MainActivity we call the serice :
public class MainActivity extends AppCompatActivity implements
ServiceConnection {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onServiceConnected(ComponentName name, IBinder
service) {
MyService.MyBinder myBinder = (MyService.MyBinder)service;
MyService s = (MyService)myBinder.getService();
Toast.makeText(MainActivity.this,"service
connected"+s.getvalue(),Toast.LENGTH_LONG).show();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
public void bind(View v){
Intent intent=new Intent(MainActivity.this,MyService.class);
bindService(intent,MainActivity.this,BIND_AUTO_CREATE);
}
}
It is a Service:
public class MyService extends Service {
private int a = 10;
public MyService() {
}
public IBinder mBinder = new MyBinder();
class MyBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public int getvalue() {
return a;
}
@Override
public boolean onUnbind(Intent intent) {
return true;
}
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
}
}
- The system calls this to deliver the IBinder returned by the service's
onServiceDisconnected()
- The Android system calls this when the connection to the service is
Lifecycle for Bind Service:
- A bound service is an implementation of the Service class that allows other applications to bind to it and interact with it.
- To provide binding for a service, you must implement the onBind()callback method.
- This method returns an IBinder object that defines the programming interface that clients can use to interact with the service.
Example program :
It is a xml file:
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bindservice"
android:onClick="bind"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
In MainActivity we call the serice :
public class MainActivity extends AppCompatActivity implements
ServiceConnection {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onServiceConnected(ComponentName name, IBinder
service) {
MyService.MyBinder myBinder = (MyService.MyBinder)service;
MyService s = (MyService)myBinder.getService();
Toast.makeText(MainActivity.this,"service
connected"+s.getvalue(),Toast.LENGTH_LONG).show();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
public void bind(View v){
Intent intent=new Intent(MainActivity.this,MyService.class);
bindService(intent,MainActivity.this,BIND_AUTO_CREATE);
}
}
It is a Service:
public class MyService extends Service {
private int a = 10;
public MyService() {
}
public IBinder mBinder = new MyBinder();
class MyBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public int getvalue() {
return a;
}
@Override
public boolean onUnbind(Intent intent) {
return true;
}
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
}
}
Please share your feedback about this topic.
0 comments:
Post a Comment