我在这里遵循指南:http://www.firstdroid.com/2010/04/29/android-development-using-gps-to-get-current-location-2/
我正试图使一个安卓应用程序,简单地得到设备的GPS坐标,并显示在屏幕上。当我telnet到模拟器并输入"geo fix 30.0 30.0"时,我的代码在模拟器中工作得很好,但是当我在手机上使用它时,它永远不会得到坐标。我的GPS工作正常,因为我打开地图,它有我的确切位置。所以下面的代码一定有问题:
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class BusAppActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Use the LocationManager class to obtain GPS locations */
LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener locListener = new MyLocationListener();
locManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, locListener);
}
/* To test in the emulator, use the telnet commands:
* telnet localhost 5554
* geo fix 30.0 30.0 */
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
TextView tv = (TextView) findViewById(R.id.textview);
@Override
public void onLocationChanged(Location loc){
Log.d("tag", "Finding Latitude");
double lat = loc.getLatitude();
Log.d("tag", "Lat: "+String.valueOf(lat));
Log.d("tag", "Finding Longitude");
double lon = loc.getLongitude();
Log.d("tag", "Lon: "+String.valueOf(lon));
String Text = "My current location is: " +
"nLatitude = " + lat +
"nLongitude = " + lon;
// Display location
tv.setText(Text);
}
@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){
}
}
}
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="gsingh.busapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".BusAppActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
如果不太麻烦的话,也许有人可以构建这个项目并在他们的手机上试用?所有需要添加的是将唯一的textview标记为"textview"
我找不到你的代码有任何问题。同样的代码对我来说工作得很好。
我认为这可能是你的文本视图(layout_height或其他东西)的问题。