【发布时间】:2014-08-23 08:50:47
【问题描述】:
我的问题如下。 我想开发一个非常简单的 android 应用程序,它可以从 GSM/3G/LTS/等获取手机当前位置。网络。我知道有很多类似的问题,但对于我的问题我找不到任何答案。
所以我编写了这段代码,它运行良好,但是当我搬到我所在城市的另一个位置并再次获得我的坐标时,它们与我之前的位置相同。冷你帮我,不打电话,短信等怎么刷新。
(PS:我在Button Click事件中调用了_getLocation()方法)
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.toggleButton1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
_getLocation();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
double lat = 0;
double lon = 0;
LocationListener asd = new MyLocationListener();
Location loc;
private void _getLocation()
{
try
{
Criteria criteria = new Criteria();
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
String bestProvider = locationManager.getBestProvider(criteria, false);
//Location location = locationManager.getLastKnownLocation(bestProvider);
LocationManager fasz = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
fasz.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,10,0,this.asd);
//Location asdasd = fasz.getLastKnownLocation(bestProvider);
Location asdasd=null;
MyLocationListener kuki = new MyLocationListener();
kuki.onLocationChanged(asdasd);
Toast.makeText(this, "Long: " + String.valueOf(asdasd.getLongitude()) + " Lat: " + String.valueOf(asdasd.getLatitude()), Toast.LENGTH_LONG).show();
//lat = location.getLatitude();
//lon = location.getLongitude();
} catch (Exception ex) {
Toast.makeText(this, ex.toString(), Toast.LENGTH_LONG).show();
// lat = -1.0;
// lon = -1.0;
}
}
final class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location locFromGps) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
其他错误:
我需要从 - onLocationChanged 中删除 @Override 文本 - onProviderDisabled - onProviderEnabled - onStatusChanged 方法。
我从 requestSingleUpdate 方法中得到了另外两个错误。关于这些,我附上了两张图片。
第一个错误:
第二个错误:
【问题讨论】: