package com.potergps.findlocation;
import java.util.List;
import com.poter.findlocation.R;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
private LocationManager lm; //定义
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lm=(LocationManager) getSystemService(LOCATION_SERVICE) ;//初始化
List<String> provider= lm.getAllProviders();//得到所有的定位方式
for(String way:provider){
System.out.println(way);
Log.i("##############", way);
};
//监听定位,第一个参数方式,2,时间。3,距离。4,监听
lm.requestLocationUpdates("gps", 0, 0, 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
String longitude = "j:" + location.getLongitude() + "\n";
String latitude = "w:" + location.getLatitude() + "\n";
String accuracy = "a" + location.getAccuracy() + "\n";
tv=(TextView) findViewById(R.id.tv);
tv.setText( "j:" + location.getLongitude() + "\n"+"w:" + location.getLatitude()
+ "\n"+"a" + location.getAccuracy() + "\n");
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
原文:http://www.cnblogs.com/chole/p/5156357.html