Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.bindservice.binder;
- 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;
- import com.example.bindservice.binder.Server.LocalBinder;
- public class Client extends Activity {
- boolean mBounded;
- Server mServer;
- TextView text;
- Button button;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.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, Server.class);
- bindService(mIntent, mConnection, BIND_AUTO_CREATE);
- };
- ServiceConnection mConnection = new ServiceConnection() {
- @Override
- public void onServiceDisconnected(ComponentName name) {
- Toast.makeText(Client.this, "Service is disconnected", 1000).show();
- mBounded = false;
- mServer = null;
- }
- @Override
- public void onServiceConnected(ComponentName name, IBinder service) {
- Toast.makeText(Client.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;
- }
- };
- }
Add Comment
Please, Sign In to add comment