I cannot get mlocManager.removeUpdates(mlocListener); to work
I have an app that uses the LocationManage functions which works well until the app is stopped or paused. The location listener function is still carrying on in the background. Relevant bits of code follow. When I click home or back the onstop() function is being triggered correctly.
package uk.cr.anchor;
import android.app.Activity;
impor开发者_如何学编程t android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TableRow;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.content.SharedPreferences;
import android.graphics.Color;
public class main extends Activity {
/** Called when the activity is first created. */
private LocationManager mlocManager;
private LocationListener mlocListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
@Override
protected void onStop(){
stoplistening();
super.onStop();
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
etc etc etc
}
private void stoplistening() {
if (mlocManager != null) {
Toast.makeText( getApplicationContext(),
"kill",
Toast.LENGTH_SHORT ).show();
mlocManager.removeUpdates(mlocListener);
}
else {
Toast.makeText( getApplicationContext(),
" not kill",
Toast.LENGTH_SHORT ).show();
}
}
}
I always get the "not kill" message.
Can anyone help me!
In the code you gave, you're declaring module-level variables at the top of your class, and you want them to be set to a location manager and listener. In onCreate, however, you're declaring new private variables that just happen to have the same name as the module-level variables. Those variables are taken off the stack once onCreate is done executing, so you're left with your module-level variables being null, because they were never initialized. Seth's change will fix the problem.
Change this
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
To This
mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
Sorry don't have any idea why it is this way, but it works.
This is a scoping problem. In your onCreate function you are declaring new variables mlocManager and mlocListener that live within this function only and can't be seen anywhere else. Do not put the type in front of the parameters you declared already because then you are effectively hiding your class variables. The way you did it now the call to removeUpdates(mLocListener) is being called on a different object than the one you are using in onCreate.
I can get round this by changing
private LocationManager mlocManager;
private LocationListener mlocListener;
to
private LM mlocManager;
private LL mlocListener;
and adding
LM = mlocManager;
LL = mlocListener;
Then changing all later references to mlocManager
to LM
and al
references or mlocListener
to LL
.
Although this works it seems very clumsy. I am sure there is a better way?
精彩评论