onPause停止LocationManager

4 android locationmanager

我想我做得对吗?

我有这个代码开始通过这里没有显示的MyLocationListener方法查找我的GPS位置,但是我想停止locationManager onPause,我想或者当这个活动不是最新的时候,但是我无法获得removeUpdates代码解决.

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);        
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINIMUM_TIME_BETWEEN_UPDATES, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new MyLocationListener());
Run Code Online (Sandbox Code Playgroud)

然后,

@Override 
public void onPause()
{
    super.onPause();
    locationManager.removeUpdates(MyLocationListener);
}
Run Code Online (Sandbox Code Playgroud)

"MyLocationListener"不会解决,我也试过"这个",

locationManager.removeUpdates((LocationListener) this);
Run Code Online (Sandbox Code Playgroud)

哪个解析但在运行时给我一个"无法暂停"错误.

Sta*_*ton 8

我有一个类似的问题: 让我的GPS传感器停下来有些麻烦

您可能需要定义一个LocationListener,它对于启动的那个和结束的那个是相同的.

尝试:

LocationListener mlocListener; 
Run Code Online (Sandbox Code Playgroud)

在类名下,然后在onCreate方法中使用以下内容:

mlocListener = new MyLocationListener();
Run Code Online (Sandbox Code Playgroud)

并为整个班级使用上面的mlocListener.

所以让你的课看起来像这样:

public class SomeClass extends Activity {
    LocationManager mlocManager;
    LocationListener mlocListener; 

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screenlayout);
    mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    mlocListener = new MyLocationListener();
    mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}

@Override
public void onPause(){
    mlocManager.removeUpdates(mlocListener);
    super.onPause();
} 
Run Code Online (Sandbox Code Playgroud)