【问题标题】:Use service to get GPS location, android?使用服务获取 GPS 位置,android?
【发布时间】:2013-08-05 19:03:14
【问题描述】:

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

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

这是我的服务类。

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

【问题讨论】:

  • 你是否在清单中实现了权限
  • .....LocTrack 是我的服务类的名称
  • 没有问互联网,GPS,位置,模拟
  • 是的,我已经设置了所有必要的权限
  • 这里的链接工作正常stackoverflow.com/a/8830135/1915697

标签: android service gps location


【解决方案1】:

最好的方法是使用 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" />

【讨论】:

  • 找不到LocationPollerResult和LocationPollerParameter....这是什么???
  • 对不起,这是一个过时的库...用这个替换以前的库,它会起作用的...[link]dropbox.com/sh/pgxk2v9l5vl0h2j/3svyZnuwOK/…
  • 我认为它要么不工作,要么我做错了什么......我复制粘贴了你的代码,但我无法在 logcat 中找到任何位置......
  • 在您的位置接收器的这些行之间添加另一个日志 Location loc=locationResult.getLocation(); Log.i("位置纬度", String.valueOf(loc.getLatitude()));字符串味精;你有没有在清单中声明你的接收者
  • 我不知道是对还是错,我这样声明 ....仍然不起作用...甚至用包名声明它
猜你喜欢
  • 2012-02-08
  • 1970-01-01
  • 1970-01-01
  • 2015-05-17
  • 1970-01-01
  • 1970-01-01
  • 2013-05-06
  • 2016-07-30
  • 1970-01-01
相关资源
最近更新 更多