2010.12.10——— android 定位跟踪

需求 :
获得手机的坐标 实时不断的显示在应用里


Activity.java





import android.app.Activity;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;import android.view.KeyEvent;import android.widget.TextView;public class MainActivity extends Activity {private LocationManager locationManager;private TextView tv;//private StringBuilder builder = new StringBuilder("位置信息:\n");@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);tv = (TextView) findViewById(R.id.myTextView01);String provider = LocationManager.GPS_PROVIDER;Location location = locationManager.getLastKnownLocation(provider);//updateMsg(location);LocationListener ll = new LocationListener(){public void onLocationChanged(Location l) {//updateMsg(l);StringBuilder builder = new StringBuilder("位置信息:\n");if (l != null) {double lat = l.getLatitude();double lng = l.getLongitude();builder.append("(");builder.append(lat);builder.append(",");builder.append(lng);builder.append(")");if(l.hasAccuracy()){builder.append("\n精度:");builder.append(l.getAccuracy());}if(l.hasAltitude()){builder.append("\n高度:");builder.append(l.getAltitude());}if(l.hasBearing()){builder.append("\n方向:");builder.append(l.getBearing());}if(l.hasSpeed()){builder.append("\n速度:");builder.append(l.getSpeed());}builder.append("\n");} else {builder.append("没有位置信息");}tv.setText(builder);}public void onProviderDisabled(String provider) {}public void onProviderEnabled(String provider) {}public void onStatusChanged(String provider, int status,Bundle extras) {}         };locationManager.requestLocationUpdates(provider, 500, 5,ll);}//private void updateMsg(Location l) {//StringBuilder builder = new StringBuilder("位置信息:\n");//if (l != null) {//double lat = l.getLatitude();//double lng = l.getLongitude();//builder.append("(");//builder.append(lat);//builder.append(",");//builder.append(lng);//builder.append(")");////if(l.hasAccuracy()){//builder.append("\n精度:");//builder.append(l.getAccuracy());//}////if(l.hasAltitude()){//builder.append("\n高度:");//builder.append(l.getAltitude());//}////if(l.hasBearing()){//builder.append("\n方向:");//builder.append(l.getBearing());//}////if(l.hasSpeed()){//builder.append("\n速度:");//builder.append(l.getSpeed());//}////builder.append("\n");////} else {//builder.append("没有位置信息");//}//tv.setText(builder);//}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {// 按下键盘上返回按钮if (keyCode == KeyEvent.KEYCODE_BACK) {new AlertDialog.Builder(this).setMessage("确定退出系统吗?").setNegativeButton("取消",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,int which) {}}).setPositiveButton("确定",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,int whichButton) {finish();}}).show();return true;} else {return super.onKeyDown(keyCode, event);}}@Overrideprotected void onDestroy() {super.onDestroy();// 或者下面这种方式//System.exit(0);//建议用这种android.os.Process.killProcess(android.os.Process.myPid());}}


最小时间 500
最短距离 5

locationManager.requestLocationUpdates(provider, 500, 5,ll);

当符合这些条件时 更新当前位置

当更新位置时 就会掉用 location的onLocationChanged方法 来更新UI

刚开始 我封装了一个方法 updateMsg() 但是 在onLocationChanged里面调用 貌似 UI不会变化
所以 又把updateMsg()的代码写到了onLocationChanged里面


不过 后来 我又试了试 可以的


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:text="" android:id="@+id/myTextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"/></LinearLayout>














更多相关文章

  1. Android ADB=Android Debug Bridge帮助信息
  2. 更改Android AVD模拟器创建路径位置的方法
  3. android studio3.0build.gradle包含的svn信息apk名称写法等
  4. android定位个人当前位置
  5. Android 获取手机卡相关信息
  6. Android 原生获取地理位置
  7. android设备信息获取
  8. android电池信息简介
  9. wifi和GPRS下获取android的IP信息

随机推荐

  1. Android™ 2.1 android.R.drawable Icon
  2. Android(安卓)内存指标分析
  3. arm-linux-androideabi-ld options
  4. Android下拉列表使用方法
  5. processBitmap - java.lang.IllegalState
  6. Android(安卓)参考网站
  7. [Android] AsyncTask的使用
  8. Android双向滚动ScrollView
  9. android的几种默认路径
  10. Android的NDK开发(1)————Android(安