使用服务获取GPS位置,android?

时间:2013-08-05 06:40:18

标签: android service gps location

这是我第一次使用服务,确实看起来很复杂。

所以我试图在用服务关闭我的应用程序之后获取用户的位置。

这是我的服务类。

public class LocTrack extends Service {


GPSTracker gp;
@Override
public void onCreate() 
{    gp = new GPSTracker(getApplicationContext());
    onLocationChanged(gp);
    super.onCreate();        
}
@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub

    return Service.START_STICKY;
}


@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

public void onLocationChanged(GPSTracker track) {


    // Getting latitude
    double latitude = track.getLatitude();

    // Getting longitude
    double longitude = track.getLongitude();

     Geocoder geocoder = new Geocoder(LocTrack.this, Locale.getDefault());

    try
     {
         List<Address> addresses = geocoder.getFromLocation(latitude, longitude,1);
         Log.e("Addresses","-->"+addresses);

     }
     catch (IOException e)
     {
         e.printStackTrace();

     }

}
}

请告诉我,我做错了什么。 如果我在活动类中使用代码,那么我可以在logcat中获取地址,但在使用服务时却无法获取。

这就是我从活动中调用服务的方式

    Intent intent = new Intent(MainActivity.this, LocTrack.class);
             startService(intent);

1 个答案:

答案 0 :(得分:2)

最好的方法是使用CWAC的位置服务 位置轮询器服务已经为我们使用它只需要给出唤醒它的时间间隔

这样做就像你需要jar文件一样,你可以从https://www.dropbox.com/sh/pgxk2v9l5vl0h2j/3svyZnuwOK/CWAC-LocationPoller.jar

获取它

从您的活动中启动LocationPoller并将警报重复设置为您想要的时间

AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, LocationPoller.class);

Bundle bundle = new Bundle();
LocationPollerParameter parameter = new LocationPollerParameter(bundle);
parameter.setIntentToBroadcastOnCompletion(new Intent(this,
        LocationReceiver.class));
// try GPS and fall back to NETWORK_PROVIDER
parameter.setProviders(new String[] { LocationManager.GPS_PROVIDER,
        LocationManager.NETWORK_PROVIDER });
parameter.setTimeout(120000);
i.putExtras(bundle);

PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        SystemClock.elapsedRealtime(), 300000, pi);

创建一个接收器类位置接收器,从那里获取lat n lon

public class LocationReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {


    try {      
      Bundle b=intent.getExtras();

      LocationPollerResult locationResult = new LocationPollerResult(b);

      Location loc=locationResult.getLocation();
      String msg;

      if (loc==null) {
        loc=locationResult.getLastKnownLocation();

        if (loc==null) {
          msg=locationResult.getError();
        }
        else {
          msg="TIMEOUT, lastKnown="+loc.toString();
        }
      }
      else {
        msg=loc.toString();



        Log.i("Location Latitude", String.valueOf(loc.getLatitude()));
        Log.i("Location Longitude", String.valueOf(loc.getLongitude()));
        Log.i("Location Accuracy", String.valueOf(loc.getAccuracy()));




      }

      Log.i(getClass().getSimpleName(), "received location: " + msg);   



    }
    catch (Exception e) {
      Log.e(getClass().getName(), e.getMessage());
    }
  }

并将其添加到您的清单

   <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

   <receiver android:name="com.commonsware.cwac.locpoll.LocationPoller" />

   <service android:name="com.commonsware.cwac.locpoll.LocationPollerService" />

以及您将获取所有内容的接收者声明

   <receiver android:name="com.RareMediaCompany.Helios.LocationReceiver" />