我在这里关注指南:http://www.firstdroid.com/2010/04/29/android-development-using-gps-to-get-current-location-2/
我正在尝试创建一个Android应用程序,只需获取设备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”。
答案 0 :(得分:1)
我发现您的代码没有任何问题。相同的代码对我来说很好。
我认为这可能是您的文字视图(layout_height或其他东西)的问题。