GPS 停止,onPause()
在我的应用程序中,当我开始使用 GPS 时,我无法停止它,无论我按后退按钮还是主页按钮,Toast 都会出现(我在 requestLocationUpdate 调用中放入 0, 0 来查看它是否会这样做)
它会出现onPause 方法(当我按下后退/主页按钮进入时)removeUpdates 调用不会执行任何操作。还是其他什么问题?请帮助我,因为在过去的三天里我一直被这个问题困扰。应用程序的逻辑:我有一个按钮,按下它然后检查哪个提供程序处于活动状态 WhatIsEnabled() 方法
我的代码是:
public class Map extends MapActivity {
private MapView mapView;
private MapController mc;
private GeoPoint p;
private Context context;
private Drawable drawable;
private Bundle instanceState;
private EditText et;
private ImageButton connect, layers, location;
private int i = 1;
//private Settings settings;
private LocationManager locationManager;
private LocationListener locationListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instanceState = savedInstanceState;
setContentView(R.layout.main);
context = getApplicationContext();
//settings = new Settings();
//to zoom
mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
et = (EditText) findViewById(R.id.address);
connect = (ImageButton) findViewById(R.id.connect_icon);
connect.setOnClickListener(connect_button);
layers = (ImageButton) findViewById(R.id.layers);
layers.setOnClickListener(layer_button);
//locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//locationListener = new Coordonates(drawable, mapView, Map.this);
location = (ImageButton) findViewById(R.id.location_icon);
location.setOnClickListener(location_button);
//markers
drawable = this.getResources().getDrawable(R.drawable.blue_pin);
}
private OnClickListener connect_button = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Map.this, Connection.class);
Map.this.startActivity(myIntent);
}
};
private OnClickListener layer_button = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(i%2 != 0) {
mapView.setStreetView(true);
mapView.setSatellite(false);
}
else {
mapView.setSatellite(true);
mapView.setStreetView(false);
}
i++;
}
};
private OnClickListener location_button = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//registerLocListener();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new Coordonates(drawable, mapView, Map.this);
switch(Settings.whatIsEnabled()) {
case 1: {
Toast.makeText(context, "Location services disabled",Toast.LENGTH_LONG).show();
break;
}
case 2: {
Toast.makeText(context, "Location obtained via GPS satellites",Toast.LENGTH_LONG).show();
Toast.makeText(context, "Waiting for location",Toast.LENGTH_LONG).show();
System.out.println("1");
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListener);//50
if(Coordonates.getLatitude() == 0)
Toast.makeText(context, "No GPS signal",Toast.LENGTH_LONG).show();
System.out.println("2");
break;
}
case 3: {
Toast.makeText(context, "Location obtained via Wi-Fi/mobile network",Toast.LENGTH_LONG).show();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);//1000
if(Coordonates.getLatitude() == 0)
Toast.makeText(context, "No Wi-Fi/mobile network signal",Toast.LENGTH_LONG).show();
break;
}/*
case 4: {
Toast.makeText(context, "Location obtained via the best provider available",Toast.LENGTH_LONG).show();
locationManager.
break;
}*/
}
}
};
/*
protected void onResume() {
super.onResume();
}*/
@Override
protected void onPause() {
// TODO Auto-generated method stub
System.out.println("a intrat in onPause()");
if(locationManager != null) {
locationManager.removeUpdates(locationListener);
}
locationListener = null;
locationManager = null;
super.onPause();
}
In my app when I start using GPS I can’t stop it, it doesn’t matter if I push the back or home button the Toast appears (I put 0, 0 at the requestLocationUpdate call to see if it does that)
It apears that the onPause method (in which when I push the back/home button enters) the removeUpdates call doesn’t do enything. Or something else is the problem? please help me cause for the last 3 days I’m stuck with this problem. The logic of the app: I have a button which I push then it checks which provider is active whatIsEnabled() method
My code is:
public class Map extends MapActivity {
private MapView mapView;
private MapController mc;
private GeoPoint p;
private Context context;
private Drawable drawable;
private Bundle instanceState;
private EditText et;
private ImageButton connect, layers, location;
private int i = 1;
//private Settings settings;
private LocationManager locationManager;
private LocationListener locationListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instanceState = savedInstanceState;
setContentView(R.layout.main);
context = getApplicationContext();
//settings = new Settings();
//to zoom
mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
et = (EditText) findViewById(R.id.address);
connect = (ImageButton) findViewById(R.id.connect_icon);
connect.setOnClickListener(connect_button);
layers = (ImageButton) findViewById(R.id.layers);
layers.setOnClickListener(layer_button);
//locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//locationListener = new Coordonates(drawable, mapView, Map.this);
location = (ImageButton) findViewById(R.id.location_icon);
location.setOnClickListener(location_button);
//markers
drawable = this.getResources().getDrawable(R.drawable.blue_pin);
}
private OnClickListener connect_button = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(Map.this, Connection.class);
Map.this.startActivity(myIntent);
}
};
private OnClickListener layer_button = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(i%2 != 0) {
mapView.setStreetView(true);
mapView.setSatellite(false);
}
else {
mapView.setSatellite(true);
mapView.setStreetView(false);
}
i++;
}
};
private OnClickListener location_button = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//registerLocListener();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new Coordonates(drawable, mapView, Map.this);
switch(Settings.whatIsEnabled()) {
case 1: {
Toast.makeText(context, "Location services disabled",Toast.LENGTH_LONG).show();
break;
}
case 2: {
Toast.makeText(context, "Location obtained via GPS satellites",Toast.LENGTH_LONG).show();
Toast.makeText(context, "Waiting for location",Toast.LENGTH_LONG).show();
System.out.println("1");
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListener);//50
if(Coordonates.getLatitude() == 0)
Toast.makeText(context, "No GPS signal",Toast.LENGTH_LONG).show();
System.out.println("2");
break;
}
case 3: {
Toast.makeText(context, "Location obtained via Wi-Fi/mobile network",Toast.LENGTH_LONG).show();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);//1000
if(Coordonates.getLatitude() == 0)
Toast.makeText(context, "No Wi-Fi/mobile network signal",Toast.LENGTH_LONG).show();
break;
}/*
case 4: {
Toast.makeText(context, "Location obtained via the best provider available",Toast.LENGTH_LONG).show();
locationManager.
break;
}*/
}
}
};
/*
protected void onResume() {
super.onResume();
}*/
@Override
protected void onPause() {
// TODO Auto-generated method stub
System.out.println("a intrat in onPause()");
if(locationManager != null) {
locationManager.removeUpdates(locationListener);
}
locationListener = null;
locationManager = null;
super.onPause();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我开发了一个类似的应用程序,但在获取位置后,我将
locationManager.removeUpdates(locationListener);
放在 onCreate() 方法中,而不是放在 onPause() 中。将其放入 onPause() 的问题是,当您离开(暂停)活动时,它将被调用。由于我没有跟踪用户的移动,因此一旦获得监听器的位置,我就会停止监听器。
当您正在开发与我不同的功能的应用程序时,这可能没有用。
祝你好运!
I developed a similar application but I put the
locationManager.removeUpdates(locationListener);
in the onCreate() method after I get location rather in the onPause(). The problem with putting it in onPause() is that it will be called when you leave (pause) the activity. Since I am not tracking the user's movement, I stop the listener once I get their location.
This might not be useful you are developing the application for a different functionality than I did.
Good Luck!