我只想在我的活动中收到我从Emulator Control发送的位置数据。以下是我在主要活动中使用的代码:
public class LocationAlert extends Activity {
TextView txtMessage;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtMessage = (TextView)findViewById(R.id.txtMsg);
LocationManager lm = (LocationManager)getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, mLocListener);
if (lm.getLastKnownLocation(LocationManager.GPS_PROVIDER) != null){
txtMessage.setText(String.valueOf(lm.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude()));
}
}
LocationListener mLocListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
if (location != null){
txtMessage.setText("Latitude: " + String.valueOf(location.getLatitude()) + ", Longitude: " + String.valueOf(location.getLongitude()));
}
}
};
}
但我没有获得任何数据,并且模拟器中运行的所有进程逐个关闭,我可以在“设备”选项卡中看到。上面的代码出了什么问题?
答案 0 :(得分:1)
您可能在清单文件中缺少某些Android权限。将此添加到您的清单
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>