读书人

封锁GPS

发布时间: 2012-09-10 11:02:33 作者: rapoo

关闭GPS

public class MainActivity extends Activity {private LocationManager locationManager;private TextView tv;private StringBuilder builder = new StringBuilder("位置信息:\n");private LocationListener ll;public void getLocation(){locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);String provider = LocationManager.GPS_PROVIDER;Location location = locationManager.getLastKnownLocation(provider);updateMsg(location);ll = new LocationListener(){public void onLocationChanged(Location location) {updateMsg(location);}public void onProviderDisabled(String provider) {}public void onProviderEnabled(String provider) {}public void onStatusChanged(String provider, int status,Bundle extras) {}         };locationManager.requestLocationUpdates(provider, 2000, 10,ll);}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);tv = (TextView) findViewById(R.id.mTextView);Button mLocation = (Button)findViewById(R.id.mLocation);mLocation.setOnClickListener(new OnClickListener() {public void onClick(View v) {getLocation();}});}private void updateMsg(Location l) {if (l != null) {double lat = l.getLatitude();double lng = l.getLongitude();builder.append("(");builder.append(lat);builder.append(",");builder.append(lng);builder.append(")");if(l.hasAccuracy()){builder.append("\n精度:");builder.append(l.getAccuracy());}if(l.hasAltitude()){builder.append("\n高度:");builder.append(l.getAltitude());}if(l.hasBearing()){builder.append("\n方向:");builder.append(l.getBearing());}if(l.hasSpeed()){builder.append("\n速度:");builder.append(l.getSpeed());}builder.append("\n");} else {builder.append("没有位置信息");}tv.setText(builder);}    public void onResume() {    if(locationManager != null) {    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, ll);    }        super.onResume();    }
    public void onPause() {    if(locationManager != null) {    locationManager.removeUpdates(ll);    }        super.onPause();    }}
?? 最近做了一个Gps定位的系统项目,不过研究几天还是没有进展。到最后项目经理告诉我解决方法,和大家分享一下。

读书人网 >移动开发

热点推荐