AndroidManifest.xml
<service android:name=".MyService"/>
MyService.java
public class MyService extends Service { Binder mBinder = new LocalService(); @Nullable @Override public IBinder onBind(Intent intent) { return mBinder; } public class LocalService extends Binder { MyService getService() { return MyService.this; } } public String getFirstMessage() { return "Message 1"; } public String getSecondMessage() { return "Message 2"; } }
MainActivity.java
public class MainActivity extends AppCompatActivity { MyService myService = null; Button buttonMessage1; Button buttonMessage2; TextView textViewOutput; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(MainActivity.this, MyService.class); bindService(intent, serviceConnection, BIND_AUTO_CREATE); buttonMessage1 = (Button) findViewById(R.id.buttonMessage1); buttonMessage2 = (Button) findViewById(R.id.buttonMessage2); textViewOutput = (TextView) findViewById(R.id.textViewOutput); buttonMessage1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (myService != null) { textViewOutput.setText(myService.getFirstMessage()); } } }); buttonMessage2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (myService != null) { textViewOutput.setText(myService.getSecondMessage()); } } }); } ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { MyService.LocalService localService = (MyService.LocalService) iBinder; myService = localService.getService(); } @Override public void onServiceDisconnected(ComponentName componentName) { myService = null; } }; @Override protected void onStop() { super.onStop(); unbindService(serviceConnection); myService = null; } }
References
https://www.youtube.com/watch?v=PUxC6vzEEgg
https://github.com/mhdr/AndroidSamples/tree/master/101