Guest User

Untitled

a guest
Oct 16th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. package com.example.bindservice.binder;
  2.  
  3. import android.app.Activity;
  4. import android.content.ComponentName;
  5. import android.content.Intent;
  6. import android.content.ServiceConnection;
  7. import android.os.Bundle;
  8. import android.os.IBinder;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.widget.Button;
  12. import android.widget.TextView;
  13. import android.widget.Toast;
  14.  
  15. import com.example.bindservice.binder.Server.LocalBinder;
  16.  
  17. public class Client extends Activity {
  18.  
  19. boolean mBounded;
  20. Server mServer;
  21. TextView text;
  22. Button button;
  23.  
  24. @Override
  25. public void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.main);
  28.  
  29. text = (TextView)findViewById(R.id.text);
  30. button = (Button) findViewById(R.id.button);
  31. button.setOnClickListener(new OnClickListener() {
  32. public void onClick(View v) {
  33. text.setText(mServer.getTime());
  34. }
  35. });
  36. }
  37.  
  38. @Override
  39. protected void onStart() {
  40. super.onStart();
  41.  
  42. Intent mIntent = new Intent(this, Server.class);
  43. bindService(mIntent, mConnection, BIND_AUTO_CREATE);
  44. };
  45.  
  46. ServiceConnection mConnection = new ServiceConnection() {
  47. @Override
  48. public void onServiceDisconnected(ComponentName name) {
  49. Toast.makeText(Client.this, "Service is disconnected", 1000).show();
  50. mBounded = false;
  51. mServer = null;
  52. }
  53.  
  54. @Override
  55. public void onServiceConnected(ComponentName name, IBinder service) {
  56. Toast.makeText(Client.this, "Service is connected", 1000).show();
  57. mBounded = true;
  58. LocalBinder mLocalBinder = (LocalBinder)service;
  59. mServer = mLocalBinder.getServerInstance();
  60. }
  61. };
  62.  
  63. @Override
  64. protected void onStop() {
  65. super.onStop();
  66. if(mBounded) {
  67. unbindService(mConnection);
  68. mBounded = false;
  69. }
  70. };
  71. }
Add Comment
Please, Sign In to add comment