Activity doesnt mantain connection to its service after minimalising it












0















I have a problem with an activity after minimalising. Everything is going ok when i start an activity and press start button. But when i minimalise activity and again maximalize it, it doesnt respond to my buttons and commands. Anybody know what to do? This is my first android app so i dont know what is going on..



here are my classes :




TrackerService




package sk.tuke.smart.makac.services;

import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;


public class TrackerService extends Service implements LocationListener {

private Intent commandIntent;
private long duration;
private boolean paused,checkedAfterPause;
private int sportActivity;
private double distance,pace,calories;
private ArrayList<Location> finalPositionList = new ArrayList<Location>();
private LocationManager locationManager;




private static final String TAG = "TrackerService";



@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.i(TAG,"onStart intent " +intent.getAction());
commandIntent=intent;
checkedAfterPause=true;
if(intent.getAction() == "sk.tuke.smart.makac.COMMAND_START"){
if(intent.getAction() == "sk.tuke.smart.makac.COMMAND_START") {
duration = 0;
}
new Timer().scheduleAtFixedRate(new TimerTask()
{

@Override
public void run() {
if(!paused){
duration++;
Intent intent1 = new Intent();
intent1.setAction("sk.tuke.smart.makac.TICK");
intent1.putExtra("duration", duration);
intent1.putExtra("distance",distance);
sendBroadcast(intent1);
Log.i(TAG,"" + duration);
}
}
}, 1000, 1000);
}else if (intent.getAction() == "sk.tuke.smart.makac.COMMAND_PAUSE"){
paused=true;
locationManager.removeUpdates(this);
}
if(intent.getAction() == "sk.tuke.smart.makac.COMMAND_CONTINUE"){
paused=false;
checkedAfterPause=false;
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,10,this);
}

}


@Override
public void onCreate() {

super.onCreate();
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,10,this);

}

@Override
public void onDestroy() {
locationManager.removeUpdates(this);
}



@Override
public IBinder onBind(Intent intent) {


return null;

}

@Override
public void onLocationChanged(Location location) {
finalPositionList.add(location);
Location lastLocation;
double minDistance;
if (finalPositionList.size() != 1 && checkedAfterPause) {
lastLocation = finalPositionList.get(finalPositionList.size() - 2);
minDistance=location.distanceTo(lastLocation);
if(minDistance>=2){
distance += location.distanceTo(lastLocation);
}else{
finalPositionList.remove(finalPositionList.size()-1);
}
}
if(commandIntent.getAction() == "sk.tuke.smart.makac.COMMAND_CONTINUE" && !checkedAfterPause){
Log.i(TAG,"checking distance after pause");
lastLocation = finalPositionList.get(finalPositionList.size() - 2);
minDistance=location.distanceTo(lastLocation);
if(minDistance<=100){
distance += location.distanceTo(lastLocation);
}
checkedAfterPause=true;
}
Log.i(TAG,"locations " + finalPositionList);
Log.i(TAG,"distance = " + distance);

}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

}

public long getDuration(){
return duration;
}



public double getPace(){
return pace;
}




}


SportsActivity







package sk.tuke.smart.makac;



import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import sk.tuke.smart.makac.helpers.MainHelper;
import sk.tuke.smart.makac.services.TrackerService;

public class StopwatchActivity extends AppCompatActivity {

private static final int MY_PERMISSIONS_REQUEST_FINE_LOCATION = 111;
private static final String TAG = "StopwatchActivity";
private boolean started;
private boolean running;
private boolean paused=false;
private long duration;
private double distance;
private MainHelper helper;
private TextView durationView,distanceView;
private Button startButton,endButton;

private BroadcastReceiver receiver = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction()=="sk.tuke.smart.makac.TICK"){
duration = intent.getLongExtra("duration",duration);
distance = intent.getDoubleExtra("distance",distance);
durationView.setText(helper.formatDuration(duration));
distanceView.setText(helper.formatDistance(distance));
}

}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stopwatch);
IntentFilter intentFilter = new IntentFilter("sk.tuke.smart.makac.TICK");
registerReceiver(receiver,intentFilter);
started=false;
running=false;
helper = new MainHelper();
durationView = findViewById(R.id.textview_stopwatch_duration);
distanceView = findViewById(R.id.textview_stopwatch_distance);
startButton = findViewById(R.id.button_stopwatch_start);
endButton = findViewById(R.id.button_stopwatch_endworkout);
if(!canAccessLocation()){
ActivityCompat.requestPermissions(this,
new String{
Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_FINE_LOCATION);

}
}


