package ziv.locationdemo;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
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;
import android.widget.TextView;

public class LocationDemo extends Activity {
 TextView locationShow = null;        // 显示区
 TextView infoTextView = null;        // 提示信息显示
 Button locationbutton = null;        // 绑定GPS监听按钮
 Button locationbuttonNetwork=null;       // 绑定NEWWOR简体按钮
 LocationManager locationManager = null;     // 定位管理
 public LocationListener locationListener_GPS = null;  // 监听gps返回数据
 public LocationListener locationListener_NETWORK=null;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  // 实例化显示区
  locationShow = (TextView) findViewById(R.id.locationShow);
  // 在视图层提出提示信息文本框
  infoTextView = (TextView) findViewById(R.id.infoTextView);
  // 实例化绑定监听按钮
  locationbutton = (Button) findViewById(R.id.locationbutton);
  locationbuttonNetwork = (Button) findViewById(R.id.locationbuttonNetwork);
  
  // 获得定位管理类
  locationManager = (LocationManager) LocationDemo.this
    .getSystemService(Context.LOCATION_SERVICE);
  // 实例化监听对象变量
  locationListener_GPS = new LocationListenerSelf("GPS");
  locationListener_NETWORK=new LocationListenerSelf("NETWORK");
  locationbuttonNetwork.setOnClickListener(new OnClickListenerButton(LocationManager.NETWORK_PROVIDER));
  // 监听单击按钮
  locationbutton.setOnClickListener(new OnClickListenerButton(LocationManager.GPS_PROVIDER));

 }

 // 执行就绪
 @Override
 protected void onResume() {
  // 在就绪执行中,自动选择位置信息提供者,且触发监听
  if (locationManager != null) {

   // 实例化查询条件
   Criteria criteria = new Criteria();
   criteria.setAccuracy(Criteria.ACCURACY_FINE);// 高精度
   // 获得定位服务提供者名称
   String provider = locationManager.getBestProvider(criteria, true);
   
   //locationManager.setTestProviderEnabled("gps", true);
   //criteria.setCostAllowed(true);

   // 自动位置变化监听
   if("gps".equals(provider)){
    
    locationManager.requestLocationUpdates(provider, 2000, 0,
      locationListener_GPS);
   }else{
    locationManager.requestLocationUpdates(provider, 2000, 0,
      locationListener_NETWORK);
    
   }
   
   // 获得最后的位置信息
   Location location = locationManager.getLastKnownLocation(provider);
   // 显示坐标信息
   if (location != null) {
    showLocation(location,provider);
   } else {
    locationShow.append(" onResume 没获得到定位\n");
    infoTextView.setText("提供者:"+provider);
   }
  }
  super.onResume();
 }

 /**
  * 显示location 信息
  *
  * @param location
  */
 private void showLocation(Location location,String provider) {

  locationShow.append("经度:" + location.getLongitude() + "\n" + "纬度:"
    + location.getLatitude() + "\n");
  infoTextView.setText("提供者:"+provider);

 }
 
 @Override
 protected void onPause() {
  System.out.println("onPause");
  if (locationManager != null) {
   locationManager.removeUpdates(locationListener_GPS);
   locationManager.removeUpdates(locationListener_NETWORK);
  }
  super.onPause();
 }
 private class OnClickListenerButton implements OnClickListener{
  String provider="";
  public OnClickListenerButton(){
   super();
  }
  public OnClickListenerButton(String provider){
   super();
   this.provider=provider;
  }
  @Override
  public void onClick(View v) {
 
   Location location = locationManager
     .getLastKnownLocation(provider);
   // 显示坐标信息
   if (location != null) {
    showLocation(location,provider);
   } else {
    locationShow.append(" 没获得到定位\n");
    infoTextView.setText("提供者:"+provider+";是否可用:"+locationManager.isProviderEnabled(provider));
   }
   //启动gps监听
   if(locationManager.GPS_PROVIDER.equals(provider)){
    
    locationManager.requestLocationUpdates(
      provider, 1000, 0,
      locationListener_GPS);
   }else{
    locationManager.requestLocationUpdates(
      provider, 1000, 0,
      locationListener_NETWORK);
   }
   
  }

 
 }
 /**
  * 位置变化监听
  * @author ziv
  *
  */
 private class LocationListenerSelf implements LocationListener{
  String provider="null";
  public LocationListenerSelf(){
   
  }
  public LocationListenerSelf(String provider){
   this.provider=provider;
  }
  @Override
  public void onLocationChanged(Location location) {
   if (location != null) {
    showLocation(location,provider);
   } else {
    locationShow.append(" location listener 没获得到定位 \n");
    infoTextView.setText("提供者:"+provider);
   }
  }

  @Override
  public void onProviderDisabled(String provider) {
   System.out.println("onProviderDisabled");

  }

  @Override
  public void onProviderEnabled(String provider) {
   System.out.println("onProviderEnabled");

  }

  @Override
  public void onStatusChanged(String provider, int status,
    Bundle extras) {
   System.out.println("onStatusChanged");

  }

 
 }
}

 

 

成功执行 。

更多相关文章

  1. android 监听电源键
  2. Android(安卓)USB状态监控(解决scheme="file")
  3. Android03_事件处理
  4. 我的Android音乐播放器
  5. Android控件编辑时键盘弹起与关闭处理
  6. Robot Framework如何对Android的控件定位
  7. Android事件机制之二:onTouch详解
  8. Android(安卓)基本控件及表单三大控件,事件处理
  9. 彻底解决Android(安卓)GPS没法定位这一顽固问题

随机推荐

  1. Activity的android:launchMode
  2. [Android(安卓)编译(一)] Ubuntu 16.04 L
  3. android测试器
  4. 疯狂Android第二章:Adapter以及部分控件
  5. 浅析android中Bitmap的使用
  6. vlc for android录制视频与截图
  7. android之SMS
  8. 仿优酷Android客户端图片左右滑动(自动滑
  9. 获取Android设备基本信息
  10. Android线程管理之ThreadLocal理解及应用