话就不多说了,直接上代码吧,希望高手帮忙
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.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String serviceString = Context.LOCATION_SERVICE;
LocationManager locationManager = (LocationManager)getSystemService(serviceString);
String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
getLocationInfo(location);
locationManager.requestLocationUpdates(provider, 2000, 0, locationListener);
}
private void getLocationInfo(Location location){
String latLongInfo;
TextView locationText = (TextView)findViewById(R.id.tv1);
if (location != null){
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongInfo = "Lat: " + lat + "\nLong: " + lng;
}
else{
latLongInfo = "No location found";
}
locationText.setText("Your Current Position is:\n" + latLongInfo);
}
private final LocationListener locationListener = new LocationListener(){
@Override
public void onLocationChanged(Location location) {
getLocationInfo(location);
}
@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
}
};
}
下面是main.xml中的内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:id="@+id/tv1"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.bzu.hzp"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="3" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
</application>
</manifest>
是什么问题啊?
其实,没有看懂楼主要问的是什么问题,那个图片显示不了。
可能会存在一些几个方面的问题吧:
1、用户权限的问题,看看是否拥有GPS相关的权限;
2、检查是否打开了GPS;
3、实验GPS一定要在室外进行,在室内是检测不到GPS信号的。