我是JAVA的新手,我必須創建一個應用程序來繪製我(通過GPS找到的)和某個預先指定的目的地之間的路線。我遵循在線教程,如http://androidexample.com/GPS_Basic__-__Android_Example/index.php?view=article_discription&aid=68&aaid=93來了解如何閱讀我自己的位置。locationManager.isProviderEnabled與Genymortion仿真器不兼容
我希望我的應用在GPS打開和關閉時的行爲不同。讀取的GPS狀態我使用的LocationManager對象,如下行:
boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
我可以手動通過右擊GPS標籤打開GPS關閉在Genymortion模擬器。下面的圖片顯示了我如何打開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....
}
我在這些方法上添加了if語句,但isGPSEnabled = true;當它應該是false ... – ThePrincess 2015-03-31 06:14:20
@ThePrincess,當你啓用和禁用GPS時,你是否看到Toast消息? – 2015-03-31 06:15:55
我看到isGPSEnabled = true當我啓用和禁用GPS – ThePrincess 2015-03-31 21:04:41