内部
public class IAmHere extends Activity implements LocationListener {
我有
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
和
里面
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.iamhere);
我有
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List<String> providers = lm.getProviders(true);
/* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
Location l = null;
for (int i=providers.size()-1; i>=0; i--) {
l = lm.getLastKnownLocation(providers.get(i));
if (l != null) break;
}
double[] gps = new double[2];
if (l != null) {
gps[0] = l.getLatitude();
gps[1] = l.getLongitude();
}
gpsString = (TextView)findViewById(R.id.gpsString);
String Data = "";
String koordinata1 = Double.toString(gps[0]);
String koordinata2 = Double.toString(gps[1]);
Data = Data + koordinata1 + " | " + koordinata2 + "\n";
gpsString.setText(String.valueOf(Data));
但似乎不起作用?为什么?我的意思是即使模拟器不想发送GPS数据 - 当我通过UI或控制台点击“发送”时,没有任何反应......?
谢谢。
答案 0 :(得分:1)
首先,您在LocationListener
上实施了IAmHere
,然后对该界面不执行任何操作。
其次,您正在调用getLastKnownLocation()
,但您无法触发Android在任何提供商上启动收集位置数据。
这两个问题是相关的。
位置服务处于关闭状态,直到有人注册以获取位置数据。通常情况下,这是通过requestLocationUpdates()
上的LocationManager
...来使用您实施的LocationListener
界面。
使用LocationManager
的方法是:
LocationListener
requestLocationUpdates()
LocationListener
(例如onDestroy()
),以免泄漏内存onLocationChanged()
时,请执行一些有用的操作 Here is a sample project以这种方式使用LocationManager
和LocationListener
。