我是JAVA的新手,我必须创建一个应用程序,绘制我在哪里(通过GPS找到)和一些预先指定的目的地之间的路线。我按照http://androidexample.com/GPS_Basic__-__Android_Example/index.php?view=article_discription&aid=68&aaid=93等在线教程学习如何阅读我自己的位置。
我希望我的应用在打开和关闭GPS时采取不同的行动。为了读取GPS状态,我将LocationManager对象用作以下行:
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
我可以通过右键单击GPS选项卡手动关闭Genymortion仿真器中的GPS。下图显示了我打开GPS的方式。
然而,即使我改变了GPS状态,我的应用程序总是说始终是GPSEnabled = true或者始终是isGPSEnabled = false。谁能告诉我我失踪了什么?如果有帮助的话,我在下面附上我的大部分代码。
public class MapDisplayActivity extends Activity implements LocationListener {
private MapDisplayFragment fragment;
private DrawerLayout sideDrawerMenu;
private ListView sideDrawerMenuList;
private LocationManager locationManager;
private boolean isGPSEnabled;
private final static String LOG_TAG = "MapDisplayActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.side_drawer_menu);
// some codes come here...
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (isGPSEnabled) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
5000, // 5 sec
10, this);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
@Override
public void onLocationChanged(Location location) {
if (isGPSEnabled) {
String str = "Latitude: " +
location.getLatitude()
+ "Longitude: " + location.getLongitude();
Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();
}else{
gpsLatlon = null;
}
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(getBaseContext(), "Gps turned off ", Toast.LENGTH_LONG).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getBaseContext(), "Gps turned on ", Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
// ~~~~~~~~~~ to imeplement LocationListener ~~~~~~~~~~~~~~
private class SideDrawerClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
selectItem(position);
}
}
// some more methods definitions....
}
答案 0 :(得分:0)
看起来你只需要在回调中设置isGPSEnabled
标志:
@Override
public void onProviderDisabled(String provider) {
if(provider.equals(LocationManager.GPS_PROVIDER)){
isGPSEnabled = false;
}
Toast.makeText(getBaseContext(), "Gps turned off ", Toast.LENGTH_LONG).show();
}
@Override
public void onProviderEnabled(String provider) {
if(provider.equals(LocationManager.GPS_PROVIDER)){
isGPSEnabled = true;
}
Toast.makeText(getBaseContext(), "Gps turned on ", Toast.LENGTH_LONG).show();
}
需要注意的一点是,如果您当前将Activity注册为位置侦听器,则只会调用这些回调。
因此,正如您现在所拥有的那样,如果活动以GPS开启,则会调用onProviderDisabled()
回调,然后在活动仍在运行时禁用GPS。
一种方法是在禁用GPS时注册NETWORK_PROVIDER
回调。
通过这种方式,您仍然可以注册位置监听器,并且您将获得GPS的onProviderEnabled()
回调。此时,您可以取消注册NETWORK回调并注册GPS回调。