android GPS DEMO
2011-03-11 10:49
225 查看
package com.gps;
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Gps extends Activity implements LocationListener {
private LocationManager lm;
private Button btn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,1.0f, this);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Log.d("===============", "gps not opened");
} else {
Location bestLoc = getBestLocation(lm);
if(bestLoc == null){
Log.d("=============", "gps opened, but can not get gps location");
}else {
Log.d("=====getLatitude=======", String.valueOf(bestLoc.getLatitude()));
Log.d("======getLongitude======", String.valueOf(bestLoc.getLongitude()));
}
}
}
private Location getBestLocation(LocationManager locationManager) {
Location gpsLoc = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location networkLoc = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location bestLoc = null;
if (gpsLoc != null && networkLoc != null) {
// if gps loc is more than 5 min older than network loc
bestLoc = networkLoc.getTime() - 5 * 60 * 1000 > gpsLoc
.getTime() ? networkLoc : gpsLoc;
} else if (gpsLoc != null) {
bestLoc = gpsLoc;
} else if (networkLoc != null) {
bestLoc = networkLoc;
}
return bestLoc;
}
});
}
@Override
public void onLocationChanged(Location paramLocation) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String paramString) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String paramString) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String paramString, int paramInt,
Bundle paramBundle) {
// TODO Auto-generated method stub
}
}
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Gps extends Activity implements LocationListener {
private LocationManager lm;
private Button btn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,1.0f, this);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Log.d("===============", "gps not opened");
} else {
Location bestLoc = getBestLocation(lm);
if(bestLoc == null){
Log.d("=============", "gps opened, but can not get gps location");
}else {
Log.d("=====getLatitude=======", String.valueOf(bestLoc.getLatitude()));
Log.d("======getLongitude======", String.valueOf(bestLoc.getLongitude()));
}
}
}
private Location getBestLocation(LocationManager locationManager) {
Location gpsLoc = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location networkLoc = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location bestLoc = null;
if (gpsLoc != null && networkLoc != null) {
// if gps loc is more than 5 min older than network loc
bestLoc = networkLoc.getTime() - 5 * 60 * 1000 > gpsLoc
.getTime() ? networkLoc : gpsLoc;
} else if (gpsLoc != null) {
bestLoc = gpsLoc;
} else if (networkLoc != null) {
bestLoc = networkLoc;
}
return bestLoc;
}
});
}
@Override
public void onLocationChanged(Location paramLocation) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String paramString) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String paramString) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String paramString, int paramInt,
Bundle paramBundle) {
// TODO Auto-generated method stub
}
}
相关文章推荐
- android gps开发必备资料(含测试demo下载)
- android gps开发必备资料(含测试demo下载)
- Android 移动支付插件demo(主应用+支付插件)
- android 2.3 浏览器因GPS闪退问题
- Android动画学习Demo(3) 沿着贝塞尔曲线移动的Property Animation
- 玩转Android Camera开发(一):Surfaceview预览Camera,基础拍照功能完整demo
- Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)
- android:ListView中使用EditText,遇到的坑,获取点击焦点、保存输入文本的demo
- Android Studio NDK-Jni开发小Demo
- Android mp3 lyric 滚动显示 Demo
- Android系统Gps分析(一)(转载)
- Android actionBar与Fragment结合使用Demo2
- [置顶] Android彻底组件化demo发布
- 【Demo 0003】Android 事件
- Android中如何监听GPS开启和关闭
- Android,实现一个弹幕效果的小demo
- 从问题到解决方案到应用-android-ApiDemo入口源代码学习及应用
- [Android]开启、关闭GPS/Wifi/Bluetooth/Sync,调节屏幕亮度
- android demo URL
- video player demo in android