public void toggle(View view){

Intent intent = new Intent(this,TrackerService.class);
started=true;
this.running = !this.running;
if(running && started){
startButton.setText("Stop");
endButton.setVisibility(view.GONE);
if(paused){
intent.setAction("sk.tuke.smart.makac.COMMAND_CONTINUE");
paused=false;
}else
intent.setAction("sk.tuke.smart.makac.COMMAND_START");

}

if(!running && started){
startButton.setText("Continue");
endButton.setVisibility(view.VISIBLE);
intent.setAction("sk.tuke.smart.makac.COMMAND_PAUSE");
paused=true;
}


startService(intent);


}

public void endWorkout(View view){
Intent intent = new Intent(this,TrackerService.class);
intent.setAction("sk.tuke.smart.makac.COMMAND_STOP");
startService(intent);
setContentView(R.layout.activity_workout_detail);
onStop();
}


public void openMaps(View view){
Intent intent = new Intent(StopwatchActivity.this, MapsActivity.class);
startActivity(intent);
}


@Override
protected void onStart() {
super.onStart();
}

@Override
protected void onStop() {
super.onStop();
Intent intent= new Intent(this,TrackerService.class);
stopService(intent);
}


private boolean hasPermission(String perm) {
return(PackageManager.PERMISSION_GRANTED==checkSelfPermission(perm));
}

private boolean canAccessLocation() {
return(hasPermission(Manifest.permission.ACCESS_FINE_LOCATION));
}
}









