0

I'm trying to stop my service with stopService(), but it will still running. It only stops when I unregister the brodcast receiver, but when I register that again, there will be two actions activated at the same time. The thread is still running somehow, I really tried everything and reading every single post on the forum but I dind't find a solution.

Service Class

public class MyService extends Service {

    final static String MY_ACTION = "MY_ACTION";
    private static Context context;
    private Timer timer;
    public  String Data;
    String ok = "Database\n";

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub

        MyThread myThread = new MyThread();
        myThread.start();

        return super.onStartCommand(intent, flags, startId);
    }

    public class MyThread extends Thread{

        @Override
        public void run() {
            // TODO Auto-generated method stub

            try {
                int delay = 1000; // delay for 1 sec.
                int period = 5 * 1000; // repeat every 120 sec.
                timer = new Timer();
                timer.scheduleAtFixedRate(new TimerTask() {
                    public void run() {
                        Calendar c = Calendar.getInstance();
                        Data = String.valueOf((c.get(Calendar.MILLISECOND)));
                        Intent intent = new Intent();
                        intent.setAction(MY_ACTION);
                        intent.putExtra("DATAPASSED", ok);
                        sendBroadcast(intent);
                    }
                }, delay, period);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            stopSelf();
        }

    }
}

MainActivity

    final IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(MyService.MY_ACTION);
    registerReceiver(broadcastReceiver, intentFilter);
    final Intent intent = new Intent(MainActivity.this, MyService.class);
    //Start our own service

    btnStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //registerReceiver(broadcastReceiver, intentFilter);
            startService(intent);
            }
    });

    btnStop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //unregisterReceiver(broadcastReceiver);
            stopService(intent);
        }
    });
}
@Override
protected void onStop() {
    // TODO Auto-generated method stub
    unregisterReceiver(broadcastReceiver);
    super.onStop();
}
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        int datapassed = intent.getIntExtra("DATAPASSED", 0);
        String s = intent.getAction().toString();
        String s1 = intent.getStringExtra("DATAPASSED");
        textview.append(s1);
    }
};
Zxhammelzx
  • 23
  • 4
  • You can pass some action to intent when you're starting service, if you find action A then do your stuff in `onStartCommand`, if you find action B (means kill flag for service) then kill it. Let me know if still confused. – Jeel Vankhede Sep 29 '18 at 20:00
  • The point is, how do I kill MY_ACTION in this case? – Zxhammelzx Sep 29 '18 at 20:04
  • Do you know about binding service to any activity using service connection? If yes then use that approach instead of using broadcast for communication between service and activity. – Jeel Vankhede Sep 29 '18 at 20:07
  • Are you sure there isn't any solution for this and I have to go for that approach? – Zxhammelzx Sep 29 '18 at 20:10
  • Yes, i mean if you want just communication between activity and service, then this isn't the way. btw broadcast receiver is an expensive component to be use in terms of memory. – Jeel Vankhede Sep 29 '18 at 20:15
  • The only thing I need to do is to communicate witht the activity for changing the UI in this case my TextView – Zxhammelzx Sep 29 '18 at 20:16
  • check this https://stackoverflow.com/questions/2463175/how-to-have-android-service-communicate-with-activity – Jeel Vankhede Sep 29 '18 at 20:16

1 Answers1

0

What I'm doing wrong is calling stopSelf() inside my Thread which is causing the service calling the onDestroy() method. I just removed that and added myTimer and myTask as local variables, then I could add myTimer.cancel() onDestroy and it worked.

Zxhammelzx
  • 23
  • 4