我有一个应用程序,使用位置管理功能,它运行良好,直到应用程序停止或暂停。位置侦听器功能仍在后台进行。下面是相关的代码。当我单击home或back时,正在正确触发onstop()函数。我无法获取mlocManager.removeUpdates(mlocListener);工作
package uk.cr.anchor;
import android.app.Activity;
import 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();
}
}
}
我总是得到“不杀”消息。
任何人都可以帮助我!
在你的onCreate函数中,你声明了一个新的变量mlocManager和mlocListener。不要将该类型放在参数前面,因为您没有使用类中声明的类型(从onCreate开始)。 – 2012-10-16 16:54:57