我的问题如下。 我想开发一个非常简单的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方法中得到了两个错误。关于这些我附上了两张照片。
第一个错误:
第二个错误:
答案 0 :(得分:0)
如果您只想从网络中读取位置更新,请尝试使用以下方法:
requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)
并将提供程序作为LocationManager.NETWORK_PROVIDER
传递答案 1 :(得分:0)
getLastKnownLocation()
返回上一个已知位置,a.k.a。不会重新获取该位置。
您必须使用其中一种方法onStart
注册活动开始/恢复的位置更新(onResume
或LocationManager.requestLocationUpdates(...)
)(我建议活动将实现LocationListener),
每次调用LocationListener.onLocationChanged(Location)
时保存新位置,并在按下按钮时使用保存的位置。
另一种选择是在按钮点击上注册,然后等待位置更新,然后才显示祝酒。
public class MainActivity extends ActionBarActivity implements LocationListener {
double lat = 0;
double lon = 0;
LocationListener asd = new MyLocationListener();
Location loc;
LocationManager locManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
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);
}
private void _getLocation()
{
try
{
this.loc = null;
locManager.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, this, null);
// Waiting for the update
while(loc == null);
Toast.makeText(this, "Long: " + loc.getLongitude() + " Lat: " + loc.getLatitude(), Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Toast.makeText(this, ex.toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onLocationChanged(Location locFromGps)
{
this.loc = locFromGps;
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}