btnStart = (Button)findViewById(R.id.btnStart);
btnStop = (Button)findViewById(R.id.btnStop);
textView = (TextView)findViewById(R.id.text);
btnStart.setOnClickListener(btnClickListener); //开始定位
btnStop.setOnClickListener(btnClickListener); //结束定位按钮
}
gpsIsOpen是自己写的查看当前GPS是否开启
getLocation 是自己写的一个获取定位信息的方法
mLocationManager.removeUpdates()是停止当前的GPS位置监听
public Button.OnClickListener btnClickListener = new Button.OnClickListener()
{
public void onClick(View v)
{
Button btn = (Button)v;
if(btn.getId() == R.id.btnStart)
{
if(!gpsIsOpen())
return;
mLocation = getLocation();
if(mLocation != null)
textView.setText("维度:" + mLocation.getLatitude() + "n经度:" + mLocation.getLongitude());
else
textView.setText("获取不到数据");
}
else if(btn.getId() == R.id.btnStop)
{
mLocationManager.removeUpdates(locationListener);
}
}
};
private boolean gpsIsOpen()
{
boolean bRet = true;
LocationManager alm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
if(!alm.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
Toast.makeText(this, "未开启GPS", Toast.LENGTH_SHORT).show();
bRet = false;
}
else
{
Toast.makeText(this, "GPS已开启", Toast.LENGTH_SHORT).show();
}
return bRet;
}
判断当前是否开启GPS
private boolean gpsIsOpen()
{
boolean bRet = true;
LocationManager alm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
if(!alm.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
Toast.makeText(this, "未开启GPS", Toast.LENGTH_SHORT).show();