80,359
社区成员
发帖
与我相关
我的任务
分享
public class LocationActivity extends Activity {
Button btnlogin; // 登录和退出按钮对像
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnlogin = (Button) findViewById(R.id.login); // 获得用户名和密码按钮实例
btnlogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent service = new Intent(LocationActivity.this, GetLocation.class);
startService(service);
}
});
}
}
public class GetLocation extends Service {
HandlerThread thread;
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
String serviceString = Context.LOCATION_SERVICE;
LocationManager locationManager = (LocationManager) getSystemService(serviceString);
String provider = locationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 20000, 10000,
locationListener);
if (location != null) {
getLocationInfo(location);
} else {
}
}
public void getLocationInfo(Location location) {
// TODO Auto-generated method stub
String latLongInfo;
if (location == null) {
latLongInfo = "No Location Found";
}
HandlerThread thread = new HandlerThread(location);
}
private final LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
getLocationInfo(location); // 获取汽车的新位置
System.out.println("location changed");
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
getLocationInfo(null);
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
getLocationInfo(null);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
};
}
public class HandlerThread {
Handler handler=new Handler();
Location address=null;
int state;
double lat,lng;
public HandlerThread(){}
public HandlerThread(Location location){
handler.post(runnable);
//handler.postDelayed(runnable,40000);
lat=location.getLatitude(); //维度
lng=location.getLongitude(); //精度
}
Runnable runnable=new Runnable() {
@Override
public void run() {
handler.postDelayed(runnable,2300);
System.out.println("汽车位置"+lat+","+lng);
}
};
}