locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = locManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER); updateGpsView(location);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000,
8, new LocationListener() { @Override
public void onStatusChanged(String provider, int status,
Bundle extras) { } @Override
public void onProviderEnabled(String provider) {
updateGpsView(locManager.getLastKnownLocation(provider)); } @Override
public void onProviderDisabled(String provider) {
updateGpsView(null); } @Override
public void onLocationChanged(Location location) {
updateGpsView(location); }
});
} // 动态更新GPS数据
public void updateGpsView(Location newLocation) {
int altitude;
float speed;
double lat;
double lon;
if (newLocation != null) {
currentAltitude = altitude = (int) newLocation.getAltitude();
speed = newLocation.getSpeed();
currentLat = lat = newLocation.getLatitude();
currentLon = lon = newLocation.getLongitude();
tv_altitude.setText(Integer.toString(altitude));
tv_speed.setText(Float.toString(speed));
tv_latitude.setText(Double.toString(lat));
tv_longitude.setText(Double.toString(lon));
} else {
tv_altitude.setText("0");
tv_speed.setText("0");
tv_latitude.setText("0");
tv_longitude.setText("0");
}
}
像这个getSystemService这个方法是来自Activity这个类的,难道我要要封装的类extends Activity?这不合理吧AndroidGPSJava

解决方案 »

  1.   

    实现Stub接口,并将service实例添加到ServiceManager统一管理,就可以通过getSystemService检索到你的服务,具体参考SystemServer.java,不过这个工作一般是手机厂商做的自己写的service,用BindService访问好了
      

  2.   


    额我不是要写Service
    我是要通过locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    获取系统的Service但是这个函数在Activity这个类里的如果我想把获取Gps这一功能封装出来难不成还要集成Activity?
      

  3.   

    不用啊,把Context作为参数传入封装类就可以了public Class Utility
    {
       Context mContext;
       public Utility(Context context)
       {
           mContext = context;
       }   public XXX getGpsInfo()
       {
         LocationManager locManager = (LocationManager) mContext .getSystemService(Context.LOCATION_SERVICE);
            Location location = locManager
                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);                       .....
       }
    }
      

  4.   

    http://download.csdn.net/detail/shen332401890/5337225