我在这里以下指南:http://www.firstdroid.com/2010/04/29/android-development-using-gps-to-get-current-location-2/GPS定位 - 安卓
我试图做一个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”。
你有清单中的以下行: ? –
NickT
是的,我喜欢。我会将其添加到帖子中。我不知道,如果我没有那条线,模拟器是否会工作... – gsingh2011
您是否在真正的手机上发生错误或只是没有位置更新?确保你的手机设置正确(GPS和NETWORK位置),不要有飞行模式,等等。 – Jack