最小化活动后,它不会维持与其服务的连接

时间:2018-11-15 18:50:55

标签: android

最小化后,我的活动有问题。当我开始一项活动并按“开始”按钮时,一切正常。但是,当我最小化活动并再次将其最大化时,它不会响应我的按钮和命令。有人知道该怎么办吗?这是我的第一个android应用程序,所以我不知道发生了什么..

这是我的课程:

  

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));
    }
}

1 个答案:

答案 0 :(得分:0)

您没有创建与服务的连接。您只是在启动它,而没有绑定它。因此,无需维护任何连接。

您似乎正在尝试通过动作进行准绑定。不要这样做,正确地绑定服务,避免出现大量此类问题。