2

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

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

这是我的服务班。

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);
4

1 回答 1

2

最好的方法是使用 CWAC 的位置服务 位置轮询服务已经为我们提供了使用它只是你必须给出时间间隔来唤醒它

这样做你需要从https://www.dropbox.com/sh/pgxk2v9l5vl0h2j/3svyZnuwOK/CWAC-LocationPoller.jar获取的 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);

制作一个接收器类 Location Receiver 从中获取 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" />
于 2013-08-05T07:02:41.603 回答