android 获取当前位置坐标 经度 纬度

android调用GPS获取当前位置          并且用EditText显示出来


    如果有写的不好的地方    请大神指点一下   

代码如下:


package com.example.gps;
import java.util.List;
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.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;



public class MainActivity extends Activity
{
    private EditText textView;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (EditText) findViewById(R.id.editText1);
        new Thread(new ThreadShow()).start();启动线程
     
    }
    Handler handler=new Handler()
    {
        public void handleMessage(Message message)
        {
            if (message.what==1)
            {
                LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                  // 返回所有已知的位置提供者的名称列表,包括未获准访问或调用活动目前已停用的。
                 
                if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER))
                {
                    Location location=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    if (location!=null)
                    {
                        //获取纬度信息
                        double latitude = location.getLatitude();
                        //获取经度信息
                        double longitude = location.getLongitude();
                        textView.setText("维度:"+latitude+"  经度:"+longitude);
                    }
                    else
                    {
                        LocationListener locationListener=new LocationListener() {
                           
                            @Override
                            public void onStatusChanged(String provider, int status, Bundle extras) {
                                // TODO Auto-generated method stub
                               
                            }
                           
                            @Override
                            public void onProviderEnabled(String provider) {
                                // TODO Auto-generated method stub
                               
                            }
                           
                            @Override
                            public void onProviderDisabled(String provider) {
                                // TODO Auto-generated method stub
                               
                            }
                           
                            @Override
                            public void onLocationChanged(Location location) {
                                // TODO Auto-generated method stub
                                if (location!=null)
                                {
                                    double latitude = location.getLatitude();
                                    //获取经度信息
                                    double longitude = location.getLongitude();
                                    textView.setText("维度:"+latitude+"  经度:"+longitude);
                                }
                            }
                        };
                        lm.requestLocationUpdates(lm.NETWORK_PROVIDER, 1000, 0, locationListener);
                        location=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location!=null)
                        {
                            double latitude = location.getLatitude();
                            //获取经度信息
                            double longitude = location.getLongitude();
                            textView.setText("维度:"+latitude+"  经度:"+longitude);
                        }
                    }
                }
            }
        }
       
    };

   
    //新建线程  并且每秒执行一次
    class ThreadShow implements Runnable {  
         
              @Override 
                public void run() { 
                 // TODO Auto-generated method stub 
                    while (true) { 
                        try { 
                            Thread.sleep(1000);   
                            Message msg = new Message(); 
                            msg.what = 1; 
                            handler.sendMessage(msg); 
                           System.out.println("send..."); 
                        } catch (Exception e) { 
                            // TODO Auto-generated catch block 
                            e.printStackTrace(); 
                            System.out.println("thread error..."); 
                        } 
                    } 
                } 
    }
           
}
QQ群:348165744
348165744
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值