话不多少,直接上代码
package android.bzu.hzp;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>AndroidManifest.xml中的代码<?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>在虚拟机中运行时提示错误
package android.bzu.hzp;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>AndroidManifest.xml中的代码<?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>在虚拟机中运行时提示错误
解决方案 »
- 如何实现查找当前所在网络的中的所有计算机名称或是IP地址?
- 求大神:关于android在指定位置显示图片的问题
- 安装Android开发环境,在模拟器中没有出现传说中的HelloWorld
- 如何调用显示已存在的activity?
- 求助:JB4.2 usb蓝牙Dongle问题
- 编译android4.2.2源码,出现Invalid LOCAL_SDK_VERSION ' CURRENT' choices. stop, 请大神帮忙解决?
- 新手求解,每次启动模拟器,都出现这个
- xmpp可以发消息 不能收消息
- 求各位大神来帮忙一下 !!! 安卓开发中的问题
- 有没有办法实现对手机的定位,手机主不会发现的那种
- 手机与PC进行通信
- 如何使解压出的byte[]正常显示成字符串???
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>