Advertisement
Antole33

Untitled

Apr 20th, 2018 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.00 KB | None | 0 0
  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_main);
  5. textView = (TextView) findViewById(R.id.textView);
  6. IntentFilter filter = new IntentFilter();
  7. filter.addAction("SOME_ACTION");
  8. receiver = new BroadcastReceiver() {
  9. @Override
  10. public void onReceive(Context context, Intent intent) {
  11. textView.setText("hey");
  12. }
  13. };
  14. registerReceiver(receiver, filter);
  15. buttonStart = (Button) findViewById(R.id.start);
  16. buttonStart.setOnClickListener(new View.OnClickListener() {
  17. public void onClick(View v) {
  18. startService(new Intent(MainActivity.this, LocalService.class));
  19. }
  20. });
  21. }
  22.  
  23. @Override
  24. protected void onDestroy() {
  25. super.onDestroy();
  26. unregisterReceiver(receiver);
  27. }
  28.  
  29. public class LocalService extends Service
  30. {
  31. private static Timer timer = new Timer();
  32.  
  33. public IBinder onBind(Intent arg0)
  34. {
  35. return null;
  36. }
  37.  
  38. public void onCreate()
  39. {
  40. super.onCreate();
  41. startService();
  42. }
  43.  
  44. private void startService()
  45. {
  46. timer.scheduleAtFixedRate(new mainTask(), 0, 5000);
  47. }
  48.  
  49. private class mainTask extends TimerTask
  50. {
  51. public void run()
  52. {
  53. Intent intent = new Intent();
  54. intent.setAction("SOME_ACTION");
  55. sendBroadcast(intent);
  56. }
  57. }
  58.  
  59. public void onDestroy()
  60. {
  61. super.onDestroy();
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement