Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- textView = (TextView) findViewById(R.id.textView);
- IntentFilter filter = new IntentFilter();
- filter.addAction("SOME_ACTION");
- receiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- textView.setText("hey");
- }
- };
- registerReceiver(receiver, filter);
- buttonStart = (Button) findViewById(R.id.start);
- buttonStart.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- startService(new Intent(MainActivity.this, LocalService.class));
- }
- });
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- unregisterReceiver(receiver);
- }
- public class LocalService extends Service
- {
- private static Timer timer = new Timer();
- public IBinder onBind(Intent arg0)
- {
- return null;
- }
- public void onCreate()
- {
- super.onCreate();
- startService();
- }
- private void startService()
- {
- timer.scheduleAtFixedRate(new mainTask(), 0, 5000);
- }
- private class mainTask extends TimerTask
- {
- public void run()
- {
- Intent intent = new Intent();
- intent.setAction("SOME_ACTION");
- sendBroadcast(intent);
- }
- }
- public void onDestroy()
- {
- super.onDestroy();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement