Android手机开发:获取GPS信息

高皓
2023-12-01

1. 主程序文件

package com.ex06.button;

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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ActivityButton extends Activity {  
    /** Called when the activity is first created. */  
    LocationManager locationManager;   
    private EditText editText;
    Button btnGetLocation;  
  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
           
        btnGetLocation=(Button)findViewById(R.id.button1);  
        editText = (EditText)findViewById(R.id.speed);
        editText.setText("Location:\n");
        btnGetLocation.setOnClickListener(new bntOnClickListen());  
          
        //通过getSystemService接口获取LocationManager实例  
        locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);  
          
        //实现监听器 LocationListener   
        LocationListener locationlisten=new LocationListener() {  
              
            public void onStatusChanged(String arg0, int arg1, Bundle arg2) {  
                // TODO Auto-generated method stub  
                // Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数  
            }  
              
            public void onProviderEnabled(String arg0) {  
                // TODO Auto-generated method stub  
                // Provider被enable时触发此函数,比如GPS被打开  
            }  
              
            public void onProviderDisabled(String arg0) {  
                // TODO Auto-generated method stub  
                // Provider被disable时触发此函数,比如GPS被关闭  
            }  
              
            //当坐标改变时触发此函数;如果Provider传进相同的坐标,它就不会被触发  
            @Override
            public void onLocationChanged(Location arg0) {  
                // TODO Auto-generated method stub  
                if (arg0 != null) {     
                    Log.i("log", "Location changed : Lat: "  + arg0.getLatitude() + " Lng: " + arg0.getLongitude()); 
                    editText.setText(arg0.getLatitude() + " Lng: " + arg0.getLongitude() + "\n");
                } else {
                	Log.i("log", "Location changed : Lat: "  + "NULL" + " Lng: " + "NULL");  
                	editText.setText("NULL" + " Lng: " + "NULL" + "\n");
                }
            }
        };  
          
        // 注册监听器 locationListener   
        //第 2 、 3个参数可以控制接收GPS消息的频度以节省电力。第 2个参数为毫秒, 表示调用 listener的周期,第 3个参数为米 ,表示位置移动指定距离后就调用 listener  
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationlisten);  
           
    }  
      
    class bntOnClickListen implements OnClickListener{  
    	@Override
        public void onClick(View v) {  
            // TODO Auto-generated method stub  
            //获取地理位置信息数据(如果没实现监听器和注册监听器,好像获取不了地理数据)  
            Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);  
            if (location!=null) {  
                String strLati = Double.toString(location.getLatitude());  
                String strLong = Double.toString(location.getLongitude());  
                //显示地理位置数据  
                System.out.println("---------地理位置信息---------");  
                System.out.println("---------" + strLati + "/" + strLong + "---------");  
                editText.setText(strLati + "/" + strLong + "\n");
            }  
            else{  
                Log.i("log", "location==NULL");
                editText.setText("location==NULL");
            }  
        }
          
    }  
}  

2. 添加权限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>


 类似资料: