Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. package at.htl_leonding.servicedemo;
  2.  
  3. import android.content.ComponentName;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.ServiceConnection;
  7. import android.os.Bundle;
  8. import android.os.IBinder;
  9. import android.support.design.widget.FloatingActionButton;
  10. import android.support.design.widget.Snackbar;
  11. import android.support.v7.app.AppCompatActivity;
  12. import android.support.v7.widget.Toolbar;
  13. import android.util.Log;
  14. import android.view.View;
  15. import android.view.Menu;
  16. import android.view.MenuItem;
  17.  
  18. public class MainActivity extends AppCompatActivity {
  19.  
  20. private static final String LOG_TAG = MainActivity.class.getSimpleName();
  21. private MyService.MyServiceBinder serviceBinder;
  22.  
  23.  
  24. private ServiceConnection serviceConnection = new ServiceConnection() {
  25. @Override
  26. public void onServiceConnected(ComponentName name, IBinder service) {
  27. Log.d(LOG_TAG, "onServiceConnected()");
  28. serviceBinder = (MyService.MyServiceBinder) service;
  29. }
  30.  
  31. @Override
  32. public void onServiceDisconnected(ComponentName name) {
  33. Log.d(LOG_TAG, "onServiceConnected()");
  34. }
  35. };
  36. @Override
  37. protected void onCreate(Bundle savedInstanceState) {
  38. super.onCreate(savedInstanceState);
  39. setContentView(R.layout.activity_main);
  40. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  41. setSupportActionBar(toolbar);
  42.  
  43. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  44. fab.setOnClickListener(new View.OnClickListener() {
  45. @Override
  46. public void onClick(View view) {
  47. serviceBinder.startService();
  48. }
  49. });
  50. }
  51.  
  52. @Override
  53. protected void onResume() {
  54. super.onResume();
  55. Intent serviceIntent = new Intent(getBaseContext(), MyService.class);
  56. getApplicationContext().bindService(serviceIntent, serviceConnection, Context.BIND_AUTO_CREATE);
  57. }
  58.  
  59. @Override
  60. protected void onPause() {
  61. getApplicationContext().unbindService(serviceConnection);
  62. super.onPause();
  63.  
  64. }
  65.  
  66. @Override
  67. public boolean onCreateOptionsMenu(Menu menu) {
  68. // Inflate the menu; this adds items to the action bar if it is present.
  69. getMenuInflater().inflate(R.menu.menu_main, menu);
  70. return true;
  71. }
  72.  
  73. @Override
  74. public boolean onOptionsItemSelected(MenuItem item) {
  75. // Handle action bar item clicks here. The action bar will
  76. // automatically handle clicks on the Home/Up button, so long
  77. // as you specify a parent activity in AndroidManifest.xml.
  78. int id = item.getItemId();
  79.  
  80. //noinspection SimplifiableIfStatement
  81. if (id == R.id.action_settings) {
  82. return true;
  83. }
  84.  
  85. return super.onOptionsItemSelected(item);
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement