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
代码如下:
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
{
}
QQ群:348165744
348165744