【发布时间】:2015-10-27 18:29:03
【问题描述】:
谁能解释一下为什么我的代码不起作用? 我试图让 gps 尽可能简单。调试器没有发现任何错误,但是当我运行它时,它就不起作用了。我可以获得启用 GPS 和禁用 GPS 的消息,但不能获得 GPS 纬度和经度..
代码如下:
public class getgps extends AppCompatActivity {
double longtitude;
double latitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_getgps);
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, mlocListener);
}
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
latitude = loc.getLatitude();
longtitude = loc.getLongitude();
String Text = "My current location is: " +
"Latitude = " + latitude +
"Longitude = " + longtitude;
Toast.makeText( getApplicationContext(),
Text,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),
"Gps Disabled",
Toast.LENGTH_SHORT ).show();
}
@Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),
"GPS Enabled",
Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Toast.makeText( getApplicationContext(), + latitude +" ",
Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_getgps, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
感谢您的想法。 ^_-
【问题讨论】:
-
尝试将
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, mlocListener);更改为mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);..同时确保您应该首次在开放天空中测试应用程序。 -
我也尝试了 0, 0 但它不起作用。现在我按照你告诉我的那样在开阔的天空中尝试了它^^并且它起作用了。但它很有趣,因为例如谷歌地图或 waze 即使在家里也没有问题。谢谢
-
哦,太好了..我将其添加为我的答案..如果它真的对您有帮助,请您将答案标记为已接受..