package an.android.application;
import java.util.Iterator;
import android.app.Activity;
import android.content.Intent;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class SpeedToll extends Activity {
/** Called when the activity is first created. */
private static final int Search = Menu.FIRST;
private static final int Myloc = Menu.FIRST + 1;
private static final int Exit = Menu.FIRST + 2;
private LocationManager locationManager;
private GpsStatus gpsstatus;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* 加载main.xml Layout */
setContentView(R.layout.desktop);
/* 以findViewById()取得Button对象,并加入onClickListener */
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
/* new一个Intent对象,并指定要启动的class */
Intent intent = new Intent();
intent.setClass(SpeedToll.this, Map.class);
/* 调用一个新的Activity */
startActivity(intent);
/* 关闭原本的Activity */
//SpeedToll.this.finish();
}
});
Button b2 = (Button) findViewById(R.id.button1);
b2.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
/* new一个Intent对象,并指定要启动的class */
Intent intent = new Intent();
intent.setClass(SpeedToll.this, Map.class);
/* 调用一个新的Activity */
startActivity(intent);
/* 关闭原本的Activity */
//SpeedToll.this.finish();
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, Search, Menu.NONE, "搜索");
menu.add(0, Myloc, Menu.NONE, "定位");
menu.add(0, Exit, Menu.NONE, "退出");
return true;
}//menu
public boolean onOptionsItemSelected(MenuItem item)
{
//super.onOptionsItemSelected(item);
switch(item.getItemId()){
case Search:
//Search();
break;
case Myloc:
GetMyLocation();
break;
case Exit:
//mLocationManager.removeUpdates(this); //关闭GPS
this.finish();
break;
}
return super.onOptionsItemSelected(item);
}
public boolean GetMyLocation(){
//获取到LocationManager对象
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
//根据设置的Criteria对象,获取最符合此标准的provider对象
String currentProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER).getName();
//根据当前provider对象获取最后一次位置信息
Location currentLocation = locationManager.getLastKnownLocation(currentProvider);
//如果位置信息为null,则请求更新位置信息
if(currentLocation == null){
locationManager.requestLocationUpdates(currentProvider, 0, 0, locationListener);
}
//增加GPS状态监听器
locationManager.addGpsStatusListener(gpsListener);
//直到获得最后一次位置信息为止,如果未获得最后一次位置信息,则显示默认经纬度
//每隔10秒获取一次位置信息
while(true){
currentLocation = locationManager.getLastKnownLocation(currentProvider);
if(currentLocation != null){
Log.d("Location", "Latitude: " + currentLocation.getLatitude());
Log.d("Location", "location: " + currentLocation.getLongitude());
Toast.makeText(SpeedToll.this, "Latitude: " + currentLocation.getLatitude()+ " "
+"location: " + currentLocation.getLongitude(), Toast.LENGTH_SHORT).show();
break;
}else{
Log.d("Location", "Latitude: " + 0);
Log.d("Location", "location: " + 0);
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
Log.e("Location", e.getMessage());
}
}
return false;
}
private GpsStatus.Listener gpsListener = new GpsStatus.Listener(){
//GPS状态发生变化时触发
@Override
public void onGpsStatusChanged(int event) {
//获取当前状态
gpsstatus=locationManager.getGpsStatus(null);
switch(event){
//第一次定位时的事件
case GpsStatus.GPS_EVENT_FIRST_FIX:
break;
//开始定位的事件
case GpsStatus.GPS_EVENT_STARTED:
break;
//发送GPS卫星状态事件
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
Toast.makeText(SpeedToll.this, "GPS_EVENT_SATELLITE_STATUS", Toast.LENGTH_SHORT).show();
Iterable<GpsSatellite> allSatellites = gpsstatus.getSatellites();
Iterator<GpsSatellite> it=allSatellites.iterator();
int count = 0;
while(it.hasNext())
{
count++;
}
Toast.makeText(SpeedToll.this, "Satellite Count:" + count, Toast.LENGTH_SHORT).show();
break;
//停止定位事件
case GpsStatus.GPS_EVENT_STOPPED:
Log.d("Location", "GPS_EVENT_STOPPED");
break;
}
}
};
//创建位置监听器
private LocationListener locationListener = new LocationListener(){
//位置发生改变时调用
@Override
public void onLocationChanged(Location location) {
Log.d("Location", "onLocationChanged");
}
//provider失效时调用
@Override
public void onProviderDisabled(String provider) {
Log.d("Location", "onProviderDisabled");
}
//provider启用时调用
@Override
public void onProviderEnabled(String provider) {
Log.d("Location", "onProviderEnabled");
}
//状态改变时调用
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Location", "onStatusChanged");
}
};
}
主要有两个文件,一个是主文件,一个是xml文件。