share|improve this question



























    0















    I have a problem with an activity after minimalising. Everything is going ok when i start an activity and press start button. But when i minimalise activity and again maximalize it, it doesnt respond to my buttons and commands. Anybody know what to do? This is my first android app so i dont know what is going on..



    here are my classes :




    TrackerService




    package sk.tuke.smart.makac.services;

    import android.app.Service;
    import android.content.Intent;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.util.Log;

    import java.util.ArrayList;
    import java.util.Timer;
    import java.util.TimerTask;


    public class TrackerService extends Service implements LocationListener {

    private Intent commandIntent;
    private long duration;
    private boolean paused,checkedAfterPause;
    private int sportActivity;
    private double distance,pace,calories;
    private ArrayList<Location> finalPositionList = new ArrayList<Location>();
    private LocationManager locationManager;




    private static final String TAG = "TrackerService";



    @Override
    public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    Log.i(TAG,"onStart intent " +intent.getAction());
    commandIntent=intent;
    checkedAfterPause=true;
    if(intent.getAction() == "sk.tuke.smart.makac.COMMAND_START"){
    if(intent.getAction() == "sk.tuke.smart.makac.COMMAND_START") {
    duration = 0;
    }
    new Timer().scheduleAtFixedRate(new TimerTask()
    {

    @Override
    public void run() {
    if(!paused){
    duration++;
    Intent intent1 = new Intent();
    intent1.setAction("sk.tuke.smart.makac.TICK");
    intent1.putExtra("duration", duration);
    intent1.putExtra("distance",distance);
    sendBroadcast(intent1);
    Log.i(TAG,"" + duration);
    }
    }
    }, 1000, 1000);
    }else if (intent.getAction() == "sk.tuke.smart.makac.COMMAND_PAUSE"){
    paused=true;
    locationManager.removeUpdates(this);
    }
    if(intent.getAction() == "sk.tuke.smart.makac.COMMAND_CONTINUE"){
    paused=false;
    checkedAfterPause=false;
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,10,this);
    }

    }


    @Override
    public void onCreate() {

    super.onCreate();
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,10,this);

    }

    @Override
    public void onDestroy() {
    locationManager.removeUpdates(this);
    }



    @Override
    public IBinder onBind(Intent intent) {


    return null;

    }

    @Override
    public void onLocationChanged(Location location) {
    finalPositionList.add(location);
    Location lastLocation;
    double minDistance;
    if (finalPositionList.size() != 1 && checkedAfterPause) {
    lastLocation = finalPositionList.get(finalPositionList.size() - 2);
    minDistance=location.distanceTo(lastLocation);
    if(minDistance>=2){
    distance += location.distanceTo(lastLocation);
    }else{
    finalPositionList.remove(finalPositionList.size()-1);
    }
    }
    if(commandIntent.getAction() == "sk.tuke.smart.makac.COMMAND_CONTINUE" && !checkedAfterPause){
    Log.i(TAG,"checking distance after pause");
    lastLocation = finalPositionList.get(finalPositionList.size() - 2);
    minDistance=location.distanceTo(lastLocation);
    if(minDistance<=100){
    distance += location.distanceTo(lastLocation);
    }
    checkedAfterPause=true;
    }
    Log.i(TAG,"locations " + finalPositionList);
    Log.i(TAG,"distance = " + distance);

    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }

    public long getDuration(){
    return duration;
    }



    public double getPace(){
    return pace;
    }




    }


    SportsActivity







    package sk.tuke.smart.makac;



    import android.Manifest;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.content.pm.PackageManager;
    import android.support.v4.app.ActivityCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    import sk.tuke.smart.makac.helpers.MainHelper;
    import sk.tuke.smart.makac.services.TrackerService;

    public class StopwatchActivity extends AppCompatActivity {

    private static final int MY_PERMISSIONS_REQUEST_FINE_LOCATION = 111;
    private static final String TAG = "StopwatchActivity";
    private boolean started;
    private boolean running;
    private boolean paused=false;
    private long duration;
    private double distance;
    private MainHelper helper;
    private TextView durationView,distanceView;
    private Button startButton,endButton;

    private BroadcastReceiver receiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
    if(intent.getAction()=="sk.tuke.smart.makac.TICK"){
    duration = intent.getLongExtra("duration",duration);
    distance = intent.getDoubleExtra("distance",distance);
    durationView.setText(helper.formatDuration(duration));
    distanceView.setText(helper.formatDistance(distance));
    }

    }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_stopwatch);
    IntentFilter intentFilter = new IntentFilter("sk.tuke.smart.makac.TICK");
    registerReceiver(receiver,intentFilter);
    started=false;
    running=false;
    helper = new MainHelper();
    durationView = findViewById(R.id.textview_stopwatch_duration);
    distanceView = findViewById(R.id.textview_stopwatch_distance);
    startButton = findViewById(R.id.button_stopwatch_start);
    endButton = findViewById(R.id.button_stopwatch_endworkout);
    if(!canAccessLocation()){
    ActivityCompat.requestPermissions(this,
    new String{
    Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},
    MY_PERMISSIONS_REQUEST_FINE_LOCATION);

    }
    }


    public void toggle(View view){

    Intent intent = new Intent(this,TrackerService.class);
    started=true;
    this.running = !this.running;
    if(running && started){
    startButton.setText("Stop");
    endButton.setVisibility(view.GONE);
    if(paused){
    intent.setAction("sk.tuke.smart.makac.COMMAND_CONTINUE");
    paused=false;
    }else
    intent.setAction("sk.tuke.smart.makac.COMMAND_START");

    }

    if(!running && started){
    startButton.setText("Continue");
    endButton.setVisibility(view.VISIBLE);
    intent.setAction("sk.tuke.smart.makac.COMMAND_PAUSE");
    paused=true;
    }


    startService(intent);


    }

    public void endWorkout(View view){
    Intent intent = new Intent(this,TrackerService.class);
    intent.setAction("sk.tuke.smart.makac.COMMAND_STOP");
    startService(intent);
    setContentView(R.layout.activity_workout_detail);
    onStop();
    }


    public void openMaps(View view){
    Intent intent = new Intent(StopwatchActivity.this, MapsActivity.class);
    startActivity(intent);
    }


    @Override
    protected void onStart() {
    super.onStart();
    }

    @Override
    protected void onStop() {
    super.onStop();
    Intent intent= new Intent(this,TrackerService.class);
    stopService(intent);
    }


    private boolean hasPermission(String perm) {
    return(PackageManager.PERMISSION_GRANTED==checkSelfPermission(perm));
    }

    private boolean canAccessLocation() {
    return(hasPermission(Manifest.permission.ACCESS_FINE_LOCATION));
    }
    }









    share|improve this question

























      0












      0








      0








      I have a problem with an activity after minimalising. Everything is going ok when i start an activity and press start button. But when i minimalise activity and again maximalize it, it doesnt respond to my buttons and commands. Anybody know what to do? This is my first android app so i dont know what is going on..



      here are my classes :




      TrackerService




      package sk.tuke.smart.makac.services;

      import android.app.Service;
      import android.content.Intent;
      import android.location.Location;
      import android.location.LocationListener;
      import android.location.LocationManager;
      import android.os.Bundle;
      import android.os.IBinder;
      import android.util.Log;

      import java.util.ArrayList;
      import java.util.Timer;
      import java.util.TimerTask;


      public class TrackerService extends Service implements LocationListener {

      private Intent commandIntent;
      private long duration;
      private boolean paused,checkedAfterPause;
      private int sportActivity;
      private double distance,pace,calories;
      private ArrayList<Location> finalPositionList = new ArrayList<Location>();
      private LocationManager locationManager;




      private static final String TAG = "TrackerService";



      @Override
      public void onStart(Intent intent, int startId) {
      super.onStart(intent, startId);
      Log.i(TAG,"onStart intent " +intent.getAction());
      commandIntent=intent;
      checkedAfterPause=true;
      if(intent.getAction() == "sk.tuke.smart.makac.COMMAND_START"){
      if(intent.getAction() == "sk.tuke.smart.makac.COMMAND_START") {
      duration = 0;
      }
      new Timer().scheduleAtFixedRate(new TimerTask()
      {

      @Override
      public void run() {
      if(!paused){
      duration++;
      Intent intent1 = new Intent();
      intent1.setAction("sk.tuke.smart.makac.TICK");
      intent1.putExtra("duration", duration);
      intent1.putExtra("distance",distance);
      sendBroadcast(intent1);
      Log.i(TAG,"" + duration);
      }
      }
      }, 1000, 1000);
      }else if (intent.getAction() == "sk.tuke.smart.makac.COMMAND_PAUSE"){
      paused=true;
      locationManager.removeUpdates(this);
      }
      if(intent.getAction() == "sk.tuke.smart.makac.COMMAND_CONTINUE"){
      paused=false;
      checkedAfterPause=false;
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,10,this);
      }

      }


      @Override
      public void onCreate() {

      super.onCreate();
      locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,10,this);

      }

      @Override
      public void onDestroy() {
      locationManager.removeUpdates(this);
      }



      @Override
      public IBinder onBind(Intent intent) {


      return null;

      }

      @Override
      public void onLocationChanged(Location location) {
      finalPositionList.add(location);
      Location lastLocation;
      double minDistance;
      if (finalPositionList.size() != 1 && checkedAfterPause) {
      lastLocation = finalPositionList.get(finalPositionList.size() - 2);
      minDistance=location.distanceTo(lastLocation);
      if(minDistance>=2){
      distance += location.distanceTo(lastLocation);
      }else{
      finalPositionList.remove(finalPositionList.size()-1);
      }
      }
      if(commandIntent.getAction() == "sk.tuke.smart.makac.COMMAND_CONTINUE" && !checkedAfterPause){
      Log.i(TAG,"checking distance after pause");
      lastLocation = finalPositionList.get(finalPositionList.size() - 2);
      minDistance=location.distanceTo(lastLocation);
      if(minDistance<=100){
      distance += location.distanceTo(lastLocation);
      }
      checkedAfterPause=true;
      }
      Log.i(TAG,"locations " + finalPositionList);
      Log.i(TAG,"distance = " + distance);

      }

      @Override
      public void onStatusChanged(String s, int i, Bundle bundle) {

      }

      @Override
      public void onProviderEnabled(String s) {

      }

      @Override
      public void onProviderDisabled(String s) {

      }

      public long getDuration(){
      return duration;
      }



      public double getPace(){
      return pace;
      }




      }


      SportsActivity







      package sk.tuke.smart.makac;



      import android.Manifest;
      import android.content.BroadcastReceiver;
      import android.content.Context;
      import android.content.Intent;
      import android.content.IntentFilter;
      import android.content.pm.PackageManager;
      import android.support.v4.app.ActivityCompat;
      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.Button;
      import android.widget.TextView;

      import sk.tuke.smart.makac.helpers.MainHelper;
      import sk.tuke.smart.makac.services.TrackerService;

      public class StopwatchActivity extends AppCompatActivity {

      private static final int MY_PERMISSIONS_REQUEST_FINE_LOCATION = 111;
      private static final String TAG = "StopwatchActivity";
      private boolean started;
      private boolean running;
      private boolean paused=false;
      private long duration;
      private double distance;
      private MainHelper helper;
      private TextView durationView,distanceView;
      private Button startButton,endButton;

      private BroadcastReceiver receiver = new BroadcastReceiver() {

      @Override
      public void onReceive(Context context, Intent intent) {
      if(intent.getAction()=="sk.tuke.smart.makac.TICK"){
      duration = intent.getLongExtra("duration",duration);
      distance = intent.getDoubleExtra("distance",distance);
      durationView.setText(helper.formatDuration(duration));
      distanceView.setText(helper.formatDistance(distance));
      }

      }
      };

      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_stopwatch);
      IntentFilter intentFilter = new IntentFilter("sk.tuke.smart.makac.TICK");
      registerReceiver(receiver,intentFilter);
      started=false;
      running=false;
      helper = new MainHelper();
      durationView = findViewById(R.id.textview_stopwatch_duration);
      distanceView = findViewById(R.id.textview_stopwatch_distance);
      startButton = findViewById(R.id.button_stopwatch_start);
      endButton = findViewById(R.id.button_stopwatch_endworkout);
      if(!canAccessLocation()){
      ActivityCompat.requestPermissions(this,
      new String{
      Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},
      MY_PERMISSIONS_REQUEST_FINE_LOCATION);

      }
      }


      public void toggle(View view){

      Intent intent = new Intent(this,TrackerService.class);
      started=true;
      this.running = !this.running;
      if(running && started){
      startButton.setText("Stop");
      endButton.setVisibility(view.GONE);
      if(paused){
      intent.setAction("sk.tuke.smart.makac.COMMAND_CONTINUE");
      paused=false;
      }else
      intent.setAction("sk.tuke.smart.makac.COMMAND_START");

      }

      if(!running && started){
      startButton.setText("Continue");
      endButton.setVisibility(view.VISIBLE);
      intent.setAction("sk.tuke.smart.makac.COMMAND_PAUSE");
      paused=true;
      }


      startService(intent);


      }

      public void endWorkout(View view){
      Intent intent = new Intent(this,TrackerService.class);
      intent.setAction("sk.tuke.smart.makac.COMMAND_STOP");
      startService(intent);
      setContentView(R.layout.activity_workout_detail);
      onStop();
      }


      public void openMaps(View view){
      Intent intent = new Intent(StopwatchActivity.this, MapsActivity.class);
      startActivity(intent);
      }


      @Override
      protected void onStart() {
      super.onStart();
      }

      @Override
      protected void onStop() {
      super.onStop();
      Intent intent= new Intent(this,TrackerService.class);
      stopService(intent);
      }


      private boolean hasPermission(String perm) {
      return(PackageManager.PERMISSION_GRANTED==checkSelfPermission(perm));
      }

      private boolean canAccessLocation() {
      return(hasPermission(Manifest.permission.ACCESS_FINE_LOCATION));
      }
      }









      share|improve this question














      I have a problem with an activity after minimalising. Everything is going ok when i start an activity and press start button. But when i minimalise activity and again maximalize it, it doesnt respond to my buttons and commands. Anybody know what to do? This is my first android app so i dont know what is going on..



      here are my classes :




      TrackerService




      package sk.tuke.smart.makac.services;

      import android.app.Service;
      import android.content.Intent;
      import android.location.Location;
      import android.location.LocationListener;
      import android.location.LocationManager;
      import android.os.Bundle;
      import android.os.IBinder;
      import android.util.Log;

      import java.util.ArrayList;
      import java.util.Timer;
      import java.util.TimerTask;


      public class TrackerService extends Service implements LocationListener {

      private Intent commandIntent;
      private long duration;
      private boolean paused,checkedAfterPause;
      private int sportActivity;
      private double distance,pace,calories;
      private ArrayList<Location> finalPositionList = new ArrayList<Location>();
      private LocationManager locationManager;




      private static final String TAG = "TrackerService";



      @Override
      public void onStart(Intent intent, int startId) {
      super.onStart(intent, startId);
      Log.i(TAG,"onStart intent " +intent.getAction());
      commandIntent=intent;
      checkedAfterPause=true;
      if(intent.getAction() == "sk.tuke.smart.makac.COMMAND_START"){
      if(intent.getAction() == "sk.tuke.smart.makac.COMMAND_START") {
      duration = 0;
      }
      new Timer().scheduleAtFixedRate(new TimerTask()
      {

      @Override
      public void run() {
      if(!paused){
      duration++;
      Intent intent1 = new Intent();
      intent1.setAction("sk.tuke.smart.makac.TICK");
      intent1.putExtra("duration", duration);
      intent1.putExtra("distance",distance);
      sendBroadcast(intent1);
      Log.i(TAG,"" + duration);
      }
      }
      }, 1000, 1000);
      }else if (intent.getAction() == "sk.tuke.smart.makac.COMMAND_PAUSE"){
      paused=true;
      locationManager.removeUpdates(this);
      }
      if(intent.getAction() == "sk.tuke.smart.makac.COMMAND_CONTINUE"){
      paused=false;
      checkedAfterPause=false;
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,10,this);
      }

      }


      @Override
      public void onCreate() {

      super.onCreate();
      locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,10,this);

      }

      @Override
      public void onDestroy() {
      locationManager.removeUpdates(this);
      }



      @Override
      public IBinder onBind(Intent intent) {


      return null;

      }

      @Override
      public void onLocationChanged(Location location) {
      finalPositionList.add(location);
      Location lastLocation;
      double minDistance;
      if (finalPositionList.size() != 1 && checkedAfterPause) {
      lastLocation = finalPositionList.get(finalPositionList.size() - 2);
      minDistance=location.distanceTo(lastLocation);
      if(minDistance>=2){
      distance += location.distanceTo(lastLocation);
      }else{
      finalPositionList.remove(finalPositionList.size()-1);
      }
      }
      if(commandIntent.getAction() == "sk.tuke.smart.makac.COMMAND_CONTINUE" && !checkedAfterPause){
      Log.i(TAG,"checking distance after pause");
      lastLocation = finalPositionList.get(finalPositionList.size() - 2);
      minDistance=location.distanceTo(lastLocation);
      if(minDistance<=100){
      distance += location.distanceTo(lastLocation);
      }
      checkedAfterPause=true;
      }
      Log.i(TAG,"locations " + finalPositionList);
      Log.i(TAG,"distance = " + distance);

      }

      @Override
      public void onStatusChanged(String s, int i, Bundle bundle) {

      }

      @Override
      public void onProviderEnabled(String s) {

      }

      @Override
      public void onProviderDisabled(String s) {

      }

      public long getDuration(){
      return duration;
      }



      public double getPace(){
      return pace;
      }




      }


      SportsActivity







      package sk.tuke.smart.makac;



      import android.Manifest;
      import android.content.BroadcastReceiver;
      import android.content.Context;
      import android.content.Intent;
      import android.content.IntentFilter;
      import android.content.pm.PackageManager;
      import android.support.v4.app.ActivityCompat;
      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.Button;
      import android.widget.TextView;

      import sk.tuke.smart.makac.helpers.MainHelper;
      import sk.tuke.smart.makac.services.TrackerService;

      public class StopwatchActivity extends AppCompatActivity {

      private static final int MY_PERMISSIONS_REQUEST_FINE_LOCATION = 111;
      private static final String TAG = "StopwatchActivity";
      private boolean started;
      private boolean running;
      private boolean paused=false;
      private long duration;
      private double distance;
      private MainHelper helper;
      private TextView durationView,distanceView;
      private Button startButton,endButton;

      private BroadcastReceiver receiver = new BroadcastReceiver() {

      @Override
      public void onReceive(Context context, Intent intent) {
      if(intent.getAction()=="sk.tuke.smart.makac.TICK"){
      duration = intent.getLongExtra("duration",duration);
      distance = intent.getDoubleExtra("distance",distance);
      durationView.setText(helper.formatDuration(duration));
      distanceView.setText(helper.formatDistance(distance));
      }

      }
      };

      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_stopwatch);
      IntentFilter intentFilter = new IntentFilter("sk.tuke.smart.makac.TICK");
      registerReceiver(receiver,intentFilter);
      started=false;
      running=false;
      helper = new MainHelper();
      durationView = findViewById(R.id.textview_stopwatch_duration);
      distanceView = findViewById(R.id.textview_stopwatch_distance);
      startButton = findViewById(R.id.button_stopwatch_start);
      endButton = findViewById(R.id.button_stopwatch_endworkout);
      if(!canAccessLocation()){
      ActivityCompat.requestPermissions(this,
      new String{
      Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION},
      MY_PERMISSIONS_REQUEST_FINE_LOCATION);

      }
      }


      public void toggle(View view){

      Intent intent = new Intent(this,TrackerService.class);
      started=true;
      this.running = !this.running;
      if(running && started){
      startButton.setText("Stop");
      endButton.setVisibility(view.GONE);
      if(paused){
      intent.setAction("sk.tuke.smart.makac.COMMAND_CONTINUE");
      paused=false;
      }else
      intent.setAction("sk.tuke.smart.makac.COMMAND_START");

      }

      if(!running && started){
      startButton.setText("Continue");
      endButton.setVisibility(view.VISIBLE);
      intent.setAction("sk.tuke.smart.makac.COMMAND_PAUSE");
      paused=true;
      }


      startService(intent);


      }

      public void endWorkout(View view){
      Intent intent = new Intent(this,TrackerService.class);
      intent.setAction("sk.tuke.smart.makac.COMMAND_STOP");
      startService(intent);
      setContentView(R.layout.activity_workout_detail);
      onStop();
      }


      public void openMaps(View view){
      Intent intent = new Intent(StopwatchActivity.this, MapsActivity.class);
      startActivity(intent);
      }


      @Override
      protected void onStart() {
      super.onStart();
      }

      @Override
      protected void onStop() {
      super.onStop();
      Intent intent= new Intent(this,TrackerService.class);
      stopService(intent);
      }


      private boolean hasPermission(String perm) {
      return(PackageManager.PERMISSION_GRANTED==checkSelfPermission(perm));
      }

      private boolean canAccessLocation() {
      return(hasPermission(Manifest.permission.ACCESS_FINE_LOCATION));
      }
      }






      android






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 18:50









      Christián SzemanChristián Szeman

      286




      286
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You aren't creating a connection to the service. You're only starting it, not binding it. So there's no connection to maintain.



          It looks like you're trying to do quasi-binding via actions. Don't do that, properly bind the service and avoid a whole raft of problems like this.






          share|improve this answer
























          • Yes thank you, i bound services and its working correctly now. Now i have new problem. After i hit back button, onDestroy is called and i want to save state of my activity and then i want to refresh it again with onResume. Can you help me with this? Basically i want same functionality in onDestroy as onPause method. When i call super.onPause in onDestory , it doesnt work.

            – Christián Szeman
            Nov 15 '18 at 22:52













          • Put the functionality to save state in a helper function, and call the helper in both places.

            – Gabe Sechan
            Nov 15 '18 at 23:52











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326122%2factivity-doesnt-mantain-connection-to-its-service-after-minimalising-it%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You aren't creating a connection to the service. You're only starting it, not binding it. So there's no connection to maintain.



          It looks like you're trying to do quasi-binding via actions. Don't do that, properly bind the service and avoid a whole raft of problems like this.






          share|improve this answer
























          • Yes thank you, i bound services and its working correctly now. Now i have new problem. After i hit back button, onDestroy is called and i want to save state of my activity and then i want to refresh it again with onResume. Can you help me with this? Basically i want same functionality in onDestroy as onPause method. When i call super.onPause in onDestory , it doesnt work.

            – Christián Szeman
            Nov 15 '18 at 22:52













          • Put the functionality to save state in a helper function, and call the helper in both places.

            – Gabe Sechan
            Nov 15 '18 at 23:52
















          0














          You aren't creating a connection to the service. You're only starting it, not binding it. So there's no connection to maintain.



          It looks like you're trying to do quasi-binding via actions. Don't do that, properly bind the service and avoid a whole raft of problems like this.






          share|improve this answer
























          • Yes thank you, i bound services and its working correctly now. Now i have new problem. After i hit back button, onDestroy is called and i want to save state of my activity and then i want to refresh it again with onResume. Can you help me with this? Basically i want same functionality in onDestroy as onPause method. When i call super.onPause in onDestory , it doesnt work.

            – Christián Szeman
            Nov 15 '18 at 22:52













          • Put the functionality to save state in a helper function, and call the helper in both places.

            – Gabe Sechan
            Nov 15 '18 at 23:52














          0












          0








          0







          You aren't creating a connection to the service. You're only starting it, not binding it. So there's no connection to maintain.



          It looks like you're trying to do quasi-binding via actions. Don't do that, properly bind the service and avoid a whole raft of problems like this.






          share|improve this answer













          You aren't creating a connection to the service. You're only starting it, not binding it. So there's no connection to maintain.



          It looks like you're trying to do quasi-binding via actions. Don't do that, properly bind the service and avoid a whole raft of problems like this.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 18:54









          Gabe SechanGabe Sechan

          68k965100




          68k965100













          • Yes thank you, i bound services and its working correctly now. Now i have new problem. After i hit back button, onDestroy is called and i want to save state of my activity and then i want to refresh it again with onResume. Can you help me with this? Basically i want same functionality in onDestroy as onPause method. When i call super.onPause in onDestory , it doesnt work.

            – Christián Szeman
            Nov 15 '18 at 22:52













          • Put the functionality to save state in a helper function, and call the helper in both places.

            – Gabe Sechan
            Nov 15 '18 at 23:52



















          • Yes thank you, i bound services and its working correctly now. Now i have new problem. After i hit back button, onDestroy is called and i want to save state of my activity and then i want to refresh it again with onResume. Can you help me with this? Basically i want same functionality in onDestroy as onPause method. When i call super.onPause in onDestory , it doesnt work.

            – Christián Szeman
            Nov 15 '18 at 22:52













          • Put the functionality to save state in a helper function, and call the helper in both places.

            – Gabe Sechan
            Nov 15 '18 at 23:52

















          Yes thank you, i bound services and its working correctly now. Now i have new problem. After i hit back button, onDestroy is called and i want to save state of my activity and then i want to refresh it again with onResume. Can you help me with this? Basically i want same functionality in onDestroy as onPause method. When i call super.onPause in onDestory , it doesnt work.

          – Christián Szeman
          Nov 15 '18 at 22:52







          Yes thank you, i bound services and its working correctly now. Now i have new problem. After i hit back button, onDestroy is called and i want to save state of my activity and then i want to refresh it again with onResume. Can you help me with this? Basically i want same functionality in onDestroy as onPause method. When i call super.onPause in onDestory , it doesnt work.

          – Christián Szeman
          Nov 15 '18 at 22:52















          Put the functionality to save state in a helper function, and call the helper in both places.

          – Gabe Sechan
          Nov 15 '18 at 23:52





          Put the functionality to save state in a helper function, and call the helper in both places.

          – Gabe Sechan
          Nov 15 '18 at 23:52




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326122%2factivity-doesnt-mantain-connection-to-its-service-after-minimalising-it%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Bressuire

          Vorschmack

          Quarantine