如何停止并重新启动Android位置服务

时间:2016-02-04 13:30:32

标签: java android service location broadcastreceiver

我在MainActivity中实现了以下服务,我想通过Buttonclick启动并重新启动Service。但是当我点击停止时,Service仍会发送Location updates!当我按下停止按钮时,如何立即停止Service,我的错是什么?

服务类:

public class LocationNotifyService extends Service implements
        LocationListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {

    final static String MY_ACTION = "MY_ACTION";

    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    public static Location mCurrentLocation;
    private static final long INTERVAL = 1000 * 10;
    private static final long FASTEST_INTERVAL = 1000 * 5;

    private static final int TWO_MINUTES = 1000 * 60 * 2;


    public static final String MOVEMENT_UPDATE = "com.client.gaitlink.AccelerationService.action.MOVEMENT_UPDATE";
    public static final String ACCELERATION_X = "com.client.gaitlink.AccelerationService.ACCELERATION_X";
    public static final String ACCELERATION_Y = "com.client.gaitlink.AccelerationService.ACCELERATION_Y";
    public static final String ACCELERATION_Z = "com.client.gaitlink.AccelerationService.ACCELERATION_Z";
    public static final String ACCELERATION_PACE = "com.client.gaitlink.AccelerationService.ACCELERATION_PACE";
    public static final String ACCELERATION_TIME = "com.client.gaitlink.AccelerationService.ACCELERATION_TIME";



    @Override
    public void onCreate()
    {

        Log.d("create", "create");

        //show error dialog if GoolglePlayServices not available
        if (isGooglePlayServicesAvailable()) {
            mLocationRequest = new LocationRequest();
            mLocationRequest.setInterval(INTERVAL);
            mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            mLocationRequest.setSmallestDisplacement(5);  /* min dist for location change, here it is 10 meter */
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();

            mGoogleApiClient.connect();
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    //Check Google play is available or not
    private boolean isGooglePlayServicesAvailable() {
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
        return ConnectionResult.SUCCESS == status;
    }


    @Override
    public void onConnected(Bundle bundle) {
        startLocationUpdates();
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    protected void startLocationUpdates()
    {
        Log.d("start","start");

        try {
            PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        } catch (IllegalStateException e) {}
    }

    @Override
    public void onLocationChanged(Location location)
    {



        Log.d("changed", "changed");

        Intent intent = new Intent(MOVEMENT_UPDATE);
        intent.setAction(MY_ACTION);
        intent.putExtra(ACCELERATION_X, location.getLongitude());
        intent.putExtra(ACCELERATION_Y, location.getLatitude());
        intent.putExtra(ACCELERATION_Z, location.getAltitude());
        intent.putExtra(ACCELERATION_PACE, location.getSpeed());
        intent.putExtra(ACCELERATION_TIME, location.getTime());

        sendBroadcast(intent);

        Log.d("send","send");
    }


    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

    public boolean stopService(Intent name)
    {
        // TODO Auto-generated method stub
        mGoogleApiClient.disconnect();

        return super.stopService(name);

    }

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


}

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    Button start;
    Button stop;
    TextView xyz;

    MyReceiver myReceiver;

    double accelerationX;
    double accelerationY;
    double accelerationZ;
    float pace;
    List<Double> pace_list = new ArrayList<Double>();


    Location oldLoc = new Location("locationOld");
    Location newLoc = new Location("locationNew");

    double distance = 0;
    long time = 0;

    String temp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        start = (Button) findViewById(R.id.start);
        stop = (Button) findViewById(R.id.stop);

        xyz = (TextView) findViewById(R.id.xyz);

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Log.d("click", "click");

                myReceiver = new MyReceiver();
                IntentFilter intentFilter = new IntentFilter();
                intentFilter.addAction(LocationNotifyService.MY_ACTION);
                registerReceiver(myReceiver, intentFilter);


                Intent intent = new Intent(MainActivity.this,
                        LocationNotifyService.class);
                startService(intent);

                //startService(new Intent(MainActivity.this, LocationNotifyService.class));
            }
        });

        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(MainActivity.this,
                        LocationNotifyService.class);
                stopService(intent);
                Log.d("stop", "stop");
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();


        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub

            Log.d("revcive", "recive");


            accelerationX = intent.getDoubleExtra(LocationNotifyService.ACCELERATION_X, 0);
            accelerationY = intent.getDoubleExtra(LocationNotifyService.ACCELERATION_Y, 0);
            accelerationZ = intent.getDoubleExtra(LocationNotifyService.ACCELERATION_Z, 0);
            pace = intent.getFloatExtra(LocationNotifyService.ACCELERATION_PACE, 0);
            pace = (float) (pace * 3.6);
            time = intent.getLongExtra(LocationNotifyService.ACCELERATION_TIME, 0);

            pace_list.add((double) pace);


            SimpleDateFormat s = new SimpleDateFormat("yyyyMMddhhmmss");
            String format = s.format(new Date());


            temp += format + " | " + accelerationX + " | " + accelerationY + " | " + accelerationZ + " | " + Collections.max(pace_list) + "\n";

            xyz.setText(temp);


        }
    }

}

的Manifest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar"
        android:configChanges="orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <action android:name="com.client.gaitlink.CommunicationService.action.ACTIVITY_STATUS_UPDATE" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name=".LocationNotifyService"
        android:exported="false"/>


</application>

1 个答案:

答案 0 :(得分:0)

我猜你忘了在stopService()

中添加以下行
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

将其添加到stopService -

**// Missing @Override**
@Override    
public boolean stopService(Intent name) {

    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    return super.stopService(name);

}

修改MainActivity.java,如下所示 -

public class MainActivity extends AppCompatActivity {

    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

    intent = new Intent(MainActivity.this,
                    LocationNotifyService.class);

    start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        ...
        startService(intent);

        }
    });


    stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        stopService(intent);
       }
    });

} // end of class MainActivity

您需要对来自stop.setOnClickListenerstart.setOnClickListener -

的代码进行评论
Intent intent = new Intent(MainActivity.this, LocationNotifyService.class);

*您必须使用相同的意图来启动服务以停止服务。